Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

added logic implementation for timestamp from dropdown

TzeYiing84b2520f3e78
diff --git a/studio/components/interfaces/Settings/Logs/LogPanel.tsx b/studio/components/interfaces/Settings/Logs/LogPanel.tsx
index 0f3cecdebb6..7b367d5e44e 100644
--- a/studio/components/interfaces/Settings/Logs/LogPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/LogPanel.tsx
@@ -11,8 +11,7 @@ import {
IconSearch,
IconClock,
} from '@supabase/ui'
-import { LogTemplate } from '.'
-import { isNull } from 'lodash'
+import { LogSearchCallback, LogTemplate } from '.'
import dayjs from 'dayjs'
interface Props {
defaultSearchValue?: string
@@ -21,7 +20,7 @@ interface Props {
isCustomQuery: boolean
newCount: number
onRefresh?: () => void
- onSearch?: (query: string) => void
+ onSearch?: LogSearchCallback
onCustomClick?: () => void
onSelectTemplate: (template: LogTemplate) => void
}
@@ -62,6 +61,9 @@ const LogPanel: FC<Props> = ({
setFrom({ value, error: '' })
setDefaultTimestamp(value)
}
+
+ const handleSearch = () => onSearch({ query: search, from: from.value })
+
return (
<div className="bg-panel-header-light dark:bg-panel-header-dark">
<div className="px-2 py-1 flex items-center justify-between w-full">
@@ -127,12 +129,12 @@ const LogPanel: FC<Props> = ({
value={from.value === '' ? defaultTimestamp : from.value}
onChange={handleFromChange}
actions={[
- search && (
+ from.value && (
<IconX
key="reset-from"
size="tiny"
className="cursor-pointer mx-1"
- title="Reset input"
+ title="Reset"
onClick={handleFromReset}
/>
),
@@ -141,7 +143,7 @@ const LogPanel: FC<Props> = ({
size="tiny"
title="Set"
type="secondary"
- onClick={() => onSearch(search)}
+ onClick={handleSearch}
>
Set
</Button>,
@@ -154,6 +156,7 @@ const LogPanel: FC<Props> = ({
{from.value ? 'Custom' : 'Now'}
</Button>
</Dropdown>
+ <Button icon={<IconX />} title="Clear timestamp filter" />
</div>
{/* wrap with form so that if user presses enter, the search value will submit automatically */}
{!isCustomQuery && (
@@ -162,7 +165,7 @@ const LogPanel: FC<Props> = ({
onSubmit={(e) => {
// prevent redirection
e.preventDefault()
- onSearch(search)
+ handleSearch()
}}
>
<Input
@@ -180,13 +183,7 @@ const LogPanel: FC<Props> = ({
/>
),
- <Button
- key="go"
- size="tiny"
- title="Go"
- type="secondary"
- onClick={() => onSearch(search)}
- >
+ <Button key="go" size="tiny" title="Go" type="secondary" onClick={handleSearch}>
<IconSearch size={16} />
</Button>,
]}
diff --git a/studio/components/interfaces/Settings/Logs/Logs.types.ts b/studio/components/interfaces/Settings/Logs/Logs.types.ts
index 98b8a2a6f38..73491eab407 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.types.ts
+++ b/studio/components/interfaces/Settings/Logs/Logs.types.ts
@@ -1,7 +1,7 @@
interface Metadata {
[key: string]: string | number | Object | Object[]
}
-
+export type LogSearchCallback = (filters: { query: string; from: string }) => void
export interface LogData {
id: string
timestamp: number
diff --git a/studio/pages/project/[ref]/settings/logs/[type].tsx b/studio/pages/project/[ref]/settings/logs/[type].tsx
index 659825f605f..3ce0db3db4a 100644
--- a/studio/pages/project/[ref]/settings/logs/[type].tsx
+++ b/studio/pages/project/[ref]/settings/logs/[type].tsx
@@ -26,6 +26,7 @@ import {
LogTemplate,
TEMPLATES,
LogData,
+ LogSearchCallback,
} from 'components/interfaces/Settings/Logs'
import { uuidv4 } from 'lib/helpers'
import useSWRInfinite from 'swr/infinite'
@@ -171,6 +172,7 @@ export const LogPage: NextPage = () => {
where: isSelectQuery ? '' : template.searchString,
sql: isSelectQuery ? template.searchString : '',
search_query: '',
+ timestamp_start: ''
}))
setEditorId(uuidv4())
}
@@ -189,18 +191,26 @@ export const LogPage: NextPage = () => {
...router.query,
q: editorValue,
s: undefined,
+ ts: undefined
},
})
}
- const handleSearch = (v: string) => {
- setParams((prev) => ({ ...prev, search_query: v || '', where: '', sql: '' }))
+ const handleSearch: LogSearchCallback = ({ query, from }) => {
+ setParams((prev) => ({
+ ...prev,
+ search_query: query || '',
+ timestamp_start: from,
+ where: '',
+ sql: '',
+ }))
if (!logsQueryParamsSyncing) return
router.push({
pathname: router.pathname,
query: {
...router.query,
q: undefined,
- s: v || '',
+ s: query || '',
+ ts: from
},
})
setEditorValue('')
diff --git a/studio/tests/pages/projects/LogPanel.test.js b/studio/tests/pages/projects/LogPanel.test.js
index 95ed77e6ba4..e3150fc2eb5 100644
--- a/studio/tests/pages/projects/LogPanel.test.js
+++ b/studio/tests/pages/projects/LogPanel.test.js
@@ -62,22 +62,32 @@ test('reset search filter', async () => {
})
test('timestamp from filter', async () => {
- render(<LogPanel />)
+ const mockFn = jest.fn()
+ render(<LogPanel onSearch={mockFn} />)
const dropdown = await screen.findByText(/Now/)
// click the dropdown
clickDropdown(dropdown)
-
- await screen.findByLabelText('From')
+
+ // TODO: use screen.findByLabelText when https://github.com/supabase/ui/issues/310 is resolved
+ await screen.findByText('From')
// display iso timestamp
const year = new Date().getFullYear()
- await screen.findByDisplayValue(year)
+ const input = await screen.findByDisplayValue(RegExp(year))
+
+ // replace the input's value
+ userEvent.clear(input)
+ // get time 20 mins before
+ const newDate = new Date()
+ newDate.setMinutes(new Date().getMinutes() - 20)
+ userEvent.type(input, newDate.toISOString())
// input actions
- await screen.findByRole('button', { name: /Clear input/ })
+ await screen.findByTitle("Reset")
const set = await screen.findByRole('button', { name: 'Set' })
userEvent.click(set)
- await screen.findByRole('button', { name: /Custom/ })
+ expect(mockFn).toBeCalled()
+ await screen.findByText("Custom")
await screen.findByTitle('Clear timestamp filter')
})