mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
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>
182 lines
6.6 KiB
TypeScript
182 lines
6.6 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
||
import { IS_PLATFORM, useFlag } from 'common'
|
||
import { useParams } from 'common/hooks'
|
||
import dayjs from 'dayjs'
|
||
import { CheckIcon, CopyIcon } from 'lucide-react'
|
||
import { useMemo, useState, type MouseEvent } from 'react'
|
||
import { cn, copyToClipboard, TableCell, TableRow } from 'ui'
|
||
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
|
||
import { TimestampInfo } from 'ui-patterns/TimestampInfo'
|
||
|
||
import { formatErrorRate } from './EdgeFunctionsListItem.utils'
|
||
import { useProjectApiUrl } from '@/data/config/project-endpoint-query'
|
||
import { useEdgeFunctionsLastHourStatsQuery } from '@/data/edge-functions/edge-functions-last-hour-stats-query'
|
||
import {
|
||
useEdgeFunctionsQuery,
|
||
type EdgeFunctionsResponse,
|
||
} from '@/data/edge-functions/edge-functions-query'
|
||
import { normalizeFunctionIds } from '@/data/edge-functions/keys'
|
||
import { createNavigationHandler } from '@/lib/navigation'
|
||
|
||
interface EdgeFunctionsListItemProps {
|
||
function: EdgeFunctionsResponse
|
||
}
|
||
|
||
export const EdgeFunctionsListItem = ({ function: item }: EdgeFunctionsListItemProps) => {
|
||
const navigate = useNavigate()
|
||
const { ref } = useParams()
|
||
const [isCopied, setIsCopied] = useState(false)
|
||
|
||
const showLastHourStats = useFlag('edgeFunctionsRequestMetrics')
|
||
|
||
const { data: endpoint, isPending: isEndpointPending } = useProjectApiUrl({ projectRef: ref })
|
||
const functionUrl = endpoint ? `${endpoint}/functions/v1/${item.slug}` : undefined
|
||
const isFunctionUrlLoading = !functionUrl && isEndpointPending
|
||
|
||
const handleNavigation = createNavigationHandler(
|
||
`/project/${ref}/functions/${item.slug}${IS_PLATFORM ? '' : `/code`}`,
|
||
navigate
|
||
)
|
||
|
||
const { data: functions } = useEdgeFunctionsQuery({ projectRef: ref })
|
||
const functionIds = useMemo(() => {
|
||
if (!showLastHourStats || !functions) return []
|
||
return normalizeFunctionIds(functions.map((item) => item.id))
|
||
}, [functions, showLastHourStats])
|
||
|
||
// [Joshen] We may be paginating the edge functions query in the future
|
||
// So this will eventually need to be a list of visibleFunctionIds instead + debounced
|
||
const {
|
||
data: lastHourStatsAll,
|
||
isPending: isStatsPending,
|
||
isError: isStatsError,
|
||
} = useEdgeFunctionsLastHourStatsQuery(
|
||
{ projectRef: ref, functionIds },
|
||
{ enabled: showLastHourStats }
|
||
)
|
||
const lastHourStats = lastHourStatsAll?.[item.id]
|
||
|
||
return (
|
||
<TableRow
|
||
key={item.id}
|
||
onClick={handleNavigation}
|
||
onAuxClick={handleNavigation}
|
||
onKeyDown={handleNavigation}
|
||
tabIndex={0}
|
||
className="cursor-pointer focus-inset"
|
||
>
|
||
<TableCell>
|
||
<p className="text-sm text-foreground whitespace-nowrap py-2">{item.name}</p>
|
||
</TableCell>
|
||
<TableCell>
|
||
<div className="text-xs text-foreground-light flex gap-2 items-center truncate">
|
||
{isFunctionUrlLoading && <ShimmeringLoader className="w-48 py-2" />}
|
||
{functionUrl && (
|
||
<>
|
||
<p title={functionUrl} className="font-mono truncate hidden md:inline max-w-120">
|
||
{functionUrl}
|
||
</p>
|
||
<button
|
||
type="button"
|
||
tabIndex={0}
|
||
aria-label="Copy function URL"
|
||
className="text-foreground-lighter hover:text-foreground transition"
|
||
onClick={(event: MouseEvent<HTMLButtonElement>) => {
|
||
function onCopy(value: string) {
|
||
setIsCopied(true)
|
||
copyToClipboard(value)
|
||
setTimeout(() => setIsCopied(false), 3000)
|
||
}
|
||
event.stopPropagation()
|
||
onCopy(functionUrl)
|
||
}}
|
||
>
|
||
{isCopied ? (
|
||
<div className="text-primary">
|
||
<CheckIcon size={14} strokeWidth={3} />
|
||
</div>
|
||
) : (
|
||
<div className="relative">
|
||
<div className="block">
|
||
<CopyIcon size={14} strokeWidth={1.5} />
|
||
</div>
|
||
</div>
|
||
)}
|
||
</button>
|
||
</>
|
||
)}
|
||
</div>
|
||
</TableCell>
|
||
<TableCell className="hidden 2xl:table-cell whitespace-nowrap">
|
||
{item.created_at ? (
|
||
<TimestampInfo
|
||
className="text-sm text-foreground-light whitespace-nowrap"
|
||
utcTimestamp={item.created_at}
|
||
label={dayjs(item.created_at).fromNow()}
|
||
/>
|
||
) : (
|
||
<span className="text-sm text-foreground-light">–</span>
|
||
)}
|
||
</TableCell>
|
||
<TableCell className="lg:table-cell">
|
||
{item.updated_at ? (
|
||
<TimestampInfo
|
||
className="text-sm text-foreground-light whitespace-nowrap"
|
||
utcTimestamp={item.updated_at}
|
||
label={dayjs(item.updated_at).fromNow()}
|
||
/>
|
||
) : (
|
||
<span className="text-sm text-foreground-light">–</span>
|
||
)}
|
||
</TableCell>
|
||
{showLastHourStats && (
|
||
<>
|
||
<TableCell className="lg:table-cell whitespace-nowrap">
|
||
{isStatsPending ? (
|
||
<ShimmeringLoader className="w-12" />
|
||
) : isStatsError ? (
|
||
<p className="text-foreground-lighter" title="Failed to load stats">
|
||
-
|
||
</p>
|
||
) : (
|
||
<p className="text-foreground-light">
|
||
{lastHourStats !== undefined ? lastHourStats.requestsCount.toLocaleString() : '-'}
|
||
</p>
|
||
)}
|
||
</TableCell>
|
||
<TableCell className="lg:table-cell whitespace-nowrap">
|
||
{isStatsPending ? (
|
||
<ShimmeringLoader className="w-12" />
|
||
) : isStatsError ? (
|
||
<p className="text-foreground-lighter" title="Failed to load stats">
|
||
-
|
||
</p>
|
||
) : lastHourStats !== undefined ? (
|
||
<span
|
||
className={cn(
|
||
'text-sm',
|
||
lastHourStats.errorRate >= 1
|
||
? 'text-destructive'
|
||
: lastHourStats.errorRate > 0.1
|
||
? 'text-warning'
|
||
: 'text-foreground-light'
|
||
)}
|
||
>
|
||
{formatErrorRate(lastHourStats.errorRate)}
|
||
</span>
|
||
) : (
|
||
<p className="text-foreground-lighter">-</p>
|
||
)}
|
||
</TableCell>
|
||
</>
|
||
)}
|
||
<TableCell className="hidden 2xl:table-cell">
|
||
<p className="text-foreground-light">{item.version}</p>
|
||
<button tabIndex={-1} className="sr-only">
|
||
Go to function details
|
||
</button>
|
||
</TableCell>
|
||
</TableRow>
|
||
)
|
||
}
|