feat(ai): ai editable title layout

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-10-16 21:16:06 +08:00
parent f9039c688c
commit f960beca82
7 changed files with 204 additions and 45 deletions
@@ -11,7 +11,7 @@ import { useBlockActions, useChatActions, useCurrentTitle } from "~/modules/ai-c
import { useAIRootState } from "../../store/AIChatContext"
import { ChatHistoryDropdown } from "./ChatHistoryDropdown"
import { ChatMoreDropdown } from "./ChatMoreDropdown"
import { EditableTitle } from "./EditableTitle"
import { AIHeaderTitle } from "./EditableTitle"
import { TaskReportDropdown } from "./TaskReportDropdown"
// Base header layout with shared logic inside
@@ -68,7 +68,7 @@ const ChatHeaderLayout = ({
<div className="mr-2 flex min-w-0 items-center">
<ChatHistoryDropdown
triggerElement={
<EditableTitle title={displayTitle} placeholder={t("common.new_chat")} />
<AIHeaderTitle title={displayTitle} placeholder={t("common.new_chat")} />
}
/>
</div>
@@ -5,7 +5,6 @@ import { ScrollArea } from "@follow/components/ui/scroll-area/ScrollArea.js"
import { cn, nextFrame, stopPropagation } from "@follow/utils"
import type { VariantProps } from "class-variance-authority"
import { cva } from "class-variance-authority"
import { noop } from "es-toolkit"
import type { EditorState, LexicalEditor } from "lexical"
import { $getRoot } from "lexical"
import type { Ref } from "react"
@@ -131,6 +130,7 @@ export const ChatInput = memo(
void handleSend()
return true
}
return false
},
[handleSend, isProcessing],
@@ -185,11 +185,7 @@ export const ChatInput = memo(
</span>
</label>
)}
<AIModelIndicator
className="-mr-1.5 ml-1 translate-y-[2px] self-start"
// Current not support switch model, will open this feature later
onModelChange={noop}
/>
<AIModelIndicator className="-mr-1.5 ml-1 translate-y-[2px] self-start" />
</div>
</div>
</div>
@@ -1,39 +1,186 @@
import { cn } from "@follow/utils/utils"
import type { ButtonHTMLAttributes } from "react"
import { useCallback, useEffect, useRef, useState } from "react"
interface EditableTitleProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type"> {
title?: string
placeholder?: string
import { useChatActions } from "~/modules/ai-chat/store/hooks"
interface TitleInputProps {
initialValue: string
placeholder: string
onSave: (value: string) => Promise<void>
onCancel: () => void
}
export const EditableTitle = function EditableTitle({
const TitleInput = ({ initialValue, placeholder, onSave, onCancel }: TitleInputProps) => {
const [editValue, setEditValue] = useState(initialValue)
const [isSaving, setIsSaving] = useState(false)
const [saveSuccess, setSaveSuccess] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
// Auto-focus and select input on mount
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus()
inputRef.current.select()
}
}, [])
const handleSave = useCallback(async () => {
if (isSaving) return
const trimmedValue = editValue.trim()
// If no changes, just cancel
if (trimmedValue === initialValue) {
onCancel()
return
}
try {
setIsSaving(true)
await onSave(trimmedValue)
// Show success feedback briefly
setSaveSuccess(true)
setTimeout(() => {
setSaveSuccess(false)
onCancel()
}, 500)
} catch (error) {
console.error("Failed to save title:", error)
// Keep in edit mode on error to allow retry
setIsSaving(false)
}
}, [editValue, initialValue, onSave, onCancel, isSaving])
const handleKeyDown = useCallback(
(event: React.KeyboardEvent) => {
if (event.key === "Enter") {
event.preventDefault()
handleSave()
} else if (event.key === "Escape") {
event.preventDefault()
onCancel()
}
},
[handleSave, onCancel],
)
const handleBlur = useCallback(() => {
if (!isSaving) {
handleSave()
}
}, [handleSave, isSaving])
return (
<div className="relative flex min-w-0 flex-1 items-center">
<input
ref={inputRef}
type="text"
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={handleKeyDown}
onBlur={handleBlur}
disabled={isSaving}
className={cn(
"text-text border-border bg-fill flex-1 truncate rounded-md border px-2 py-1 font-bold outline-none",
"focus:border-blue focus:ring-blue focus:ring-1",
isSaving && "opacity-50",
)}
placeholder={placeholder}
/>
{isSaving && (
<i className="i-mgc-loading-3-cute-re text-text-secondary absolute right-2 size-4 animate-spin" />
)}
{saveSuccess && <i className="i-mgc-check-cute-re text-green absolute right-2 size-4" />}
</div>
)
}
interface AIHeaderTitleProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type"> {
title?: string
placeholder?: string
onTitleSave?: (newTitle: string) => Promise<void>
}
export const AIHeaderTitle = ({
ref,
title = "",
placeholder = "Untitled Chat",
className,
onTitleSave,
...buttonProps
}: EditableTitleProps & { ref?: React.RefObject<HTMLButtonElement | null> }) {
}: AIHeaderTitleProps & { ref?: React.RefObject<HTMLButtonElement | null> }) => {
const chatActions = useChatActions()
const [isEditing, setIsEditing] = useState(false)
const displayTitle = title || placeholder
const { ["aria-label"]: ariaLabelProp, ...restButtonProps } = buttonProps
const ariaLabel = ariaLabelProp ?? displayTitle
const handleStartEdit = useCallback((e: React.MouseEvent) => {
e.stopPropagation()
setIsEditing(true)
}, [])
const handleSave = useCallback(
async (newTitle: string) => {
if (onTitleSave) {
await onTitleSave(newTitle)
} else {
await chatActions.editChatTitle(newTitle)
}
},
[onTitleSave, chatActions],
)
const handleCancel = useCallback(() => {
setIsEditing(false)
}, [])
return (
<button
{...restButtonProps}
ref={ref}
type="button"
aria-haspopup="menu"
aria-label={ariaLabel}
className={cn(
"group flex min-w-0 flex-1 cursor-pointer items-center gap-2 rounded-md bg-transparent p-0 text-left",
"focus-visible:ring-border focus-visible:ring-offset-background outline-none transition-opacity hover:opacity-80 focus-visible:ring-2 focus-visible:ring-offset-2",
className,
<div className="group relative flex min-w-0 flex-1 items-center gap-2">
{isEditing ? (
<TitleInput
initialValue={title}
placeholder={placeholder}
onSave={handleSave}
onCancel={handleCancel}
/>
) : (
<>
<button
{...restButtonProps}
ref={ref}
type="button"
aria-haspopup="menu"
aria-label={ariaLabel}
className={cn(
"group/button flex min-w-0 flex-1 cursor-pointer items-center gap-2 rounded-md bg-transparent p-0 text-left",
"focus-visible:ring-border focus-visible:ring-offset-background outline-none transition-opacity hover:opacity-80 focus-visible:ring-2 focus-visible:ring-offset-2",
className,
)}
>
<h1 className="text-text truncate font-bold">
<span className="animate-mask-left-to-right [--animation-duration:1s]">
{displayTitle}
</span>
</h1>
<i className="i-mingcute-down-line text-text-secondary group-data-[state=open]:text-text group-hover/button:text-text size-4 transition-all duration-200 group-data-[state=open]:rotate-180" />
</button>
<button
type="button"
onClick={handleStartEdit}
className={cn(
"text-text-secondary hover:text-text flex size-6 items-center justify-center rounded opacity-0 transition-all group-hover:opacity-100",
"hover:bg-fill",
)}
aria-label="Edit title"
>
<i className="i-mgc-edit-cute-re size-4" />
</button>
</>
)}
>
<h1 className="text-text truncate font-bold">
<span className="animate-mask-left-to-right [--animation-duration:1s]">{displayTitle}</span>
</h1>
<i className="i-mingcute-down-line text-text-secondary group-data-[state=open]:text-text group-hover:text-text size-4 transition-colors transition-transform duration-200 group-data-[state=open]:rotate-180" />
</button>
</div>
)
}
@@ -23,6 +23,7 @@ import { EntrySubscriptionList } from "~/modules/entry-column/EntrySubscriptionL
import { EntryContent } from "~/modules/entry-content/components/entry-content"
import { AIEntryHeader } from "~/modules/entry-content/components/entry-header"
import { AppLayoutGridContainerProvider } from "~/providers/app-grid-layout-container-provider"
import { MainViewHotkeysProvider } from "~/providers/main-view-hotkeys-provider"
/**
* AIEnhancedTimelineLayout Component
@@ -200,6 +201,7 @@ export const AIEnhancedTimelineLayout = memo(function AIEnhancedTimelineLayout()
<AIChatRoot wrapFocusable={false}>
<AIEnhancedTimelineLayoutImpl />
<AIIndicator />
<MainViewHotkeysProvider />
</AIChatRoot>
)
})
@@ -1,3 +1,4 @@
import { jotaiStore } from "@follow/utils"
import { getStorageNS } from "@follow/utils/ns"
import { transformShortcut } from "@follow/utils/utils"
import { useAtomValue, useSetAtom } from "jotai"
@@ -105,16 +106,16 @@ type ExtractSetType<T extends Set<unknown>> = T extends Set<infer U> ? U : never
export type AllowCustomizeCommandId = ExtractSetType<typeof allowCustomizeCommands>
export type BindingCommandId = keyof typeof defaultCommandShortcuts
const __commandShortcutAtom = (commandId: BindingCommandId) =>
selectAtom(overrideCommandShortcutsAtom, (v) => {
return v[commandId] ?? defaultCommandShortcuts[commandId]
})
export const useCommandShortcut = (commandId: BindingCommandId): string => {
return useAtomValue(
useMemo(
() =>
selectAtom(overrideCommandShortcutsAtom, (v) => {
return v[commandId] ?? defaultCommandShortcuts[commandId]
}),
[commandId],
),
)
return useAtomValue(useMemo(() => __commandShortcutAtom(commandId), [commandId]))
}
export const getCommandShortcut = (commandId: BindingCommandId) => {
return jotaiStore.get(__commandShortcutAtom(commandId))
}
export const useSetCustomCommandShortcut = () => {
@@ -15,7 +15,7 @@ import { COMMAND_ID } from "~/modules/command/commands/id"
import { useRunCommandFn } from "~/modules/command/hooks/use-command"
import { useCommandBinding, useCommandShortcuts } from "~/modules/command/hooks/use-command-binding"
const useRegisterGlobalHotkeys = () => {
const useRegisterAppGlobalHotkeys = () => {
const notInFloatingLayerScope = useGlobalFocusableScopeSelector(
FocusablePresets.isNotFloatingLayerScope,
)
@@ -38,14 +38,9 @@ const useRegisterGlobalHotkeys = () => {
commandId: COMMAND_ID.global.quickSearch,
when: notInFloatingLayerScope,
})
useCommandBinding({
commandId: COMMAND_ID.global.toggleAIChat,
when: notInFloatingLayerScope,
})
}
export const GlobalHotkeysProvider = () => {
useRegisterGlobalHotkeys()
useRegisterAppGlobalHotkeys()
useEventListener("keydown", (e) => {
if (e.key === "Tab") {
nextFrame(() => {
@@ -0,0 +1,18 @@
import { useGlobalFocusableScopeSelector } from "@follow/components/common/Focusable/hooks.js"
import { FocusablePresets } from "~/components/common/Focusable"
import { COMMAND_ID } from "~/modules/command/commands/id"
import { useCommandBinding } from "~/modules/command/hooks/use-command-binding"
export const MainViewHotkeysProvider = () => {
const notInFloatingLayerScope = useGlobalFocusableScopeSelector(
FocusablePresets.isNotFloatingLayerScope,
)
useCommandBinding({
commandId: COMMAND_ID.global.toggleAIChat,
when: notInFloatingLayerScope,
})
return null
}