Files
supabase/apps/studio/components/ui/DataTable/DataTableSheetRowAction.tsx
T
7867f22522 feat(studio): link log users and show Auth activity timelines (#50837)
Add a User tab that resolves the signed-in user from the selected log or
a related request step. It shows the shared Auth profile, offers
filtering from the User ID row, and explains anonymous, external, or
deleted users.

When unified logs are enabled, the Auth user's Logs tab shows the user's
10 most recent logs in `EmbeddedLogsTable`, with the Logs page's columns
and row styling. A row opens that log on the Logs page, filtered to the
user, and View more in Logs opens the rest. Other configurations retain
the existing Logs view. Shared user presentation is isolated in #50841;
this PR contains the log/Auth integration and resolves missing-user
queries to null.

The list uses the #50953 helpers instead of its own:
`createUnifiedLogsSearch` builds the query search, `buildUnifiedLogsUrl`
builds the links (it now takes a `logId` to open a log), and the query
asks for only the 10 rows it shows via `pageSize`. At the panel's
default width the table is wider than the panel, so Event message needs
a horizontal scroll; the empty state renders outside the table so it
stays in view.

Issue:
[DEBUG-222](https://linear.app/supabase/issue/DEBUG-222/build-correlated-log-and-user-connections-in-studio)

### How to test

1. With the Unified Logs preview and `otelUnifiedLogs` enabled, open a
signed-in user's log and select User. Check the profile, copy the User
ID, and use Add filter to filter logs to that user.
2. Use Open in Authentication, then open the user's Logs tab. Select a
row and confirm it opens the matching log; check that View more in Logs
preserves the user filter.
3. Open an anonymous request and confirm the explanatory state. For
correlated, external, and deleted-user cases, run the fixtures: `pnpm
--filter studio exec vitest run
components/interfaces/UnifiedLogs/components/LogUser.platform.test.tsx`.
4. In the log's User tab, focus User ID and press Enter, Arrow Down,
Enter. Confirm it copies the ID without changing the selected log. Arrow
Down should navigate logs again after the menu closes.

### Validation

- Latest head: `8e4a9993b0db52ec5e611e55233b5ed77f67e358`, based on
`master`, with conflicts resolved and independent review complete.
- Local baseline and shuffled verification passed 324 tests across 26
files with retries disabled (shuffle seed `5083710`). An independent
affected-suite run passed 29 tests with retries disabled and shuffle
seed `5083730`.
- Live QA found and reproduced a keyboard collision: field-menu Arrow
Down also selected the next log. The shared menu now keeps keyboard
events inside the menu. The regression fails when the guard is removed
and passes after restoration.
- On the verified latest-head preview, keyboard User ID copy, mouse
copy, Add filter, Escape, navigation after closing the menu, and
adjacent Auth email copying passed. No browser console errors were
observed; browser network telemetry was unavailable.
- Full signed-in activity, matching log/profile, Authentication
navigation, View more filtering, anonymous state and deleted-user
handling passed on `311206f9ef`. The subsequent change only affects menu
keyboard propagation, which was revalidated on the latest head.
- The disposable staging user was deleted and absence verified.
Temporary key files were removed and the clipboard cleared.
- Current CI: all E2E suites, unit tests, formatting, lint, Docker and
deployments passed; typecheck is still running. Local full typecheck
exhausted Node's heap, so no local full pass is claimed.

### Stack

All prerequisite log-decoupling and request-correlation PRs, including
#50841, are merged into `master`. This is the final code PR in the
stack.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jordi Enric <jordi.err@gmail.com>
2026-10-29 16:15:21 +01:00

238 lines
7.3 KiB
TypeScript

import { Table } from '@tanstack/react-table'
import { endOfDay, endOfHour, startOfDay, startOfHour } from 'date-fns'
import {
CalendarClockIcon,
CalendarDaysIcon,
CalendarSearchIcon,
ChevronLeftIcon,
ChevronRightIcon,
CopyIcon,
EqualIcon,
FilterIcon,
} from 'lucide-react'
import { ComponentPropsWithRef, useEffect, useState } from 'react'
import {
cn,
copyToClipboard,
DropdownMenu,
DropdownMenuContent,
DropdownMenuGroup,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from 'ui'
import {
isLogsFilterColumnValue,
type LogsColumnFilterValue,
} from '@/components/interfaces/UnifiedLogs/UnifiedLogs.filters'
import { DataTableFilterField } from '@/components/ui/DataTable/DataTable.types'
interface DataTableSheetRowActionProps<
TData,
TFields extends DataTableFilterField<TData>,
> extends ComponentPropsWithRef<typeof DropdownMenuTrigger> {
fieldValue?: TFields['value']
filterFields: TFields[]
value: string | number
table?: Table<TData>
label?: string
onAddFilter?: () => void
}
export function DataTableSheetRowAction<TData, TFields extends DataTableFilterField<TData>>({
fieldValue,
filterFields,
value,
children,
className,
table,
label,
onAddFilter,
onKeyDown,
...props
}: DataTableSheetRowActionProps<TData, TFields>) {
const [open, setOpen] = useState(false)
/**
* [Joshen] This imo is just a temporary solution and needs to be addressed at the
* UI component level RE how we want to handle DropdownContent when scrolling, as its
* not specific to unified logs.
*
* DropdownMenuContent here exceeds the scrolling parent as its portalled. Opting to
* close the dropdown menu here when scrolling as a workaround.
*/
useEffect(() => {
if (!open) return
const onScroll = () => setOpen(false)
document.addEventListener('scroll', onScroll, true)
return () => document.removeEventListener('scroll', onScroll, true)
}, [open])
const field = !!fieldValue ? filterFields.find((f) => f.value === fieldValue) : undefined
const column =
!!fieldValue && !!field
? table?.getAllColumns().find((c) => c.id === fieldValue.toString())
: undefined
function renderOptions() {
if (!field) return null
switch (field.type) {
case 'checkbox':
return (
<DropdownMenuItem
onClick={() => {
// Equality filters use the wrapped { operator, values } shape so the
// row action stays compatible with the FilterBar (which writes `=` and `<>`).
const current = column?.getFilterValue()
const existing: LogsColumnFilterValue = isLogsFilterColumnValue(current)
? current
: { operator: '=', values: [] }
const next: LogsColumnFilterValue = existing.values.includes(String(value))
? existing
: { operator: existing.operator, values: [...existing.values, String(value)] }
column?.setFilterValue(next)
}}
className="flex items-center gap-2"
>
<FilterIcon size={12} />
Add filter
</DropdownMenuItem>
)
case 'input':
return (
<DropdownMenuItem
onClick={() =>
column?.setFilterValue({
operator: field.value === 'event_message' ? '~~*' : '=',
values: [String(value)],
} satisfies LogsColumnFilterValue)
}
className="flex items-center gap-2"
>
<FilterIcon size={12} />
Add filter
</DropdownMenuItem>
)
case 'slider':
return (
<DropdownMenuGroup>
<DropdownMenuItem
onClick={() => column?.setFilterValue([0, value])}
className="flex items-center gap-2"
>
{/* FIXME: change icon as it is not clear */}
<ChevronLeftIcon size={12} />
Less or equal than
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => column?.setFilterValue([value, 5000])}
className="flex items-center gap-2"
>
{/* FIXME: change icon as it is not clear */}
<ChevronRightIcon size={12} />
Greater or equal than
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => column?.setFilterValue([value])}
className="flex items-center gap-2"
>
<EqualIcon size={12} />
Equal to
</DropdownMenuItem>
</DropdownMenuGroup>
)
case 'timerange':
const date = new Date(value)
return (
<DropdownMenuGroup>
<DropdownMenuItem
onClick={() => column?.setFilterValue([date])}
className="flex items-center gap-2"
>
<CalendarSearchIcon size={12} />
Exact timestamp
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
const start = startOfHour(date)
const end = endOfHour(date)
column?.setFilterValue([start, end])
}}
className="flex items-center gap-2"
>
<CalendarClockIcon size={12} />
Same hour
</DropdownMenuItem>
<DropdownMenuItem
onClick={() => {
const start = startOfDay(date)
const end = endOfDay(date)
column?.setFilterValue([start, end])
}}
className="flex items-center gap-2"
>
<CalendarDaysIcon size={12} />
Same day
</DropdownMenuItem>
</DropdownMenuGroup>
)
default:
return null
}
}
return (
<DropdownMenu open={open} onOpenChange={setOpen}>
<DropdownMenuTrigger
asChild
className={cn('focus-ring relative', className)}
onKeyDown={(e) => {
if (e.key === 'ArrowDown') {
// REMINDER: default behavior is to open the dropdown menu
// But because we use it to navigate between rows, we need to prevent it
// and only use "Enter" to select the option
e.preventDefault()
}
onKeyDown?.(e)
}}
{...props}
>
{children}
</DropdownMenuTrigger>
<DropdownMenuContent
align="end"
alignOffset={12}
side="bottom"
className="w-56"
onKeyDown={(event) => event.stopPropagation()}
>
{!!field && !!column && (
<>
{renderOptions()}
<DropdownMenuSeparator />
</>
)}
{!column && !!onAddFilter && (
<>
<DropdownMenuItem onClick={onAddFilter} className="flex items-center gap-2">
<FilterIcon size={12} />
Add filter
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
)}
<DropdownMenuItem
onSelect={() => copyToClipboard(String(value))}
className="flex items-center gap-2"
>
<CopyIcon size={12} />
Copy {label}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}