The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
FE-1813: Audit Logs Improvements (#37946)
Jordi Enrice187e212c14c
diff --git a/apps/studio/components/interfaces/Account/AuditLogs.tsx b/apps/studio/components/interfaces/Account/AuditLogs.tsxindex eeef64ab8c9..9b774a17b9c 100644--- a/apps/studio/components/interfaces/Account/AuditLogs.tsx+++ b/apps/studio/components/interfaces/Account/AuditLogs.tsx@@ -6,16 +6,15 @@ import { LogDetailsPanel } from 'components/interfaces/AuditLogs'import Table from 'components/to-be-cleaned/Table'import AlertError from 'components/ui/AlertError'import { ButtonTooltip } from 'components/ui/ButtonTooltip'-import { DatePicker } from 'components/ui/DatePicker'import { FilterPopover } from 'components/ui/FilterPopover'import ShimmeringLoader from 'components/ui/ShimmeringLoader'import type { AuditLog } from 'data/organizations/organization-audit-logs-query'import { useOrganizationsQuery } from 'data/organizations/organizations-query'import { useProfileAuditLogsQuery } from 'data/profile/profile-audit-logs-query'import { useProjectsQuery } from 'data/projects/projects-query'-import { Alert, Button } from 'ui'+import { Button } from 'ui'import { TimestampInfo } from 'ui-patterns'-import { formatSelectedDateRange } from '../Organization/AuditLogs/AuditLogs.utils'+import { LogsDatePicker } from '../Settings/Logs/Logs.DatePickers'const AuditLogs = () => {const currentTime = dayjs().utc().set('millisecond', 0)@@ -33,12 +32,16 @@ const AuditLogs = () => {const { data: projects } = useProjectsQuery()const { data: organizations } = useOrganizationsQuery()const { data, error, isLoading, isSuccess, isError, isRefetching, refetch } =- useProfileAuditLogsQuery({- iso_timestamp_start: dateRange.from,- iso_timestamp_end: dateRange.to,- })+ useProfileAuditLogsQuery(+ {+ iso_timestamp_start: dateRange.from,+ iso_timestamp_end: dateRange.to,+ },+ {+ retry: false,+ }+ )- const retentionPeriod = data?.retention_period ?? 0const logs = data?.result ?? []const sortedLogs = logs?.sort((a, b) =>@@ -54,9 +57,6 @@ const AuditLogs = () => {}})- const minDate = dayjs().subtract(retentionPeriod, 'days')- const maxDate = dayjs()-// This feature depends on the subscription tier of the user. Free user can view logs up to 1 day// in the past. The API limits the logs to maximum of 1 day and 5 minutes so when the page is// viewed for more than 5 minutes, the call parameters needs to be updated. This also works with@@ -88,35 +88,38 @@ const AuditLogs = () => {activeOptions={filters.projects}onSaveFilters={(values) => setFilters({ ...filters, projects: values })}/>- <DatePicker- hideTime- hideClear- triggerButtonType="dashed"- triggerButtonTitle=""- from={dateRange.from}- to={dateRange.to}- minDate={minDate.toDate()}- maxDate={maxDate.toDate()}- onChange={(value) => {- if (value.from !== null && value.to !== null) {- const { from, to } = formatSelectedDateRange(value)- setDateRange({ from, to })- }- }}- renderFooter={() => {- return (- <Alert title="" variant="info" className="mx-3 pl-2 pr-2 pt-1 pb-2">- You have a log retention period of{' '}- <span className="text-brand">- {retentionPeriod} day- {retentionPeriod > 1 ? 's' : ''}- </span>- . You may only view logs from{' '}- {dayjs().subtract(retentionPeriod, 'days').format('DD MMM YYYY')} as the- earliest date.- </Alert>- )- }}+ <LogsDatePicker+ hideWarnings+ value={dateRange}+ onSubmit={(value) => setDateRange(value)}+ helpers={[+ {+ text: 'Last 1 hour',+ calcFrom: () => dayjs().subtract(1, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 3 hours',+ calcFrom: () => dayjs().subtract(3, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },++ {+ text: 'Last 6 hours',+ calcFrom: () => dayjs().subtract(6, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 12 hours',+ calcFrom: () => dayjs().subtract(12, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 24 hours',+ calcFrom: () => dayjs().subtract(1, 'day').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ ]}/>{isSuccess && (<>diff --git a/apps/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx b/apps/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsxindex 0895737379e..f14eb4f2030 100644--- a/apps/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx+++ b/apps/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx@@ -31,8 +31,7 @@ import {Button,WarningIcon,} from 'ui'-import { Admonition } from 'ui-patterns'-import { formatSelectedDateRange } from './AuditLogs.utils'+import { LogsDatePicker } from 'components/interfaces/Settings/Logs/Logs.DatePickers'// [Joshen considerations]// - Maybe fix the height of the table to the remaining height of the viewport, so that the search input is always visible@@ -69,14 +68,7 @@ const AuditLogs = () => {},{enabled: canReadAuditLogs,- retry(_failureCount, error) {- if (error.message.endsWith('upgrade to Team or Enterprise Plan to access audit logs.')) {- return false- }- return true- },- retryOnMount: false,- refetchOnWindowFocus: false,+ retry: false,})@@ -139,6 +131,77 @@ const AuditLogs = () => {<><ScaffoldContainerLegacy><div className="space-y-4 flex flex-col">+ <div className="flex items-center justify-between">+ <div className="flex items-center space-x-2">+ <p className="text-xs prose">Filter by</p>+ <FilterPopover+ name="Users"+ options={activeMembers}+ labelKey="username"+ valueKey="gotrue_id"+ activeOptions={filters.users}+ onSaveFilters={(values) => setFilters({ ...filters, users: values })}+ />+ <FilterPopover+ name="Projects"+ options={+ projects?.filter((p) => p.organization_id === currentOrganization?.id) ?? []+ }+ labelKey="name"+ valueKey="ref"+ activeOptions={filters.projects}+ onSaveFilters={(values) => setFilters({ ...filters, projects: values })}+ />+ <LogsDatePicker+ hideWarnings+ value={dateRange}+ onSubmit={(value) => setDateRange(value)}+ helpers={[+ {+ text: 'Last 1 hour',+ calcFrom: () => dayjs().subtract(1, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 3 hours',+ calcFrom: () => dayjs().subtract(3, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },++ {+ text: 'Last 6 hours',+ calcFrom: () => dayjs().subtract(6, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 12 hours',+ calcFrom: () => dayjs().subtract(12, 'hour').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ {+ text: 'Last 24 hours',+ calcFrom: () => dayjs().subtract(1, 'day').toISOString(),+ calcTo: () => dayjs().toISOString(),+ },+ ]}+ />+ {isSuccess && (+ <>+ <div className="h-[20px] border-r border-strong !ml-4 !mr-2" />+ <p className="prose text-xs">Viewing {sortedLogs.length} logs in total</p>+ </>+ )}+ </div>+ <Button+ type="default"+ disabled={isLoading || isRefetching}+ icon={<RefreshCw className={isRefetching ? 'animate-spin' : ''} />}+ onClick={() => refetch()}+ >+ {isRefetching ? 'Refreshing' : 'Refresh'}+ </Button>+ </div>+{isLoading && (<div className="space-y-2"><ShimmeringLoader />@@ -176,6 +239,15 @@ const AuditLogs = () => {</div></div></Alert_Shadcn_>+ ) : error.message.includes('range exceeded') ? (+ <Alert_Shadcn_ variant="destructive" title="Date range too large">+ <WarningIcon />+ <AlertTitle_Shadcn_>Date range too large</AlertTitle_Shadcn_>+ <AlertDescription_Shadcn_>+ The selected date range exceeds the maximum allowed period. Please select a+ smaller time range.+ </AlertDescription_Shadcn_>+ </Alert_Shadcn_>) : (<AlertError error={error} subject="Failed to retrieve audit logs" />)@@ -183,79 +255,6 @@ const AuditLogs = () => {{isSuccess && (<>- <div className="flex items-center justify-between">- <div className="flex items-center space-x-2">- <p className="text-xs prose">Filter by</p>- <FilterPopover- name="Users"- options={activeMembers}- labelKey="username"- valueKey="gotrue_id"- activeOptions={filters.users}- onSaveFilters={(values) => setFilters({ ...filters, users: values })}- />- <FilterPopover- name="Projects"- options={- projects?.filter((p) => p.organization_id === currentOrganization?.id) ?? []- }- labelKey="name"- valueKey="ref"- activeOptions={filters.projects}- onSaveFilters={(values) => setFilters({ ...filters, projects: values })}- />- <DatePicker- hideTime- hideClear- triggerButtonType="dashed"- triggerButtonTitle=""- from={dateRange.from}- to={dateRange.to}- minDate={minDate.toDate()}- maxDate={maxDate.toDate()}- onChange={(value) => {- if (value.from !== null && value.to !== null) {- const { from, to } = formatSelectedDateRange(value)- setDateRange({ from, to })- }- }}- renderFooter={() => {- return (- <Admonition- showIcon={false}- type="default"- className="w-auto mx-2 px-3 py-2"- >- <div className="text-xs text-foreground-light">- Your organization has a log retention period of{' '}- <span className="text-brand">- {retentionPeriod} day- {retentionPeriod > 1 ? 's' : ''}- </span>- . You may only view logs from{' '}- {dayjs().subtract(retentionPeriod, 'days').format('DD MMM YYYY')} as the- earliest date.- </div>- </Admonition>- )- }}- />- {isSuccess && (- <>- <div className="h-[20px] border-r border-strong !ml-4 !mr-2" />- <p className="prose text-xs">Viewing {sortedLogs.length} logs in total</p>- </>- )}- </div>- <Button- type="default"- disabled={isLoading || isRefetching}- icon={<RefreshCw className={isRefetching ? 'animate-spin' : ''} />}- onClick={() => refetch()}- >- {isRefetching ? 'Refreshing' : 'Refresh'}- </Button>- </div>{logs.length === 0 ? (<div className="bg-surface-100 border rounded p-4 flex items-center justify-between"><p className="prose text-sm">diff --git a/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx b/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsxindex b4a7010cce6..258a65f1dfb 100644--- a/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx+++ b/apps/studio/components/interfaces/Settings/Logs/Logs.DatePickers.tsx@@ -1,7 +1,7 @@/* eslint-disable react-hooks/exhaustive-deps */import dayjs from 'dayjs'-import { ChevronLeft, ChevronRight, Clock, XIcon } from 'lucide-react'-import { PropsWithChildren, useEffect, useState } from 'react'+import { ChevronLeft, ChevronRight, Clock, HistoryIcon, XIcon } from 'lucide-react'+import { PropsWithChildren, useEffect, useRef, useState } from 'react'import DatePicker from 'react-datepicker'import { Label } from '@ui/components/shadcn/ui/label'@@ -20,6 +20,7 @@ import {} from 'ui'import { LOGS_LARGE_DATE_RANGE_DAYS_THRESHOLD } from './Logs.constants'import type { DatetimeHelper } from './Logs.types'+import { ButtonTooltip } from 'components/ui/ButtonTooltip'export type DatePickerValue = {to: string@@ -34,6 +35,7 @@ interface Props {onSubmit: (value: DatePickerValue) => voidbuttonTriggerProps?: ButtonPropspopoverContentProps?: typeof PopoverContent_Shadcn_+ hideWarnings?: boolean}export const LogsDatePicker = ({@@ -42,9 +44,12 @@ export const LogsDatePicker = ({value,buttonTriggerProps,popoverContentProps,+ hideWarnings,}: PropsWithChildren<Props>) => {const [open, setOpen] = useState(false)+ const todayButtonRef = useRef<HTMLButtonElement>(null)+// Reset the state when the popover closesuseEffect(() => {if (!open) {@@ -315,8 +320,13 @@ export const LogsDatePicker = ({/></div><div className="flex-shrink">- <Button- icon={<XIcon size={14} />}+ <ButtonTooltip+ tooltip={{+ content: {+ text: 'Clear time range',+ },+ }}+ icon={<HistoryIcon size={14} />}type="text"size="tiny"className="px-1.5"@@ -324,13 +334,20 @@ export const LogsDatePicker = ({setStartTime({ HH: '00', mm: '00', ss: '00' })setEndTime({ HH: '00', mm: '00', ss: '00' })}}- ></Button>+ ></ButtonTooltip></div></div><div className="p-2 border-t"><DatePickerinlineselectsRange+ todayButton={+ <div className="sr-only">+ <Button type="text" size="tiny" ref={todayButtonRef}>+ Go to today+ </Button>+ </div>+ }onChange={(dates) => {handleDatePickerChange(dates)}}@@ -371,7 +388,7 @@ export const LogsDatePicker = ({)}/></div>- {isLargeRange && (+ {isLargeRange && !hideWarnings && (<div className="text-xs px-3 py-1.5 border-y bg-warning-300 text-warning-foreground border-warning-500 text-warning-600">Large ranges may result in memory errors for <br /> big projects.</div>@@ -393,6 +410,10 @@ export const LogsDatePicker = ({<Buttontype="default"onClick={() => {+ // [Jordi]: Workaround so that if the user is in a different month, the datepicker will move the view to the current month+ if (todayButtonRef.current) {+ todayButtonRef.current.click()+ }setStartDate(new Date())setEndDate(new Date())}}