Files
supabase/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
Joshen Lim 5c6ef8ae3d Joshenlim/fe 4221 explorer tab behaviours to mimic sql editor (#49386)
## Context

Improves the tab behaviour for explorer to follow the SQL Editor
- Tabs now start as preview tabs and become permanent once you start
interacting with them
 - Query Tabs become permanent as soon as you start typing in the editor
- Chat tabs become permanent as soon as you start typing in the chat
input
- Notebook tabs become permanenet as soon as you make any changes to the
notebook
- Notebooks with unsaved changes will show the orange dot indicator
<img width="197" height="67" alt="image"
src="https://github.com/user-attachments/assets/3005c379-a49a-4cd8-8d90-65406b186141"
/>
- Closing a notebook tab with unsaved changes will show a confirmation
dialog
  - Except if the new notebook has no content (no changes)
<img width="375" height="218" alt="image"
src="https://github.com/user-attachments/assets/6ad10779-6415-4c55-bb4f-61d938e744c9"
/>


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Explorer chat, query, and notebook tabs now begin as previews and
become permanent when edited or saved.
* Added unsaved-change indicators and close confirmation for edited
notebook tabs.
  * Confirmed closure of edited notebooks now discards unsaved changes.
* **Bug Fixes**
  * Improved restoration and persistence of Explorer drafts.
  * Notebook saves now reflect the latest edits and tab state.
* Prevented stale save responses from incorrectly marking newer edits as
saved.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-24 14:35:22 +08:00

95 lines
3.3 KiB
TypeScript

import { useParams } from 'common'
import { Loader2, MessageSquare } from 'lucide-react'
import { useRouter } from 'next/router'
import { useEffect, useEffectEvent } from 'react'
import { Button } from 'ui'
import { ExplorerChatToolbar } from './ExplorerChatToolbar'
import { useCreateChat } from './hooks'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { AssistantChat } from '@/components/ui/AIAssistantPanel/AssistantChat'
import { useAiAssistantState, useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
import { createTabId, useTabsStateSnapshot } from '@/state/tabs'
export const ExplorerChatTab = () => {
const { id, ref } = useParams()
const router = useRouter()
const tabs = useTabsStateSnapshot()
const aiAssistant = useAiAssistantStateSnapshot()
const aiAssistantState = useAiAssistantState()
const { createChat, openChat } = useCreateChat()
const { activeSidebar } = useSidebarManagerSnapshot()
const chat = id ? aiAssistant.chats[id] : undefined
const chatInstance = id ? aiAssistant.chatInstances[id] : undefined
const tabId = id ? createTabId('chat', { id }) : undefined
const shortcutsEnabled = activeSidebar?.id !== SIDEBAR_KEYS.AI_ASSISTANT
const ensureChatInstance = useEffectEvent(() => {
if (!id || !chat) return
aiAssistantState.ensureChatInstance(id)
})
const removeDeletedChatTab = useEffectEvent(() => {
if (!tabId || !tabs.openTabs.includes(tabId)) return
tabs.handleTabClose({
id: tabId,
router,
editor: 'explorer',
onClearDashboardHistory: () => {},
})
})
useEffect(() => ensureChatInstance(), [id, chat])
useEffect(() => {
if (aiAssistant.isInitialized && id && !chat) removeDeletedChatTab()
}, [aiAssistant.isInitialized, id, chat])
if (!aiAssistant.isInitialized || (chat && !chatInstance)) {
return (
<div className="h-full bg-surface-100 flex items-center justify-center">
<Loader2 className="animate-spin text-foreground-muted" />
</div>
)
}
if (!id || !chat) {
return (
<div className="h-full bg-surface-100 flex flex-col items-center justify-center gap-3 px-6 text-center">
<MessageSquare className="text-foreground-muted" />
<div>
<h2 className="text-sm text-foreground">Chat not found</h2>
<p className="text-sm text-foreground-lighter">
This chat may have been deleted or is no longer available.
</p>
</div>
<Button variant="default" onClick={() => router.push(`/project/${ref}/explorer`)}>
Back to Explorer
</Button>
</div>
)
}
const handleBranchChat = (messageId: string) => {
const branchId = aiAssistantState.createBranch(id, messageId)
if (branchId) openChat(branchId)
}
return (
<AssistantChat
chatId={id}
shortcutsEnabled={shortcutsEnabled}
className="bg-surface-100"
onNewChat={() => createChat()}
onSelectChat={openChat}
onBranchChat={handleBranchChat}
onInputChange={() => tabs.makeTabPermanent(createTabId('chat', { id }))}
renderHeader={(headerProps) => (
<ExplorerChatToolbar {...headerProps} chatId={id} shortcutsEnabled={shortcutsEnabled} />
)}
/>
)
}