|
| 1 | +import { Suspense } from 'react' |
| 2 | +import { ChipLink } from '@sim/emcn' |
| 3 | +import type { Metadata } from 'next' |
| 4 | +import { redirect } from 'next/navigation' |
| 5 | +import { createSearchParamsCache } from 'nuqs/server' |
| 6 | +import { AccessRequestsLoading } from '@/components/access-requests/access-requests-loading' |
| 7 | +import { MyAccessRequests } from '@/components/access-requests/my-access-requests' |
| 8 | +import { OrganizationAccessRequests } from '@/components/access-requests/organization-access-requests' |
| 9 | +import { accessRequestEntrySearchParams } from '@/components/access-requests/search-params' |
| 10 | +import { EmptyState } from '@/components/empty-state/empty-state' |
| 11 | +import { getSession } from '@/lib/auth' |
| 12 | +import { WORKSPACES_PATH } from '@/lib/navigation/paths' |
| 13 | +import { buildAuthCrossLink } from '@/app/(auth)/auth-redirect' |
| 14 | + |
| 15 | +export const metadata: Metadata = { |
| 16 | + title: 'Access requests', |
| 17 | + robots: { index: false, follow: false }, |
| 18 | +} |
| 19 | + |
| 20 | +interface AccessRequestsPageProps { |
| 21 | + searchParams: Promise<Record<string, string | string[] | undefined>> |
| 22 | +} |
| 23 | + |
| 24 | +const entrySearchParams = createSearchParamsCache(accessRequestEntrySearchParams) |
| 25 | + |
| 26 | +/** Session-only entry so access requests remain reachable outside the organization Search rollout. */ |
| 27 | +export default async function AccessRequestsPage({ searchParams }: AccessRequestsPageProps) { |
| 28 | + const [rawParams, session] = await Promise.all([searchParams, getSession()]) |
| 29 | + const params = entrySearchParams.parse(rawParams) |
| 30 | + const query = new URLSearchParams() |
| 31 | + if (params.organizationId) query.set('organizationId', params.organizationId) |
| 32 | + if (params.view !== 'requests') query.set('view', params.view) |
| 33 | + if (params.requestId) query.set('requestId', params.requestId) |
| 34 | + if (!session?.user) { |
| 35 | + redirect( |
| 36 | + buildAuthCrossLink('/login', { |
| 37 | + callbackUrl: `/access-requests?${query}`, |
| 38 | + isInviteFlow: false, |
| 39 | + }) |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + if (!params.organizationId) { |
| 44 | + return ( |
| 45 | + <EmptyState |
| 46 | + title='Choose an organization' |
| 47 | + description='Open My access requests from your workspace menu.' |
| 48 | + action={<ChipLink href={WORKSPACES_PATH}>Your workspaces</ChipLink>} |
| 49 | + /> |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <Suspense fallback={<AccessRequestsLoading />}> |
| 55 | + {params.view === 'admin' ? ( |
| 56 | + <main className='flex-1 px-6 py-8'> |
| 57 | + <div className='mx-auto flex max-w-3xl flex-col gap-6'> |
| 58 | + <div className='flex items-center justify-between gap-4'> |
| 59 | + <h1 className='text-[var(--text-primary)] text-lg'>Access requests</h1> |
| 60 | + <ChipLink href={WORKSPACES_PATH}>Your workspaces</ChipLink> |
| 61 | + </div> |
| 62 | + <OrganizationAccessRequests organizationId={params.organizationId} standalone /> |
| 63 | + </div> |
| 64 | + </main> |
| 65 | + ) : ( |
| 66 | + <MyAccessRequests scope={{ kind: 'organization', organizationId: params.organizationId }} /> |
| 67 | + )} |
| 68 | + </Suspense> |
| 69 | + ) |
| 70 | +} |
0 commit comments