Skip to content

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

fixed datepicker and dropdowndown behaviour

TzeYiingb6f70e62e123
diff --git a/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx b/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx
index d4a46de6d55..11f387aa757 100644
--- a/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx
+++ b/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx
@@ -1,17 +1,35 @@
import { Button, Dropdown, IconClock } from '@supabase/ui'
import { DatePicker } from 'components/ui/DatePicker'
+import { useState, useEffect } from 'react'
-const DatePickers = ({
- to,
- from,
- // defaultValues,
- onChange,
-}: {
- to?: number
- from?: number
- defaultValues?: any
- onChange: ({}) => void
-}) => {
+type ToFrom = { to: string; from: string }
+interface Props {
+ to?: string
+ from?: string
+ onChange: ({ to, from }: ToFrom) => void
+}
+
+const HELPERS = [
+ { text: 'Last hour', value: '1_hour' },
+ { text: 'Last 3 hours', value: '3_hour' },
+ { text: 'Last day', value: '1_day' },
+]
+const DEFAULT_HELPER_VALUE = HELPERS[0].value
+
+const DatePickers: React.FC<Props> = ({ to, from, onChange }) => {
+ const [helperValue, setHelperValue] = useState<string>('')
+
+ const handleHelperChange = (newValue: string) => {
+ setHelperValue(newValue)
+ const toDate = new Date()
+ const fromDate = {
+ '1_hour': new Date(toDate.getTime() - 60 * 60 * 1000),
+ '3_hour': new Date(toDate.getTime() - 3 * 60 * 60 * 1000),
+ '1_day': new Date(toDate.getTime() - 24 * 60 * 60 * 1000),
+ }[newValue]
+ if (onChange) onChange({ to: toDate.toISOString(), from: fromDate?.toISOString() as string })
+ }
+ const selectedHelper = HELPERS.find((h) => h.value === (helperValue || DEFAULT_HELPER_VALUE))
return (
<div className="flex items-center">
<Dropdown
@@ -20,19 +38,38 @@ const DatePickers = ({
align="start"
overlay={
<>
- <Dropdown.RadioGroup onChange={(e) => console.log(e)} value={'1_hour'}>
- <Dropdown.Radio value="1_hour">1 hour</Dropdown.Radio>
- <Dropdown.Radio value="3_hour">Last 3 hour</Dropdown.Radio>
- <Dropdown.Radio value="1_day">Last day</Dropdown.Radio>
+ <Dropdown.RadioGroup
+ onChange={handleHelperChange}
+ value={helperValue || DEFAULT_HELPER_VALUE}
+ >
+ {HELPERS.map((helper) => (
+ <Dropdown.Radio key={helper.value} value={helper.value}>
+ {helper.text}
+ </Dropdown.Radio>
+ ))}
</Dropdown.RadioGroup>
</>
}
>
- <Button as="span" type="default" icon={<IconClock size={12} />}>
- Last hour
+ <Button
+ as="span"
+ type={helperValue ? 'alternative' : 'outline'}
+ icon={<IconClock size={12} />}
+ className="rounded-r-none"
+ >
+ {selectedHelper?.text}
</Button>
</Dropdown>
- <DatePicker onChange={onChange} to={to} from={from} />
+ <DatePicker
+ triggerButtonClassName="rounded-l-none"
+ triggerButtonType={helperValue ? 'outline' : 'alternative'}
+ onChange={(value: ToFrom) => {
+ setHelperValue('')
+ if (onChange) onChange(value)
+ }}
+ to={!helperValue ? to : undefined}
+ from={!helperValue ? from : undefined}
+ />
</div>
)
}
diff --git a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
index 63f25a966f2..02fa7c7161f 100644
--- a/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
+++ b/studio/components/interfaces/Settings/Logs/PreviewFilterPanel.tsx
@@ -53,6 +53,7 @@ const PreviewFilterPanel: FC<Props> = ({
onSearch = () => {},
defaultSearchValue = '',
defaultToValue = '',
+ defaultFromValue = '',
onCustomClick,
dispatchWhereFilters,
whereFilters: filters,
@@ -82,15 +83,6 @@ const PreviewFilterPanel: FC<Props> = ({
}
}, [defaultToValue])
- // const handleSearch = () => onSearch({ query: search, to: to.value })
-
- // const handleSearch = (value: string) =>
- // onSearch({
- // query: value,
- // to: to.value,
- // // , from: from.value
- // })
-
const RefreshButton = () => {
return !isCustomQuery ? (
<Button
@@ -151,14 +143,8 @@ const PreviewFilterPanel: FC<Props> = ({
onSearch({ query: search, to: '', from: '' })
}
- const showReset = to.value !== '' || from.value !== ''
-
- const showFromReset = to.value !== ''
-
const handleSearch = () => onSearch({ query: search, to: to.value, from: from.value })
- // console.log('this is what is going through', FILTER_OPTIONS[table])
-
return (
<div
className={'flex items-center justify-between w-full' + (condensedLayout ? ' px-5 pt-4' : '')}
@@ -206,6 +192,8 @@ const PreviewFilterPanel: FC<Props> = ({
setTo({ value: e.to, error: '' })
handleSearch()
}}
+ to={defaultToValue}
+ from={defaultFromValue}
/>
<div>
@@ -223,59 +211,6 @@ const PreviewFilterPanel: FC<Props> = ({
/>
))}
</div>
-
- {/* <div className="flex flex-row">
- <Popover
- side="bottom"
- align="end"
- portalled
- overlay={
- <>
- <Input
- label="From"
- labelOptional="UTC"
- value={from.value === '' ? defaultTimestamp : from.value}
- onChange={handleFromChange}
- error={from.error}
- className="w-72 p-3"
- />
- <Input
- label="To"
- labelOptional="UTC"
- value={to.value === '' ? defaultTimestamp : to.value}
- onChange={handleToChange}
- error={to.error}
- className="w-72 p-3"
- />
- <div className="flex flex-row justify-end pb-2 px-4">
- <Button key="set" size="tiny" title="Set" type="secondary" onClick={handleSearch}>
- Set
- </Button>
- </div>
- </>
- }
- >
- <Button
- as="span"
- size="tiny"
- className={showReset ? '!rounded-r-none' : ''}
- type={showReset ? 'outline' : 'text'}
- icon={<IconClock size="tiny" />}
- >
- {to.value || from.value ? 'Custom' : 'Now'}
- </Button>
- </Popover>
- {showReset && (
- <Button
- size="tiny"
- className={showReset ? '!rounded-l-none' : ''}
- icon={<IconX size="tiny" />}
- type="outline"
- title="Clear timestamp filter"
- onClick={handleReset}
- />
- )}
- </div> */}
<div className="flex items-center space-x-2">
<Button
type="default"
@@ -285,74 +220,6 @@ const PreviewFilterPanel: FC<Props> = ({
Event chart
</Button>
</div>
-
- {!isCustomQuery && (
- <>
- <div className="flex flex-row gap-2">
- {/* <Popover
- side="bottom"
- align="end"
- portalled
- overlay={
- <Input
- label="To"
- labelOptional="UTC"
- value={to.value === '' ? defaultTimestamp : to.value}
- onChange={handleFromChange}
- error={to.error}
- className="w-72 p-3"
- actions={[
- <Button
- key="set"
- size="tiny"
- title="Set"
- type="secondary"
- onClick={() => handleSearch(localSearchValue)}
- >
- Set
- </Button>,
- ]}
- />
- }
- >
- <Button
- as="span"
- size="tiny"
- className={showFromReset ? '!rounded-r-none' : ''}
- type={showFromReset ? 'default' : 'secondary'}
- icon={<IconClock size="tiny" />}
- >
- {to.value ? 'Custom' : 'Now'}
- </Button>
- </Popover> */}
-
- {/* Clear the filters could be here */}
- </div>
- {/* {showFromReset && ( */}
- {/* <Button
- size="tiny"
- className={showFromReset ? '!rounded-l-none' : ''}
- type="warning"
- title="Clear timestamp filter"
- onClick={handleFromReset}
- >
- Clear filter
- </Button> */}
- {/* )} */}
- {/* {!isCustomQuery && (
- <>
- <div className="flex items-center space-x-2">
- <p className="text-xs">Show event chart</p>
- <Toggle size="tiny" checked={isShowingEventChart} onChange={onToggleEventChart} />
- </div>
- <Button type="default" icon={<IconEye size={12} />}>
- Show Histogram
- </Button>
- </>
- )} */}
- {/* wrap with form so that if user presses enter, the search value will submit automatically */}
- </>
- )}
</div>
<Button type="secondary" onClick={onCustomClick} iconRight={<IconExternalLink size={10} />}>
Explore via query
diff --git a/studio/components/ui/DatePicker/DatePicker.tsx b/studio/components/ui/DatePicker/DatePicker.tsx
index d370be10f71..37c12abe9eb 100644
--- a/studio/components/ui/DatePicker/DatePicker.tsx
+++ b/studio/components/ui/DatePicker/DatePicker.tsx
@@ -24,6 +24,7 @@ import DateSplitInput from './DateSplitInput'
import DividerSlash from './DividerSlash'
import dayjs from 'dayjs'
+import { ButtonProps } from '@supabase/ui/dist/cjs/components/Button/Button'
var utc = require('dayjs/plugin/utc')
var timezone = require('dayjs/plugin/timezone') // dependent on utc plugin
@@ -32,13 +33,11 @@ dayjs.extend(utc)
dayjs.extend(timezone)
interface RootProps {
- onChange?: ({}) => void
- initialValues?: {
- to: number
- from: number
- }
- to?: number
- from?: number
+ onChange?: ({ to, from }: { to: string; from: string }) => void
+ to?: string
+ from?: string
+ triggerButtonType?: ButtonProps['type']
+ triggerButtonClassName?: string
}
const START_DATE_DEFAULT = new Date()
@@ -51,7 +50,13 @@ const TIME_NOW = format(new Date(), 'HH:mm:ss').split(':')
const START_TIME_DEFAULT = { HH: TIME_NOW[0], mm: TIME_NOW[1], ss: TIME_NOW[2] }
const END_TIME_DEFAULT = { HH: TIME_NOW[0], mm: TIME_NOW[1], ss: TIME_NOW[2] }
-function _DatePicker({ to, from, onChange, initialValues }: RootProps) {
+function _DatePicker({
+ to,
+ from,
+ onChange,
+ triggerButtonType = 'default',
+ triggerButtonClassName = '',
+}: RootProps) {
const [open, setOpen] = useState<boolean>(false)
const [showTime, setShowTime] = useState<boolean>(true)
@@ -65,10 +70,12 @@ function _DatePicker({ to, from, onChange, initialValues }: RootProps) {
const [startTime, setStartTime] = useState<any>(START_TIME_DEFAULT)
const [endTime, setEndTime] = useState<any>(END_TIME_DEFAULT)
- if (initialValues) {
- const from = dayjs(initialValues.from)
- const to = dayjs(initialValues.to)
- }
+ useEffect(() => {
+ if (!to || !from) {
+ setAppliedStartDate(null)
+ setAppliedEndDate(null)
+ }
+ }, [to, from])
function handleDatePickerChange(dates: [from: Date | null, to: Date | null]) {
const [from, to] = dates
@@ -94,16 +101,6 @@ function _DatePicker({ to, from, onChange, initialValues }: RootProps) {
}
}
- function unixPayload() {
- return {
- from: dayjs(from).valueOf(),
- to: dayjs(to).valueOf(),
- }
- }
-
- // console.log('unixPayload', unixPayload())
- // console.log('isoPayload', isoPayload())
-
if (onChange) onChange(isoPayload())
}
@@ -331,7 +328,12 @@ function _DatePicker({ to, from, onChange, initialValues }: RootProps) {
</>
}
>
- <Button type="default" as="span" icon={<IconCalendar />}>
+ <Button
+ type={triggerButtonType}
+ as="span"
+ icon={<IconCalendar />}
+ className={triggerButtonClassName}
+ >
{/* Custom */}
{appliedStartDate && appliedEndDate ? (
<>
diff --git a/studio/tests/pages/projects/PreviewFilterPanel.test.js b/studio/tests/pages/projects/PreviewFilterPanel.test.js
index 30aed15fe6c..b10846892d7 100644
--- a/studio/tests/pages/projects/PreviewFilterPanel.test.js
+++ b/studio/tests/pages/projects/PreviewFilterPanel.test.js
@@ -7,6 +7,7 @@ import Flag from 'components/ui/Flag/Flag'
Flag.mockImplementation(({ children }) => <>{children}</>)
jest.mock('hooks')
import { useFlag } from 'hooks'
+import { clickDropdown } from 'tests/helpers'
useFlag.mockReturnValue(true)
test.todo('templates')
@@ -58,6 +59,13 @@ test('Manual refresh', async () => {
userEvent.click(btn)
expect(mockFn).toBeCalled()
})
+test('Datepicker dropdown', async () => {
+ render(<PreviewFilterPanel />)
+ clickDropdown(await screen.findByText(/Last hour/))
+ userEvent.click(await screen.findByText(/Last 3 hours/))
+ await screen.findByText(/Last 3 hours/)
+ await expect(screen.findByText(/Last hour/)).rejects.toThrow()
+})
test.todo('timestamp to/from filter default value')
// , async () => {