Files
supabase/apps/studio/components/ui/Logs/LogsExplorerHeader.tsx
Alaister YoungandAlaister Young 0a4cec0464 [FE-5108] (part 1) refactor(studio): migrate core UI & prefetchers off next/link (#54304)
Part 1 of a 6-PR stack migrating all `next/link` imports in
`apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known
external URLs) and removing `compat/next/link.tsx`.

| Part | PR | Scope |
| --- | --- | --- |
| **1** | **#54304 ← this PR** |
**`data/prefetchers/PrefetchableLink.tsx` and `components/ui/**`** |
| 2 | #54305 | `components/layouts/**` and top-level shell components |
| 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor,
BranchManagement, Branching, Functions, Realtime |
| 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues,
Linter, Reports, QueryPerformance, QuerySources, ConfigDrift,
DiskManagement, Compute |
| 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth,
Support, Home, SignIn, ConnectSheet, etc.) |
| 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and
update basePath tests/docs |

Migrates `data/prefetchers/PrefetchableLink.tsx` and all shared
`components/ui/**` components off `next/link`.

**Added:**

- `apps/studio/components/ui/DynamicLink.tsx` (with unit tests) — shared
`forwardRef` link wrapper for dynamic URL strings that runs
`splitInternalUrl` and renders a plain `<a>` for external URLs (or
same-origin URLs outside the app's `basePath`) vs TanStack `<Link>` for
internal paths

**Changed:**

- Replaced `next/link` in
`apps/studio/data/prefetchers/PrefetchableLink.tsx` and all 30 importing
components under `apps/studio/components/ui/**` (`InlineLink`,
`CardButton`, `CommandItemLink`, `ObservabilityLink`, `ProductMenuItem`,
`ResourceItem`, `UpgradePlanButton`, `ToggleSpendCapButton`, banners, AI
assistant panel links, etc.)
- Used typed `<Link to="..." params={...} search={...} hash="..." />`
for known internal routes, `<DynamicLink href={...} />` for
dynamic/untyped URL strings (whether internal with query/hash or
external), and plain `<a>` for known external links
- Updated `apps/studio/routes/index.tsx` `validateSearch` return type to
`{ next?: string }` so `<Link to="/">` compiles without requiring
`search={{ next: undefined }}`
- Updated
`hooks/misc/__tests__/useTableEditorFiltersSort.navigation.test.tsx`,
`PolicyTableRow.utils.test.ts`, `SupportAssistantSuccessCard.test.tsx`,
and `useOrganizationRestrictions.test.tsx` to render inside a TanStack
Router context (`customRender` / `RouterContextProvider`)

Addresses
[FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link)

## To test

- [ ] Click internal and external `InlineLink`, `CardButton`,
`ProductMenuItem`, and `PrefetchableLink` targets and verify client-side
navigation vs external anchors
- [ ] Verify banner links (upgrade, resource exhaustion, queue
operations) preserve query params and hashes
- [ ] Confirm Studio typecheck, lint, and unit tests pass

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 16:23:48 +08:00

264 lines
8.6 KiB
TypeScript

import { useFlag, useParams } from 'common'
import {
BookOpenIcon,
CheckIcon,
ChevronsUpDownIcon,
CopyIcon,
ExternalLinkIcon,
ListIcon,
XIcon,
} from 'lucide-react'
import { useState } from 'react'
import { logConstants } from 'shared-data'
import {
Button,
Card,
cn,
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
copyToClipboard,
Popover,
PopoverContent,
PopoverTrigger,
SidePanel,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { DocsButton } from '../DocsButton'
import { LOGS_EXPLORER_DOCS_URL } from '@/components/interfaces/Settings/Logs/Logs.constants'
import {
otelFieldsFromKeys,
toOtelFieldSchemas,
} from '@/components/interfaces/Settings/Logs/Logs.fieldReference'
import { useOtelLogKeysQuery } from '@/data/logs/otel-log-keys-query'
import { DOCS_URL } from '@/lib/constants'
export interface LogsExplorerHeaderProps {
subtitle?: string
}
const LogsExplorerHeader = ({ subtitle }: LogsExplorerHeaderProps) => {
const [showReference, setShowReference] = useState(false)
const [open, setOpen] = useState(false)
const useOtel = useFlag('otelLegacyLogs')
const schemas = useOtel ? toOtelFieldSchemas(logConstants.schemas) : logConstants.schemas
const [selectedRef, setSelectedRef] = useState(schemas[0]?.reference)
const selectedSchema = schemas.find((s) => s.reference === selectedRef) ?? schemas[0]
const { ref: projectRef } = useParams()
const { data: discoveredKeys, isPending: isLoadingKeys } = useOtelLogKeysQuery(
{ projectRef, source: selectedRef },
{ enabled: useOtel && showReference }
)
return (
<div className="flex flex-col md:flex-row md:items-center gap-4 md:gap-8 transition-all pb-6 justify-between">
<div className="flex flex-col md:flex-row md:items-center gap-3">
<div className="flex flex-row items-center gap-3">
<div className="flex h-6 w-6 items-center justify-center rounded-sm border border-brand-600 bg-brand-300 text-primary">
<ListIcon size={14} strokeWidth={3} />
</div>
<h1>Logs Explorer</h1>
</div>
{subtitle && <span className="text-2xl text-foreground-light">{subtitle}</span>}
</div>
<div className="flex flex-row gap-2">
<DocsButton href={LOGS_EXPLORER_DOCS_URL} />
<SidePanel
size="large"
header={
<div className="flex flex-row justify-between items-center">
<h3>Field Reference</h3>
<Button
variant="text"
className="px-1"
onClick={() => setShowReference(false)}
icon={<XIcon size={18} strokeWidth={1.5} />}
/>
</div>
}
visible={showReference}
cancelText="Close"
onCancel={() => setShowReference(false)}
hideFooter
triggerElement={
<Button
onClick={() => setShowReference(true)}
icon={<BookOpenIcon strokeWidth={1.5} />}
>
<span>Field Reference</span>
</Button>
}
>
<SidePanel.Content>
<div className="pt-4 pb-2 space-y-1">
{useOtel ? (
<p className="text-sm">
The following table shows the fields available on each source. Nested fields live
in the <code className="text-xs">log_attributes</code> map and are read with{' '}
<code className="text-xs">log_attributes['key']</code> — no unnesting joins
needed.
</p>
) : (
<p className="text-sm">
The following table shows all the available paths that can be queried from each
respective source. Do note that to access nested keys, you would need to perform
the necessary{' '}
<a
href={`${DOCS_URL}/guides/platform/logs#unnesting-arrays`}
target="_blank"
rel="noreferrer"
className="text-primary"
>
unnesting joins
<ExternalLinkIcon
size={14}
className="ml-1 inline translate-y-[-2px]"
strokeWidth={1.5}
/>
</a>
</p>
)}
</div>
</SidePanel.Content>
<SidePanel.Separator />
<div className="px-4 pb-4 flex flex-col gap-4">
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger asChild>
<Button
role="combobox"
size={'small'}
aria-expanded={open}
className="w-full justify-between"
iconRight={<ChevronsUpDownIcon className="ml-2 h-4 w-4 shrink-0 opacity-50" />}
>
{selectedSchema?.name ?? 'Select source...'}
</Button>
</PopoverTrigger>
<PopoverContent className="p-0" sameWidthAsTrigger>
<Command>
<CommandInput placeholder="Search source..." />
<CommandList>
<CommandEmpty>No source found.</CommandEmpty>
<CommandGroup>
{schemas.map((schema) => (
<CommandItem
key={schema.reference}
value={schema.reference}
onSelect={() => {
setSelectedRef(schema.reference)
setOpen(false)
}}
>
<CheckIcon
className={cn(
'mr-2 h-4 w-4',
selectedSchema?.reference === schema.reference
? 'opacity-100'
: 'opacity-0'
)}
/>
{schema.name}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{useOtel && isLoadingKeys ? (
<p className="text-sm text-foreground-light py-2">Loading fields…</p>
) : (
<Card>
<Table>
<TableHeader>
<TableRow>
<TableHead className="text-xs p-2" key="path">
Path
</TableHead>
<TableHead key="type" className="text-xs p-2">
Type
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{(useOtel
? otelFieldsFromKeys(discoveredKeys ?? [])
: selectedSchema.fields
).map((field) => (
<Field key={field.path} field={field} />
))}
</TableBody>
</Table>
</Card>
)}
</div>
</SidePanel>
</div>
</div>
)
}
export default LogsExplorerHeader
const Field = ({
field,
}: {
field: {
path: string
type: string
}
}) => {
const [isCopied, setIsCopied] = useState(false)
return (
<TableRow>
<TableCell
className="font-mono text-xs p-2 cursor-pointer hover:text-foreground transition flex items-center space-x-2"
onClick={() =>
copyToClipboard(field.path, () => {
setIsCopied(true)
setTimeout(() => setIsCopied(false), 3000)
})
}
>
<span>{field.path}</span>
{isCopied ? (
<Tooltip>
<TooltipTrigger>
<CheckIcon size={14} strokeWidth={3} className="text-primary" />
</TooltipTrigger>
<TooltipContent side="bottom" className="font-sans">
Copied
</TooltipContent>
</Tooltip>
) : (
<Tooltip>
<TooltipTrigger>
<CopyIcon size={14} />
</TooltipTrigger>
<TooltipContent side="bottom" className="font-sans">
Copy value
</TooltipContent>
</Tooltip>
)}
</TableCell>
<TableCell className="font-mono text-xs p-2">{field.type}</TableCell>
</TableRow>
)
}