mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
## Context Saves the last visited explorer tab via `useDashboardHistory`, such that landing back on `/explorer` will open the last visited page. Similar behaviour to Table Editor and SQL Editor Would also be useful when going between the SQL Editor and Explorer to bring snippets over <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Explorer now remembers and restores the last visited query, chat, or notebook tab. * Automatically returns to the Explorer home screen when a saved tab is unavailable. * Displays a loading state while the last visited tab is being restored. * **Bug Fixes** * Closing deleted chat tabs now clears their saved history. * **Tests** * Added coverage for Explorer tab restoration, loading states, and stale history cleanup. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
96 lines
3.5 KiB
TypeScript
96 lines
3.5 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 { useDashboardHistory } from '@/hooks/misc/useDashboardHistory'
|
|
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 { setLastVisitedExplorerTab } = useDashboardHistory()
|
|
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: () => setLastVisitedExplorerTab(undefined),
|
|
})
|
|
})
|
|
|
|
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 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}
|
|
composerContext={{ initialInput: aiAssistant.initialInput }}
|
|
onInputChange={() => tabs.makeTabPermanent(createTabId('chat', { id }))}
|
|
renderHeader={(headerProps) => (
|
|
<ExplorerChatToolbar {...headerProps} chatId={id} shortcutsEnabled={shortcutsEnabled} />
|
|
)}
|
|
/>
|
|
)
|
|
}
|