Files
supabase/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.hooks.ts
Jordi Enric be030229bf feat(studio): add workers to unified logs FE-4281 (#49682)
## Problem

Unified Logs does not expose Workers logs, so users cannot search
Workers ingress, runtime, or build events alongside other services.

## Fix

Add a Workers log type that classifies all three Workers OTEL streams.
Gate the option and any persisted Workers filters with the existing
Workers feature flag.

## How to test

- Enable the Workers feature flag and open Unified Logs.
- Select Workers from the Log Type filter.
- Expected result: Unified Logs shows ingress, runtime, and build events
with the Workers icon.
- Disable the Workers feature flag and load a URL containing
`log_type:eq:workers`.
- Expected result: the Workers option and filter are removed, and
Workers logs are not queried.
- Run `./node_modules/.bin/vitest --run
components/interfaces/UnifiedLogs/UnifiedLogs.queries.test.ts
components/interfaces/UnifiedLogs/UnifiedLogs.utils.test.ts
data/workers/worker-logs-query.test.ts` from `apps/studio`.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
  * Added Workers as a selectable log type in Unified Logs.
* Unified Logs now combines worker ingress, guest, and API streams under
the Workers category.
  * Added a dedicated Workers icon and worker log filtering.

* **Improvements**
  * Worker filters and URL parameters respect feature availability.
* Worker details show relevant metadata while omitting unavailable HTTP
fields.
  * Improved handling of worker log levels, statuses, and raw data.
  * Added stronger validation for unified log data.

* **Tests**
* Added coverage for worker routing, filtering, feature visibility,
parsing, and metadata redaction.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-07 15:53:36 +02:00

67 lines
2.2 KiB
TypeScript

import type { ColumnFiltersState } from '@tanstack/react-table'
import { useDebounce } from 'common'
import { useQueryState } from 'nuqs'
import { useEffect, useMemo, useRef } from 'react'
import { SEARCH_PARAMS_PARSER } from './UnifiedLogs.constants'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { useShortcut } from '@/state/shortcuts/useShortcut'
export const useFilterSearchSync = ({
applyFilterSearch,
columnFilters,
enabled,
}: {
applyFilterSearch: () => void
columnFilters: ColumnFiltersState
enabled: boolean
}) => {
const debouncedApplyFilterSearch = useDebounce(applyFilterSearch, 250)
useEffect(() => {
if (!enabled) return
debouncedApplyFilterSearch()
return () => debouncedApplyFilterSearch.cancel()
}, [columnFilters, debouncedApplyFilterSearch, enabled])
}
export const useResetFocus = () => {
useShortcut(SHORTCUT_IDS.UNIFIED_LOGS_RESET_FOCUS, () => {
// FIXME: some dedicated div[tabindex="0"] do not auto-unblur (e.g. the DataTableFilterResetButton)
// REMINDER: we cannot just document.activeElement?.blur(); as the next tab will focus the next element in line,
// which is not what we want. We want to reset entirely.
document.body.setAttribute('tabindex', '0')
document.body.focus()
document.body.removeAttribute('tabindex')
})
}
export const useLiveMode = <TData extends { date: Date }>(data: TData[]) => {
const [live] = useQueryState('live', SEARCH_PARAMS_PARSER.live)
// REMINDER: used to capture the live mode on timestamp
const liveTimestamp = useRef<number | undefined>(live ? new Date().getTime() : undefined)
useEffect(() => {
if (live) liveTimestamp.current = new Date().getTime()
else liveTimestamp.current = undefined
}, [live])
const anchorRow = useMemo(() => {
if (!live) return undefined
const item = data.find((item) => {
// return first item that is there if not liveTimestamp
if (!liveTimestamp.current) return true
// return first item that is after the liveTimestamp
if (item.date.getTime() > liveTimestamp.current) return false
return true
// return first item if no liveTimestamp
})
return item
}, [live, data])
return { row: anchorRow, timestamp: liveTimestamp.current }
}