Files
supabase/apps/studio/components/ui/DataTable/LiveButton.tsx
Joshen Lim a1df468edd Add keyboard shortcut to live mode (#48280)
## Context

As per PR title - there's already a keyboard shortcut mapping for the
live mode toggle that was originally present in `UnifiedLogs`, so this
reuses that.

Opting for a more explicit tooltip copy as well as "Live" doesn't really
explain what it does
<img width="257" height="82" alt="image"
src="https://github.com/user-attachments/assets/2159eef3-6291-4fdc-93ca-da706c8688f0"
/>
<img width="195" height="101" alt="image"
src="https://github.com/user-attachments/assets/0d5d211c-af66-406d-9cff-7de7b15f0892"
/>


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

## Summary by CodeRabbit

* **New Features**
* Added keyboard shortcut support for toggling live updates in database
connections.
  * Added shortcut guidance to the Live/Pause control.
* Resuming live updates now refreshes activity immediately and updates
the displayed timestamp.

* **Bug Fixes**
* Improved live-refresh controls and messaging to clearly reflect the
refresh cadence.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-24 15:18:21 +08:00

79 lines
2.2 KiB
TypeScript

import type { FetchPreviousPageOptions } from '@tanstack/react-query'
import { CirclePause, CirclePlay } from 'lucide-react'
import { useQueryStates } from 'nuqs'
import { useEffect } from 'react'
import { Button } from 'ui'
import { useDataTable } from './providers/DataTableProvider'
import { ShortcutTooltip } from '@/components/ui/ShortcutTooltip'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
import { useShortcut } from '@/state/shortcuts/useShortcut'
const REFRESH_INTERVAL = 10_000
interface LiveButtonProps {
searchParamsParser: any
fetchPreviousPage?: (options?: FetchPreviousPageOptions | undefined) => Promise<unknown>
}
export function LiveButton({ fetchPreviousPage, searchParamsParser }: LiveButtonProps) {
const [{ live, date, sort }, setSearch] = useQueryStates(searchParamsParser)
const { table } = useDataTable()
useShortcut(SHORTCUT_IDS.DATA_TABLE_TOGGLE_LIVE, handleClick, { registerInCommandMenu: false })
useEffect(() => {
let timeoutId: NodeJS.Timeout
async function fetchData() {
if (live) {
await fetchPreviousPage?.()
timeoutId = setTimeout(fetchData, REFRESH_INTERVAL)
} else {
clearTimeout(timeoutId)
}
}
fetchData()
return () => {
clearTimeout(timeoutId)
}
}, [live, fetchPreviousPage])
// REMINDER: make sure to reset live when date is set
// TODO: test properly
useEffect(() => {
if ((date || sort) && live) {
setSearch((prev) => ({ ...prev, live: null }))
}
}, [date, sort])
function handleClick() {
setSearch((prev) => ({
...prev,
live: !prev.live,
date: null,
sort: null,
}))
table.getColumn('date')?.setFilterValue(undefined)
table.resetSorting()
}
return (
<ShortcutTooltip
shortcutId={SHORTCUT_IDS.DATA_TABLE_TOGGLE_LIVE}
label={live ? 'Pause live mode' : 'Start live mode'}
side="bottom"
>
<Button
onClick={handleClick}
variant={live ? 'primary' : 'default'}
size="tiny"
icon={live ? <CirclePause className="h-4 w-4" /> : <CirclePlay className="h-4 w-4" />}
>
Live
</Button>
</ShortcutTooltip>
)
}