Files
supabase/apps/studio/components/interfaces/Explorer/ExplorerChatTab.tsx
Alaister YoungandAlaister Young d8568215ad [FE-5109] (part 4) refactor(studio): migrate database, editor, storage, and functions interfaces off next/router (#54056)
Part 4 of a 6-PR stack migrating all `next/router` and
`next/compat/router` imports in `apps/studio` onto TanStack Router hooks
and removing the router shims.

| Part | PR | Scope |
| --- | --- | --- |
| 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`,
`data/prefetchers/**` |
| 2 | #54054 | `components/ui/**` |
| 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` |
| **4** | **#54056 ← this PR** | **Database, Branching, Compute, Edge
Functions, Explorer, SQL Editor, Storage, Table Editor** |
| 5 | #54057 | Remaining `components/interfaces/**` and
`QueryInsightsTable` router events |
| 6 | #54058 | Remove `next/router` & `next/compat/router` shims,
`_router-events.ts`, Vite aliases, and test mocks |

Migrates the database, branching, compute, functions, explorer, SQL
editor, storage, and table editor interfaces off `next/router` and
`next/compat/router`.

**Changed:**

- Migrated the sole `next/compat/router` consumer
(`components/interfaces/Database/Replication/Destinations.tsx`) to
`useNavigate`
- Replaced `useRouter` across `BranchManagement`, `Branching`,
`Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`,
`Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`,
`SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`,
`VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and
`TableGridEditor` with TanStack Router hooks (`useNavigate`,
`useLocation`, `useRoutePattern`) and `useParams` from `common`
- Updated unit and component tests across `Database/Replication`,
`Explorer`, `Functions`, and
`Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router

**Simplified:**

- `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read
search values from `useParams` instead of casting `location.search`
- `resolveSourceSwitch` returns `replace: boolean` instead of a
`push`/`replace` method name
- `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref
instead of navigating with `ref ?? ''`
- `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo`
helper for their navigation shortcuts
- Tests use the shared `createTestRouter` and `customRender` router, use
`toMatchObject` for search assertions, and drop duplicate assertions

Addresses
[FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks)

## To test

- [ ] SQL Editor: creating, renaming, moving, opening
templates/examples, and keyboard shortcuts navigate between snippets
cleanly
- [ ] Table Editor & Database: opening tables, schema table nodes,
triggers, functions, backups/PITR, and replication destination flows
navigate as expected
- [ ] Storage: switching between Files, Analytics, and Vector buckets
and deleting a bucket navigates cleanly
- [ ] Edge Functions & Explorer: opening function details/logs, explorer
chat/query tabs, and branch management actions navigate properly

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 14:44:59 +08:00

95 lines
3.5 KiB
TypeScript

import { useParams } from 'common'
import { Loader2Icon, MessageSquareIcon } from 'lucide-react'
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, useTabRouter, useTabsStateSnapshot } from '@/state/tabs'
export const ExplorerChatTab = () => {
const { id, ref } = useParams()
const router = useTabRouter()
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">
<Loader2Icon 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">
<MessageSquareIcon 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} />
)}
/>
)
}