import { useIntersectionObserver } from '@uidotdev/usehooks' import { useParams } from 'common' import { BuildingIcon, UserIcon } from 'lucide-react' import { ComponentProps, useEffect, useRef } from 'react' import { Button, SheetContent, SheetFooter, SheetHeader, SheetSection, SheetTitle } from 'ui' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { IdCopyChip } from './IdCopyChip' import { OAuthGrantAccess } from './OAuthGrantAccess' import { OAuthGrantAccordion } from './OAuthGrantAccordionItem' import { AlertError } from '@/components/ui/AlertError' import { OAuthApproval } from '@/data/oauth-apps/oauth-apps-approvals-query' import { useOAuthAppMemberGrantsQuery } from '@/data/oauth-apps/oauth-apps-member-grants-query' interface OAuthAppsGrantDetailsSheetProps extends ComponentProps { approval: OAuthApproval onRevoke?: () => void } export const OAuthAppsGrantDetailsSheet = ({ approval, onRevoke, ...props }: OAuthAppsGrantDetailsSheetProps) => { const titleRef = useRef(null) const { slug } = useParams() const { data, error, isPending, isError, isSuccess, hasNextPage, fetchNextPage, isFetchingNextPage, } = useOAuthAppMemberGrantsQuery({ slug, appId: approval.id }) const [sentinelRef, entry] = useIntersectionObserver({ root: null, threshold: 0, rootMargin: '0px', }) useEffect(() => { if (hasNextPage && entry?.isIntersecting && !isFetchingNextPage && !isError) { fetchNextPage() } }, [hasNextPage, entry?.isIntersecting, fetchNextPage, isFetchingNextPage, isError]) const grants = data?.pages.flatMap((page) => page.data) ?? [] const organizationGrants = grants.filter( (grant) => grant.attributes.kind === 'organization_bound' ) const memberGrants = grants.filter((grant) => grant.attributes.kind === 'member_bound') const isEmpty = grants.length === 0 let statusText = 'Grants loaded' if (isFetchingNextPage) statusText = 'Loading more grants...' else if (isPending) statusText = 'Loading grants...' else if (isError) statusText = '' return ( { event.preventDefault() titleRef.current?.focus() }} > Access for {approval.attributes.app.name}

Details

App ID

{statusText}

{isPending && } {isError && } {isSuccess && isEmpty && (

No grants have been authorized for this application.

)} {organizationGrants.map((grant) => ( } expanded /> ))} {memberGrants.length > 0 && ( <>

Member access

{memberGrants.map((grant) => ( } /> ))} )}

{isFetchingNextPage ? 'Loading...' : ''}

{onRevoke && ( )}
) }