import { keepPreviousData } from '@tanstack/react-query' import { useDebounce, useIntersectionObserver } from '@uidotdev/usehooks' import { useEffect, useMemo, useState } from 'react' import { Command, CommandGroup, CommandInput, CommandItem, CommandList } from 'ui' import type { CustomOptionProps } from 'ui-patterns/FilterBar' import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader' import { useOrgProjectsInfiniteQuery } from '@/data/projects/org-projects-infinite-query' import { useProjectsInfiniteQuery } from '@/data/projects/projects-infinite-query' type AuditLogsProject = { ref: string; name: string } interface AuditLogsProjectOptionProps extends CustomOptionProps { // Scopes the list to an organization's projects, otherwise lists all of the user's projects slug?: string onSelectProject: (project: AuditLogsProject) => void } // Select-only project list for the FilterBar. The stored value is the project ref, while the // chip shows the name through the property's `formatValue`. export const AuditLogsProjectOption = ({ slug, onChange, onSelectProject, }: AuditLogsProjectOptionProps) => { const [search, setSearch] = useState('') const debouncedSearch = useDebounce(search, 500) const [sentinelRef, entry] = useIntersectionObserver({ threshold: 0, rootMargin: '0px' }) const isOrgScoped = !!slug const variables = { search: search.length === 0 ? search : debouncedSearch } const accountQuery = useProjectsInfiniteQuery(variables, { placeholderData: keepPreviousData, enabled: !isOrgScoped, }) const orgQuery = useOrgProjectsInfiniteQuery( { slug, ...variables }, { placeholderData: keepPreviousData, enabled: isOrgScoped } ) const { error, isLoading, isError, isSuccess, isFetching, isFetchingNextPage, hasNextPage, fetchNextPage, } = isOrgScoped ? orgQuery : accountQuery const accountProjects = useMemo( () => accountQuery.data?.pages.flatMap((page) => page.projects) ?? [], [accountQuery.data?.pages] ) const orgProjects = useMemo( () => orgQuery.data?.pages.flatMap((page) => page.projects) ?? [], [orgQuery.data?.pages] ) const projects: AuditLogsProject[] = isOrgScoped ? orgProjects : accountProjects useEffect(() => { if (entry?.isIntersecting && hasNextPage && !isFetching && !isFetchingNextPage) { fetchNextPage() } }, [entry?.isIntersecting, hasNextPage, isFetching, isFetchingNextPage, fetchNextPage]) const handleSelect = (project: AuditLogsProject) => { onSelectProject(project) onChange(project.ref) } return ( setSearch('')} className="text-sm" // FilterBar keeps focus on the chip's input, so move it here for cmdk's arrow/enter keys autoFocus /> {isLoading && ( )} {isError && ( Failed to retrieve projects: {error.message} )} {isSuccess && projects.length === 0 && ( {search.length > 0 ? 'No projects found based on your search' : 'No projects found'} )} {isSuccess && projects.map((project) => ( handleSelect(project)} > {project.name} ))} {hasNextPage && ( )} ) } // FilterBar invokes `component(props)` as a plain function, so this factory returns that function // with `onSelectProject` (and optionally `slug`) bound, keeping the component itself defined at // module level. export const createAuditLogsProjectOption = ( onSelectProject: AuditLogsProjectOptionProps['onSelectProject'], slug?: string ) => { const renderProjectOption = (props: CustomOptionProps) => ( ) return renderProjectOption }
Failed to retrieve projects: {error.message}
{search.length > 0 ? 'No projects found based on your search' : 'No projects found'}