feat(webui): rename sidebar topics inline

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-18 01:48:42 +08:00
1 parent 7edd377352
commit d9537bcc94
23 files changed
+736 -68

No files matched your search

+50 -10
View File
@@ -47,6 +47,7 @@ import {
HoverHint,
} from "@/components/ui/tooltip";
import { MAX_WORKBENCH_PANES } from "@/components/workbench/workbench-model";
import { SidebarTopicRename } from "@/components/SidebarTopicRename";
import {
SIDEBAR_SELECTION_ITEM_CLASS,
SidebarSelectionHighlight,
@@ -225,7 +226,7 @@ interface ChatListProps {
onRequestDelete: (key: string, label: string) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[]) => void;
onTogglePin: (key: string) => void;
onRequestRename: (key: string, label: string) => void;
onRequestRename: (key: string, title: string) => Promise<void>;
onRequestRenameTab?: (key: string, label: string) => void;
onToggleArchive: (key: string) => void;
paneGroups?: Record<string, SidebarPaneGroup>;
@@ -339,6 +340,22 @@ export const ChatList = memo(function ChatList({
const [paneDropTarget, setPaneDropTarget] = useState<string | null>(null);
const [deleteSelectionMode, setDeleteSelectionMode] = useState(false);
const [openActionMenuId, setOpenActionMenuId] = useState<string | null>(null);
const [renamingTopic, setRenamingTopic] = useState<{ key: string } | null>(null);
const renamingKey = renamingTopic?.key ?? null;
const previousRenamingKeyRef = useRef<string | null>(null);
const beginRename = (key: string) => setRenamingTopic({ key });
const endRename = () => {
setRenamingTopic((current) => current === renamingTopic ? null : current);
};
useLayoutEffect(() => {
const previousKey = previousRenamingKeyRef.current;
previousRenamingKeyRef.current = renamingKey;
if (renamingKey !== null || previousKey === null) return;
const row = Array.from(scrollViewportRef.current?.querySelectorAll<HTMLElement>(
"[data-chat-row]",
) ?? []).find((element) => element.dataset.chatRow === previousKey);
row?.querySelector<HTMLButtonElement>("button")?.focus({ preventScroll: true });
}, [renamingKey]);
const [selectedDeleteKeys, setSelectedDeleteKeys] = useState<Set<string>>(
() => new Set(),
);
@@ -904,6 +921,9 @@ export const ChatList = memo(function ChatList({
onSelectPane={onSelectPane}
onRequestDelete={onRequestDelete}
onRequestRename={onRequestRename}
renamingKey={renamingKey}
onBeginRename={beginRename}
onEndRename={endRename}
onTogglePin={onTogglePin}
onToggleArchive={onToggleArchive}
pinned={pinnedPanes}
@@ -968,7 +988,14 @@ export const ChatList = memo(function ChatList({
&& "bg-sidebar-accent/55 text-sidebar-accent-foreground",
)}
>
<button
{renamingKey === s.key ? (
<SidebarTopicRename
key={s.key}
title={title}
onSave={(nextTitle) => onRequestRename(s.key, nextTitle)}
onClose={endRename}
/>
) : <button
type="button"
onClick={(event) => {
if (deleteSelectionMode) {
@@ -1011,9 +1038,9 @@ export const ChatList = memo(function ChatList({
</span>
{isPinned ? <PinnedChatIndicator /> : null}
</span>
</button>
</button>}
<SessionActivityIndicator state={activityState} />
{!deleteSelectionMode ? (
{!deleteSelectionMode && renamingKey !== s.key ? (
<DropdownMenu
modal={false}
open={actionMenus.openId === actionMenuId}
@@ -1043,7 +1070,7 @@ export const ChatList = memo(function ChatList({
{isPinned ? t("chat.unpin") : t("chat.pin")}
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() => onRequestRename(s.key, title)}
onSelect={() => beginRename(s.key)}
>
<Pencil className="h-4 w-4 shrink-0" />
{t("chat.rename")}
@@ -1319,6 +1346,9 @@ function ActivePaneRows({
onSelectPane,
onRequestDelete,
onRequestRename,
renamingKey,
onBeginRename,
onEndRename,
onTogglePin,
onToggleArchive,
pinned,
@@ -1343,7 +1373,10 @@ function ActivePaneRows({
recovery: ReadonlySet<string>;
onSelectPane?: (tabKey: string, paneKey: string) => void;
onRequestDelete: (key: string, label: string) => void;
onRequestRename: (key: string, label: string) => void;
onRequestRename: (key: string, title: string) => Promise<void>;
renamingKey: string | null;
onBeginRename: (key: string) => void;
onEndRename: () => void;
onTogglePin: (key: string) => void;
onToggleArchive: (key: string) => void;
pinned: ReadonlySet<string>;
@@ -1414,7 +1447,14 @@ function ActivePaneRows({
&& "bg-sidebar-accent/55 text-sidebar-accent-foreground",
)}
>
<button
{renamingKey === pane.key ? (
<SidebarTopicRename
key={pane.key}
title={pane.title}
onSave={(nextTitle) => onRequestRename(pane.key, nextTitle)}
onClose={onEndRename}
/>
) : <button
type="button"
onClick={(event) => {
if (deleteSelectionMode) {
@@ -1450,9 +1490,9 @@ function ActivePaneRows({
{isPinned ? <PinnedChatIndicator /> : null}
</span>
</button>
</button>}
<SessionActivityIndicator state={activityState} />
{!deleteSelectionMode ? <DropdownMenu
{!deleteSelectionMode && renamingKey !== pane.key ? <DropdownMenu
modal={false}
open={actionMenus.openId === actionMenuId}
onOpenChange={(open) => actionMenus.onOpenChange(actionMenuId, open)}
@@ -1479,7 +1519,7 @@ function ActivePaneRows({
{isPinned ? t("chat.unpin") : t("chat.pin")}
</DropdownMenuItem>
<DropdownMenuItem
onSelect={() => onRequestRename(pane.key, pane.title)}
onSelect={() => onBeginRename(pane.key)}
>
<Pencil className="h-4 w-4 shrink-0" />
{t("chat.rename")}
+1 -1
View File
@@ -49,7 +49,7 @@ interface SidebarProps {
onRequestDelete: (key: string, label: string) => void;
onRequestDeleteMany?: (items: SidebarDeleteItem[]) => void;
onTogglePin: (key: string) => void;
onRequestRename: (key: string, label: string) => void;
onRequestRename: (key: string, title: string) => Promise<void>;
onRequestRenameTab?: (key: string, label: string) => void;
onToggleArchive: (key: string) => void;
paneGroups?: Record<string, SidebarPaneGroup>;
+115
View File
@@ -0,0 +1,115 @@
import { useEffect, useId, useRef, useState } from "react";
import { Check, Loader2, X } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Input } from "@/components/ui/input";
interface SidebarTopicRenameProps {
title: string;
onSave: (title: string) => Promise<void>;
onClose: () => void;
}
export function SidebarTopicRename({ title, onSave, onClose }: SidebarTopicRenameProps) {
const { t } = useTranslation();
const [value, setValue] = useState(title);
const [saving, setSaving] = useState(false);
const [failed, setFailed] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
const savingRef = useRef(false);
const composingRef = useRef(false);
const errorId = useId();
useEffect(() => {
inputRef.current?.focus();
inputRef.current?.select();
}, []);
const save = async () => {
const cleaned = value.trim();
if (!cleaned || savingRef.current) return;
savingRef.current = true;
setSaving(true);
setFailed(false);
try {
await onSave(cleaned);
onClose();
} catch {
setFailed(true);
inputRef.current?.focus();
} finally {
savingRef.current = false;
setSaving(false);
}
};
return (
<form
data-sidebar-topic-rename
className="min-w-0 flex-1 py-0.5"
aria-busy={saving}
onSubmit={(event) => {
event.preventDefault();
void save();
}}
onClick={(event) => event.stopPropagation()}
onPointerDown={(event) => event.stopPropagation()}
onContextMenu={(event) => event.stopPropagation()}
onKeyDown={(event) => {
event.stopPropagation();
const composing = composingRef.current
|| event.nativeEvent.isComposing || event.nativeEvent.keyCode === 229;
if (event.key === "Enter" && event.target === inputRef.current) {
event.preventDefault();
if (!composing) void save();
} else if (event.key === "Escape" && !composing) {
event.preventDefault();
if (!savingRef.current) onClose();
}
}}
>
<div className="flex min-w-0 items-center gap-0.5">
<Input
ref={inputRef}
className="h-7 min-w-0 flex-1 rounded-md border-sidebar-border bg-sidebar px-1.5 py-0 text-[13px]"
aria-label={t("chat.renamePlaceholder")}
aria-invalid={failed || undefined}
aria-describedby={failed ? errorId : undefined}
placeholder={t("chat.renamePlaceholder")}
value={value}
maxLength={160}
readOnly={saving}
onCompositionStart={() => { composingRef.current = true; }}
onCompositionEnd={() => { composingRef.current = false; }}
onChange={(event) => {
setValue(event.target.value);
setFailed(false);
}}
/>
<button
type="submit"
aria-label={t("chat.renameSave")}
disabled={saving || !value.trim()}
className="touch-target inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-md text-sidebar-foreground disabled:opacity-40"
>
{saving ? <Loader2 className="h-3.5 w-3.5 animate-spin" aria-hidden />
: <Check className="h-3.5 w-3.5" aria-hidden />}
</button>
<button
type="button"
aria-label={t("deleteConfirm.cancel")}
disabled={saving}
onClick={onClose}
className="touch-target inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-md text-sidebar-muted-foreground disabled:opacity-40"
>
<X className="h-3.5 w-3.5" aria-hidden />
</button>
</div>
{failed ? (
<p id={errorId} role="alert" className="px-1.5 py-1 text-xs text-destructive">
{t("chat.renameFailed")}
</p>
) : null}
</form>
);
}