Skip to content

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

Update search funcitonality for audit logs

Joshen Lim55e865157384
diff --git a/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx b/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx
index 74150eba91d..8c2801ab5b6 100644
--- a/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx
+++ b/studio/components/interfaces/Organization/AuditLogs/AuditLogs.tsx
@@ -4,6 +4,7 @@ import { useState } from 'react'
import { useParams } from 'common'
import Table from 'components/to-be-cleaned/Table'
+import AlertError from 'components/ui/AlertError'
import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import {
OrganizationAuditLog,
@@ -13,17 +14,22 @@ import { useOrganizationDetailQuery } from 'data/organizations/organization-deta
import { useOrganizationRolesQuery } from 'data/organizations/organization-roles-query'
import { useOrganizationsQuery } from 'data/organizations/organizations-query'
import { useProjectsQuery } from 'data/projects/projects-query'
-import { Button, IconSearch, IconUser, Input } from 'ui'
+import { Button, IconUser } from 'ui'
+import FilterPopover from './FilterPopover'
import LogDetailsPanel from './LogDetailsPanel'
-import AlertError from 'components/ui/AlertError'
// [Joshen considerations]
// - Maybe fix the height of the table to the remaining height of the viewport, so that the search input is always visible
+// - We'll need pagination as well if the audit logs get too large, but that needs to be implemented on the API side first if possible
const AuditLogs = () => {
const { slug } = useParams()
- const currentTime = dayjs()
+ const currentTime = dayjs().set('millisecond', 0)
const [selectedLog, setSelectedLog] = useState<OrganizationAuditLog>()
+ const [filters, setFilters] = useState<{ users: string[]; projects: string[] }>({
+ users: [], // gotrue_id
+ projects: [], // project_ref
+ })
const { data: projects } = useProjectsQuery()
const { data: organizations } = useOrganizationsQuery()
@@ -42,9 +48,22 @@ const AuditLogs = () => {
const members = detailData?.members ?? []
const roles = rolesData?.roles ?? []
- const sortedLogs = logs?.sort(
- (a, b) => Number(new Date(b.timestamp)) - Number(new Date(a.timestamp))
- )
+ const sortedLogs = logs
+ ?.sort((a, b) => Number(new Date(b.timestamp)) - Number(new Date(a.timestamp)))
+ ?.filter((log) => {
+ if (filters.users.length > 0) {
+ return filters.users.includes(log.actor.id)
+ } else {
+ return log
+ }
+ })
+ ?.filter((log) => {
+ if (filters.projects.length > 0) {
+ return filters.projects.includes(log.permission_group.project_ref || '')
+ } else {
+ return log
+ }
+ })
return (
<>
@@ -69,14 +88,26 @@ const AuditLogs = () => {
</div>
) : (
<>
- <div className="flex items-center">
- <Input
- size="tiny"
- className="w-80"
- icon={<IconSearch size={14} strokeWidth={1.5} />}
- placeholder="Search audit logs"
+ <div className="flex items-center space-x-2">
+ <p className="text-xs prose">Filter by</p>
+ <FilterPopover
+ name="Users"
+ options={members ?? []}
+ labelKey="username"
+ valueKey="gotrue_id"
+ activeOptions={filters.users}
+ onSaveFilters={(values) => setFilters({ ...filters, users: values })}
+ />
+ <FilterPopover
+ name="Projects"
+ options={projects ?? []}
+ labelKey="name"
+ valueKey="ref"
+ activeOptions={filters.projects}
+ onSaveFilters={(values) => setFilters({ ...filters, projects: values })}
/>
</div>
+
<Table
head={[
<Table.th key="user">User</Table.th>,
@@ -144,11 +175,21 @@ const AuditLogs = () => {
</div>
</Table.td>
<Table.td>
- <p className="text-scale-1100">
+ <p className="text-scale-1100 truncate">
+ {project?.name
+ ? 'Project: '
+ : organization?.name
+ ? 'Organization: '
+ : null}
{project?.name ?? organization?.name ?? 'Entity no longer exists'}
</p>
- <p className="text-scale-1000 text-xs mt-0.5">
- {log.permission_group.org_slug ?? log.permission_group.project_ref}
+ <p className="text-scale-1000 text-xs mt-0.5 truncate">
+ {log.permission_group.project_ref
+ ? 'Ref: '
+ : log.permission_group.org_slug
+ ? 'Slug: '
+ : null}
+ {log.permission_group.project_ref ?? log.permission_group.org_slug}
</p>
</Table.td>
<Table.td>
diff --git a/studio/components/interfaces/Organization/AuditLogs/FilterPopover.tsx b/studio/components/interfaces/Organization/AuditLogs/FilterPopover.tsx
new file mode 100644
index 00000000000..c38c96736e3
--- /dev/null
+++ b/studio/components/interfaces/Organization/AuditLogs/FilterPopover.tsx
@@ -0,0 +1,100 @@
+import { useState } from 'react'
+import { Button, Checkbox, Popover } from 'ui'
+
+interface FilterPopoverProps {
+ options: any[]
+ activeOptions: any[]
+ valueKey: string
+ labelKey: string
+ name: string
+ onSaveFilters: (options: string[]) => void
+}
+
+// [Joshen] Form + Checkbox.Group doesn't seem to work properly RE form state
+// hence why the manual implementation here as a workaround
+
+const FilterPopover = ({
+ options = [],
+ activeOptions = [],
+ valueKey,
+ labelKey,
+ name,
+ onSaveFilters,
+}: FilterPopoverProps) => {
+ const [open, setOpen] = useState(false)
+ const [selectedOptions, setSelectedOptions] = useState<string[]>([])
+
+ const formattedOptions = activeOptions.map((option) => {
+ const base = options.find((x) => x[valueKey] === option)
+ return base[labelKey]
+ })
+
+ return (
+ <Popover
+ side="bottom"
+ align="start"
+ open={open}
+ onOpenChange={() => setOpen(!open)}
+ header={<div className="prose text-xs">Select {name.toLowerCase()}</div>}
+ overlay={
+ <>
+ <div className="space-y-4 px-3 py-3 min-w-[170px]">
+ <Checkbox.Group
+ id="projects"
+ onChange={(event) => {
+ const value = event.target.value
+ if (selectedOptions.includes(value)) {
+ setSelectedOptions(selectedOptions.filter((x) => x !== value))
+ } else {
+ setSelectedOptions(selectedOptions.concat(value))
+ }
+ }}
+ options={options.map((option) => {
+ return {
+ value: option[valueKey],
+ label: option[labelKey],
+ defaultChecked: activeOptions.includes(option[valueKey]),
+ }
+ })}
+ ></Checkbox.Group>
+ </div>
+ <div className="flex items-center justify-end gap-2 border-t border-scale-400 bg-scale-200 py-2 px-3 dark:border-scale-500 dark:bg-scale-400 ">
+ <Button
+ size="tiny"
+ type="default"
+ onClick={() => {
+ onSaveFilters([])
+ setOpen(false)
+ }}
+ >
+ Clear
+ </Button>
+ <Button
+ type="primary"
+ onClick={() => {
+ onSaveFilters(selectedOptions)
+ setOpen(false)
+ }}
+ >
+ Save
+ </Button>
+ </div>
+ </>
+ }
+ >
+ <Button type={activeOptions.length > 0 ? 'default' : 'dashed'} onClick={() => setOpen(false)}>
+ <span>{name}</span>
+ {activeOptions.length > 0 && <span className="mr-1">:</span>}
+ {activeOptions.length >= 3 ? (
+ <span>
+ {formattedOptions[0]} and {activeOptions.length - 1} others
+ </span>
+ ) : activeOptions.length > 0 ? (
+ <span>{formattedOptions.join(', ')}</span>
+ ) : null}
+ </Button>
+ </Popover>
+ )
+}
+
+export default FilterPopover
diff --git a/studio/data/organizations/organization-audit-logs-query.ts b/studio/data/organizations/organization-audit-logs-query.ts
index 49f1cc6e707..07680d1a20f 100644
--- a/studio/data/organizations/organization-audit-logs-query.ts
+++ b/studio/data/organizations/organization-audit-logs-query.ts
@@ -29,6 +29,53 @@ export type OrganizationAuditLog = {
timestamp: string
}
+const MOCK_LOGS: OrganizationAuditLog[] = [
+ {
+ action: { name: 'Test', metadata: [{ method: 'Test', status: 200 }] },
+ actor: {
+ id: '88687b48-6496-478b-a9e5-f0d626605a3b',
+ type: 'user',
+ metadata: [{ email: '[email protected]' }],
+ },
+ permission_group: { org_slug: 'sfplxoawkrhwbgwzikum', project_ref: null },
+ target: { description: 'Test description' },
+ timestamp: new Date('2023-06-05 08:30:23').toISOString(),
+ },
+ {
+ action: { name: 'Test', metadata: [{ method: 'Test', status: 200 }] },
+ actor: {
+ id: '88687b48-6496-478b-a9e5-f0d626605a3b',
+ type: 'user',
+ metadata: [{ email: '[email protected]' }],
+ },
+ permission_group: { org_slug: 'sfplxoawkrhwbgwzikum', project_ref: null },
+ target: { description: 'Test description' },
+ timestamp: new Date('2023-06-05 08:20:23').toISOString(),
+ },
+ {
+ action: { name: 'Test', metadata: [{ method: 'Test', status: 200 }] },
+ actor: {
+ id: '88687b48-6496-478b-a9e5-f0d626605a3b',
+ type: 'user',
+ metadata: [{ email: '[email protected]' }],
+ },
+ permission_group: { org_slug: 'sfplxoawkrhwbgwzikum', project_ref: null },
+ target: { description: 'Test description' },
+ timestamp: new Date('2023-06-05 08:10:23').toISOString(),
+ },
+ {
+ action: { name: 'Test', metadata: [{ method: 'Test', status: 200 }] },
+ actor: {
+ id: '88687b48-6496-478b-a9e5-f0d626605a3b',
+ type: 'user',
+ metadata: [{ email: '[email protected]' }],
+ },
+ permission_group: { org_slug: 'sfplxoawkrhwbgwzikum', project_ref: 'uipodoqangxyoawfsfer' },
+ target: { description: 'Test description' },
+ timestamp: new Date('2023-06-05 08:00:23').toISOString(),
+ },
+]
+
export type OrganizationAuditLogsVariables = {
slug?: string
iso_timestamp_start?: string
@@ -42,6 +89,8 @@ export async function getOrganizationAuditLogs(
) {
if (!slug) throw new Error('slug is required')
+ return MOCK_LOGS
+
const response = await get(
`${API_URL}/organizations/${slug}/audit?iso_timestamp_start=${iso_timestamp_start}&iso_timestamp_end=${iso_timestamp_end}`,
{ signal }