feat: chat title as history menu trigger
This commit is contained in:
@@ -22,7 +22,6 @@ const ChatHeaderLayout = ({
|
||||
onNewChatClick: () => void
|
||||
currentTitle: string | undefined
|
||||
displayTitle: string | undefined
|
||||
onSaveTitle: (newTitle: string) => Promise<void>
|
||||
}) => ReactNode
|
||||
}) => {
|
||||
const currentTitle = useCurrentTitle()
|
||||
@@ -43,13 +42,6 @@ const ChatHeaderLayout = ({
|
||||
blockActions.clearBlocks({ keepSpecialTypes: true })
|
||||
}, [chatActions, blockActions])
|
||||
|
||||
const handleTitleSave = useCallback(
|
||||
async (newTitle: string) => {
|
||||
await chatActions.editChatTitle(newTitle)
|
||||
},
|
||||
[chatActions],
|
||||
)
|
||||
|
||||
const isFloating = useAIChatPanelStyle() === AIChatPanelStyle.Floating
|
||||
|
||||
const { isScrolledBeyondThreshold } = useAIRootState()
|
||||
@@ -73,11 +65,11 @@ const ChatHeaderLayout = ({
|
||||
)}
|
||||
|
||||
<div className="relative z-10 flex h-full items-center justify-between px-4">
|
||||
<div className="mr-2 flex min-w-0 flex-1 items-center">
|
||||
<EditableTitle
|
||||
title={displayTitle}
|
||||
onSave={handleTitleSave}
|
||||
placeholder={t("common.new_chat")}
|
||||
<div className="mr-2 flex min-w-0 items-center">
|
||||
<ChatHistoryDropdown
|
||||
triggerElement={
|
||||
<EditableTitle title={displayTitle} placeholder={t("common.new_chat")} />
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -87,7 +79,6 @@ const ChatHeaderLayout = ({
|
||||
onNewChatClick: handleNewChatClick,
|
||||
currentTitle,
|
||||
displayTitle,
|
||||
onSaveTitle: handleTitleSave,
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
@@ -108,8 +99,6 @@ export const ChatHeader = () => {
|
||||
<i className="i-mgc-add-cute-re text-text-secondary size-5" />
|
||||
</ActionButton>
|
||||
|
||||
<ChatHistoryDropdown />
|
||||
|
||||
<TaskReportDropdown />
|
||||
|
||||
<ChatMoreDropdown
|
||||
@@ -145,8 +134,6 @@ export const ChatPageHeader = () => {
|
||||
<i className="i-mgc-add-cute-re text-text-secondary size-5" />
|
||||
</ActionButton>
|
||||
|
||||
<ChatHistoryDropdown />
|
||||
|
||||
<TaskReportDropdown />
|
||||
|
||||
<div className="bg-border mx-2 h-5 w-px" />
|
||||
|
||||
+1
-1
@@ -89,7 +89,7 @@ export const ChatHistoryDropdown = ({
|
||||
<DropdownMenuTrigger asChild={asChild}>
|
||||
{triggerElement || defaultTrigger}
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="max-h-96 w-72 overflow-y-auto">
|
||||
<DropdownMenuContent align="start" className="max-h-96 w-72 overflow-y-auto">
|
||||
{loading ? (
|
||||
<div className="flex items-center justify-center py-8">
|
||||
<i className="i-mgc-loading-3-cute-re text-text-secondary size-5 animate-spin" />
|
||||
|
||||
+23
-123
@@ -1,139 +1,39 @@
|
||||
import { Input } from "@follow/components/ui/input/Input.js"
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { useCallback, useEffect, useState } from "react"
|
||||
import type { ButtonHTMLAttributes } from "react"
|
||||
|
||||
interface EditableTitleProps {
|
||||
interface EditableTitleProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type"> {
|
||||
title?: string
|
||||
onSave: (newTitle: string) => Promise<void>
|
||||
className?: string
|
||||
placeholder?: string
|
||||
maxLength?: number
|
||||
}
|
||||
|
||||
export const EditableTitle = ({
|
||||
export const EditableTitle = function EditableTitle({
|
||||
ref,
|
||||
title = "",
|
||||
onSave,
|
||||
className,
|
||||
placeholder = "Untitled Chat",
|
||||
maxLength = 100,
|
||||
}: EditableTitleProps) => {
|
||||
const [isEditing, setIsEditing] = useState(false)
|
||||
const [editValue, setEditValue] = useState("")
|
||||
const [isSaving, setIsSaving] = useState(false)
|
||||
const [originalTitle, setOriginalTitle] = useState("")
|
||||
const [saveSuccess, setSaveSuccess] = useState(false)
|
||||
|
||||
className,
|
||||
...buttonProps
|
||||
}: EditableTitleProps & { ref?: React.RefObject<HTMLButtonElement | null> }) {
|
||||
const displayTitle = title || placeholder
|
||||
|
||||
const handleStartEdit = useCallback(() => {
|
||||
setOriginalTitle(title)
|
||||
setEditValue(title)
|
||||
setIsEditing(true)
|
||||
}, [title])
|
||||
|
||||
const handleSave = useCallback(async () => {
|
||||
if (isSaving) return
|
||||
|
||||
const trimmedValue = editValue.trim()
|
||||
|
||||
// If no changes, just exit edit mode
|
||||
if (trimmedValue === originalTitle) {
|
||||
setIsEditing(false)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
setIsSaving(true)
|
||||
await onSave(trimmedValue)
|
||||
|
||||
// Show success feedback briefly
|
||||
setSaveSuccess(true)
|
||||
setTimeout(() => setSaveSuccess(false), 2000)
|
||||
|
||||
setIsEditing(false)
|
||||
} catch (error) {
|
||||
console.error("Failed to save title:", error)
|
||||
// Keep in edit mode on error to allow retry
|
||||
} finally {
|
||||
setIsSaving(false)
|
||||
}
|
||||
}, [editValue, originalTitle, onSave, isSaving])
|
||||
|
||||
const handleCancel = useCallback(() => {
|
||||
setEditValue(originalTitle)
|
||||
setIsEditing(false)
|
||||
setIsSaving(false)
|
||||
}, [originalTitle])
|
||||
|
||||
const handleKeyDown = useCallback(
|
||||
(event: React.KeyboardEvent) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault()
|
||||
handleSave()
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault()
|
||||
handleCancel()
|
||||
}
|
||||
},
|
||||
[handleSave, handleCancel],
|
||||
)
|
||||
|
||||
const handleBlur = useCallback(() => {
|
||||
if (!isSaving) {
|
||||
handleSave()
|
||||
}
|
||||
}, [handleSave, isSaving])
|
||||
|
||||
// Auto-focus input when entering edit mode
|
||||
useEffect(() => {
|
||||
if (isEditing) {
|
||||
const input = document.activeElement as HTMLInputElement
|
||||
if (input?.select) {
|
||||
input.select()
|
||||
}
|
||||
}
|
||||
}, [isEditing])
|
||||
|
||||
if (isEditing) {
|
||||
return (
|
||||
<div className="relative min-w-0 flex-1">
|
||||
<Input
|
||||
ref={(ref) => ref?.focus()}
|
||||
value={editValue}
|
||||
onChange={(e) => setEditValue(e.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
onBlur={handleBlur}
|
||||
maxLength={maxLength}
|
||||
disabled={isSaving}
|
||||
className={cn(
|
||||
"text-text border-accent/50 focus:border-accent bg-transparent font-bold",
|
||||
"h-auto p-2 text-base leading-none",
|
||||
isSaving && "cursor-not-allowed opacity-60",
|
||||
className,
|
||||
)}
|
||||
placeholder={placeholder}
|
||||
/>
|
||||
{isSaving && (
|
||||
<div className="animate-in fade-in zoom-in-75 absolute right-2 top-1/2 -translate-y-1/2 duration-200">
|
||||
<i className="i-mgc-loading-3-cute-re text-text-secondary size-4 animate-spin" />
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const { ["aria-label"]: ariaLabelProp, ...restButtonProps } = buttonProps
|
||||
const ariaLabel = ariaLabelProp ?? displayTitle
|
||||
|
||||
return (
|
||||
<div
|
||||
className={cn("group flex min-w-0 flex-1 cursor-pointer items-center gap-2", className)}
|
||||
onClick={handleStartEdit}
|
||||
<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,
|
||||
)}
|
||||
>
|
||||
<h1 className="text-text truncate font-bold transition-opacity group-hover:opacity-80">
|
||||
<h1 className="text-text truncate font-bold">
|
||||
<span className="animate-mask-left-to-right [--animation-duration:1s]">{displayTitle}</span>
|
||||
</h1>
|
||||
{saveSuccess && (
|
||||
<i className="i-mgc-check-cute-re text-green animate-in fade-in zoom-in-50 size-4 transition-all duration-300" />
|
||||
)}
|
||||
<i className="i-mgc-edit-cute-re text-text-secondary group-hover:animate-in group-hover:fade-in group-hover:zoom-in-75 size-4 opacity-0 transition-all duration-150 hover:opacity-100 group-hover:opacity-60" />
|
||||
</div>
|
||||
<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>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import { ActionButton } from "@follow/components/ui/button/index.js"
|
||||
import { memo } from "react"
|
||||
|
||||
import { EditableTitle } from "~/modules/ai-chat/components/layouts/EditableTitle"
|
||||
|
||||
interface AITaskModalHeaderProps {
|
||||
title: string
|
||||
error?: string
|
||||
placeholder?: string
|
||||
maxLength?: number
|
||||
onClose: () => void
|
||||
onSaveTitle: (newTitle: string) => Promise<void> | void
|
||||
}
|
||||
|
||||
export const AITaskModalHeader = memo(function AITaskModalHeader({
|
||||
title,
|
||||
error,
|
||||
placeholder = "AI Task",
|
||||
maxLength = 50,
|
||||
onClose,
|
||||
onSaveTitle,
|
||||
}: AITaskModalHeaderProps) {
|
||||
return (
|
||||
<div className="-mt-4 ml-6 mr-2 flex min-w-0 flex-1 items-start gap-2">
|
||||
<EditableTitle
|
||||
title={title}
|
||||
placeholder={placeholder}
|
||||
maxLength={maxLength}
|
||||
onSave={async (newTitle) => {
|
||||
await onSaveTitle(newTitle.trim() || placeholder)
|
||||
}}
|
||||
className="py-3"
|
||||
/>
|
||||
<ActionButton tooltip="Close" onClick={onClose}>
|
||||
<i className="i-mgc-close-cute-re text-text-secondary size-5" />
|
||||
</ActionButton>
|
||||
{error && <div className="text-red absolute left-6 top-10 w-[320px] text-xs">{error}</div>}
|
||||
</div>
|
||||
)
|
||||
})
|
||||
Reference in New Issue
Block a user