Files
supabase/apps/studio/data/projects/org-projects-infinite-query.ts
Joshen Lim e3fec137fe Joshenlim/fe 4466 audit logs update organization logs as well (#50935)
## Context

Follows up from [this
PR](https://github.com/supabase/supabase/pull/50799) - updates the Audit
Logs UI for organization audit logs. Just differs from Account audit
logs slightly with added "Actor" + "Target" column

Reuses the same UI components from account audit logs so quite a bit of
code clean up 🙂
<img width="1451" height="955" alt="image"
src="https://github.com/user-attachments/assets/a1002cee-917f-4d9a-b977-3fab8ecd2d13"
/>


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

* **New Features**
* Organization audit logs now use a sortable table with row selection
and a resizable details panel.
* Actor details show a member’s username when available, otherwise the
actor’s email; a dash appears when neither is available.
* Logs can be filtered by users and projects, with separate messages for
no logs and no matching results.
* A refresh control and loading indicators help show audit-log updates.
* **Bug Fixes**
* Organization project lists stop loading when pagination totals are
unavailable.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-09-29 17:41:54 +08:00

109 lines
3.6 KiB
TypeScript

import { InfiniteData, useInfiniteQuery, useQueryClient } from '@tanstack/react-query'
import { platformComponents as components } from 'api-types'
import { useCallback } from 'react'
import { INFINITE_PROJECTS_KEY_PREFIX, projectKeys } from './keys'
import { get, handleError } from '@/data/fetchers'
import { useProfile } from '@/lib/profile'
import type { ResponseError, UseCustomInfiniteQueryOptions } from '@/types'
// [Joshen] Try to keep this value a multiple of 6 (common denominator of 2 and 3) to fit the cards view
// So that the last row will always be a full row of cards while there's a next page
// API max rows is 100, I'm just choosing 96 here as the highest value thats a multiple of 6
const DEFAULT_LIMIT = 96
interface GetOrgProjectsInfiniteVariables {
slug?: string
limit?: number
sort?: 'name_asc' | 'name_desc' | 'created_asc' | 'created_desc'
search?: string
page?: number
statuses?: string[]
}
export type OrgProjectsResponse = components['schemas']['OrganizationProjectsResponse_Output']
export type OrgProject = OrgProjectsResponse['projects'][number]
export async function getOrganizationProjects(
{
slug,
limit = DEFAULT_LIMIT,
page = 0,
sort = 'name_asc',
search: _search = '',
statuses: _statuses = [],
}: GetOrgProjectsInfiniteVariables,
signal?: AbortSignal,
headers?: Record<string, string>
) {
if (!slug) throw new Error('Slug is required')
const offset = page * limit
const search = _search.length === 0 ? undefined : _search
const statuses = _statuses.length === 0 ? undefined : _statuses.join(',')
const { data, error } = await get('/platform/organizations/{slug}/projects', {
params: { path: { slug }, query: { limit, offset, sort, search, statuses } },
signal,
headers,
})
if (error) handleError(error)
return data
}
export type OrgProjectsInfiniteData = Awaited<ReturnType<typeof getOrganizationProjects>>
export type OrgProjectsInfiniteError = ResponseError
export const useOrgProjectsInfiniteQuery = <TData = OrgProjectsInfiniteData>(
{
slug,
limit = DEFAULT_LIMIT,
sort = 'name_asc',
search,
statuses = [],
}: GetOrgProjectsInfiniteVariables,
{
enabled = true,
...options
}: UseCustomInfiniteQueryOptions<
OrgProjectsInfiniteData,
OrgProjectsInfiniteError,
InfiniteData<TData>,
readonly unknown[],
number | undefined
> = {}
) => {
const { profile } = useProfile()
return useInfiniteQuery({
queryKey: projectKeys.infiniteListByOrg(slug, { limit, sort, search, statuses }),
queryFn: ({ signal, pageParam }) =>
getOrganizationProjects({ slug, limit, page: pageParam, sort, search, statuses }, signal),
enabled: enabled && profile !== undefined && typeof slug !== 'undefined',
staleTime: 30 * 60 * 1000, // 30 minutes
initialPageParam: 0,
getNextPageParam(lastPage, pages) {
const page = pages.length
const currentTotalCount = page * limit
const totalCount = lastPage.pagination?.count
if (totalCount === undefined || currentTotalCount >= totalCount) return undefined
return page
},
...options,
})
}
export const getComputeSize = (project: OrgProject) => {
const primaryDatabase = project.databases.find((db) => db.identifier === project.ref)
return primaryDatabase?.infra_compute_size
}
export const useInvalidateProjectsInfiniteQuery = () => {
const queryClient = useQueryClient()
const invalidateProjectsQuery = useCallback(() => {
return queryClient.invalidateQueries({ queryKey: [INFINITE_PROJECTS_KEY_PREFIX] })
}, [queryClient])
return { invalidateProjectsQuery }
}