|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { type ReactNode, useState } from 'react' |
| 4 | +import { Chip, cn, toast } from '@sim/emcn' |
| 5 | +import { Clock } from '@sim/emcn/icons' |
| 6 | +import { inter } from '@/app/_styles/fonts/inter/inter' |
| 7 | +import { SourceCard } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-card' |
| 8 | +import { useClearSearchHistory, useSearchHistory } from '@/hooks/queries/search-history' |
| 9 | + |
| 10 | +interface SearchLandingHistoryProps { |
| 11 | + organizationId: string |
| 12 | + userId: string |
| 13 | + onSearch: (query: string) => void |
| 14 | + children: ReactNode |
| 15 | +} |
| 16 | + |
| 17 | +/** Private shortcuts under the composer; ordinary buttons retain native Tab/Enter navigation. */ |
| 18 | +export function SearchLandingHistory({ |
| 19 | + organizationId, |
| 20 | + userId, |
| 21 | + onSearch, |
| 22 | + children, |
| 23 | +}: SearchLandingHistoryProps) { |
| 24 | + const history = useSearchHistory(organizationId) |
| 25 | + const clear = useClearSearchHistory(organizationId, userId) |
| 26 | + const [selection, setSelection] = useState<'sources' | 'queries' | null>(null) |
| 27 | + const data = history.isError ? undefined : history.data |
| 28 | + const sources = data?.sources.slice(0, 5) ?? [] |
| 29 | + const queries = data?.queries.slice(0, 5) ?? [] |
| 30 | + const selected = selection ?? (sources.length > 0 ? 'sources' : 'queries') |
| 31 | + return ( |
| 32 | + <div className={cn('w-full min-w-0', inter.className)}> |
| 33 | + {children} |
| 34 | + {(sources.length > 0 || queries.length > 0) && ( |
| 35 | + <section aria-label='Recent activity' className='mt-6 px-2'> |
| 36 | + <div className='mb-2 flex flex-wrap items-center justify-between gap-2'> |
| 37 | + <div role='group' aria-label='History type' className='flex items-center gap-1'> |
| 38 | + <Chip |
| 39 | + active={selected === 'sources'} |
| 40 | + aria-pressed={selected === 'sources'} |
| 41 | + onClick={() => setSelection('sources')} |
| 42 | + > |
| 43 | + Recently viewed |
| 44 | + </Chip> |
| 45 | + <Chip |
| 46 | + active={selected === 'queries'} |
| 47 | + aria-pressed={selected === 'queries'} |
| 48 | + onClick={() => setSelection('queries')} |
| 49 | + > |
| 50 | + Recent searches |
| 51 | + </Chip> |
| 52 | + </div> |
| 53 | + <Chip |
| 54 | + disabled={clear.isPending} |
| 55 | + onClick={() => |
| 56 | + clear.mutate(undefined, { onError: (error) => toast.error(error.message) }) |
| 57 | + } |
| 58 | + > |
| 59 | + Clear history |
| 60 | + </Chip> |
| 61 | + </div> |
| 62 | + <div className='grid grid-cols-1'> |
| 63 | + <div |
| 64 | + className={cn( |
| 65 | + 'col-start-1 row-start-1 min-w-0', |
| 66 | + selected !== 'sources' && 'invisible' |
| 67 | + )} |
| 68 | + inert={selected !== 'sources'} |
| 69 | + aria-hidden={selected !== 'sources'} |
| 70 | + > |
| 71 | + {sources.length > 0 ? ( |
| 72 | + sources.map((source) => <SourceCard key={source.url} source={source} dense />) |
| 73 | + ) : ( |
| 74 | + <p className='px-2 py-2 text-[var(--text-tertiary)] text-small'> |
| 75 | + Sources you open will appear here. |
| 76 | + </p> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + <div |
| 80 | + className={cn( |
| 81 | + 'col-start-1 row-start-1 min-w-0', |
| 82 | + selected !== 'queries' && 'invisible' |
| 83 | + )} |
| 84 | + inert={selected !== 'queries'} |
| 85 | + aria-hidden={selected !== 'queries'} |
| 86 | + > |
| 87 | + {queries.length > 0 ? ( |
| 88 | + queries.map(({ query }) => ( |
| 89 | + <div key={query} className='py-1'> |
| 90 | + <Chip fullWidth leftIcon={Clock} onClick={() => onSearch(query)}> |
| 91 | + {query} |
| 92 | + </Chip> |
| 93 | + </div> |
| 94 | + )) |
| 95 | + ) : ( |
| 96 | + <p className='px-2 py-2 text-[var(--text-tertiary)] text-small'> |
| 97 | + Your recent searches will appear here. |
| 98 | + </p> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + </section> |
| 103 | + )} |
| 104 | + {history.isError && ( |
| 105 | + <div className='mt-6 flex items-center gap-2 px-4 text-[var(--text-tertiary)] text-small'> |
| 106 | + Recent activity couldn’t load. |
| 107 | + <Chip onClick={() => void history.refetch()} disabled={history.isFetching}> |
| 108 | + Try again |
| 109 | + </Chip> |
| 110 | + </div> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + ) |
| 114 | +} |
0 commit comments