Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions apps/web/components/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ export function ChatSidebar({
const [confirmingDeleteId, setConfirmingDeleteId] = useState<string | null>(
null,
)
const [saveState, setSaveState] = useState<"idle" | "saving" | "saved">(
"idle",
)
const saveStateRef = useRef<"idle" | "saving" | "saved">("idle")
saveStateRef.current = saveState
const messagesContainerRef = useRef<HTMLDivElement>(null)
const isScrolledToBottomRef = useRef(true)
const userJustSentRef = useRef(false)
Expand Down Expand Up @@ -426,8 +431,48 @@ export function ChatSidebar({
setThreadId(null)
setFallbackChatId(newChatId)
setInput("")
setSaveState("idle")
}, [setThreadId, setMessages])

// Reset saveState to idle when a new assistant message arrives
useEffect(() => {
if (messages.at(-1)?.role === "assistant") {
setSaveState("idle")
}
}, [messages])

// Reset saveState when the active space changes so saved state doesn't carry over
// biome-ignore lint/correctness/useExhaustiveDependencies: chatProject triggers the reset but isn't used inside the effect body
useEffect(() => {
setSaveState("idle")
}, [chatProject])

const handleSaveToSpace = useCallback(async () => {
if (saveStateRef.current !== "idle" || messages.length === 0) return
setSaveState("saving")
try {
const response = await fetch(`${chatApiBase}/chat/save-as-document`, {
method: "POST",
credentials: "include",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
chatId: currentChatId,
projectId: chatProject,
messages,
}),
})
if (!response.ok) throw new Error("Save failed")
setSaveState("saved")
analytics.chatSavedToSpace({
chat_id: currentChatId,
project_id: chatProject,
})
} catch (error) {
console.error("Failed to save chat:", error)
setSaveState("idle")
}
}, [chatApiBase, messages, currentChatId, chatProject])

const fetchThreads = useCallback(async () => {
setIsLoadingThreads(true)
try {
Expand Down Expand Up @@ -905,6 +950,20 @@ export function ChatSidebar({

const chatToolbarActions = (
<div className="flex shrink-0 items-center gap-2">
{messages.length > 0 && !isAutoChatSpace && (
<button
type="button"
disabled={saveState === "saving" || isResponding}
onClick={handleSaveToSpace}
className="text-xs text-muted-foreground hover:text-foreground transition-colors disabled:opacity-50"
>
{saveState === "saving"
? "Saving..."
: saveState === "saved"
? "Saved to space"
: `Save to ${chatSpaceLabel}`}
</button>
)}
<button
type="button"
onClick={() => setIsHistoryOpen(true)}
Expand Down
3 changes: 3 additions & 0 deletions apps/web/lib/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ export const analytics = {
chatThreadDeleted: (props: { thread_id: string }) =>
safeCapture("chat_thread_deleted", props),

chatSavedToSpace: (props: { chat_id: string; project_id: string }) =>
safeCapture("chat_saved_to_space", props),

modelChanged: (props: { model: string }) =>
safeCapture("model_changed", props),

Expand Down
Loading