| 35 | interface ChatTopbarProps { |
| 36 | chatId?: string; |
| 37 | /** Lets the page drop its local messages after a server-side clear. */ |
| 38 | onHistoryCleared?: () => void; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Header for the chat column: the way back to your projects, the title, and |
| 43 | * the chat's own management. With the sidebar gone this bar is where rename, |
| 44 | * clear and delete live. |
| 45 | */ |
| 46 | export default function ChatTopbar({ |
| 47 | chatId, |
| 48 | onHistoryCleared, |
| 49 | }: ChatTopbarProps) { |
| 50 | const router = useRouter(); |
| 51 | const { chats, refetchChats } = useChatList(); |
| 52 | const [renaming, setRenaming] = useState(false); |
| 53 | const [draft, setDraft] = useState(''); |
| 54 | const [confirmDelete, setConfirmDelete] = useState(false); |
| 55 | |
| 56 | const title = chatId |
| 57 | ? chats.find((c) => c.id === chatId)?.title || 'Untitled' |
| 58 | : 'New Chat'; |
| 59 | |
| 60 | // The browser tab should say which project this is, not just "Codefox" — |
| 61 | // with several projects open, identical tabs are unfindable. |
| 62 | useEffect(() => { |
| 63 | document.title = `${title} — CodeFox`; |
| 64 | return () => { |
| 65 | document.title = 'CodeFox'; |
| 66 | }; |
| 67 | }, [title]); |
| 68 | |
| 69 | const [updateTitle] = useMutation(UPDATE_CHAT_TITLE, { |
| 70 | onCompleted: () => refetchChats(), |
| 71 | onError: () => toast.error('Could not rename the chat'), |
| 72 | }); |
| 73 | const [clearHistory] = useMutation(CLEAR_CHAT_HISTORY, { |
| 74 | onCompleted: () => { |
| 75 | onHistoryCleared?.(); |
| 76 | toast.success('History cleared'); |
| 77 | }, |
| 78 | onError: () => toast.error('Could not clear the history'), |
| 79 | }); |
| 80 | const [deleteChat] = useMutation(DELETE_CHAT, { |
| 81 | onCompleted: () => { |
| 82 | toast.success('Chat deleted'); |
| 83 | router.push('/'); |
| 84 | }, |
| 85 | onError: () => toast.error('Could not delete the chat'), |
| 86 | }); |
| 87 | |
| 88 | const commitRename = () => { |
| 89 | setRenaming(false); |
| 90 | const next = draft.trim(); |
| 91 | if (!next || next === title || !chatId) return; |
| 92 | updateTitle({ variables: { input: { chatId, title: next } } }); |
| 93 | }; |
| 94 | |