|
| 1 | +import { clsx } from 'clsx'; |
| 2 | +import { flow } from 'fp-ts/lib/function'; |
| 3 | + |
| 4 | +import { useLastNonNullValue } from '@llm/commons-front'; |
| 5 | +import { |
| 6 | + type SdkAppT, |
| 7 | + SdkSearchAppsInputV, |
| 8 | + useSdkForLoggedIn, |
| 9 | +} from '@llm/sdk'; |
| 10 | +import { LazyIcon } from '~/modules/shared'; |
| 11 | +import { useWorkspaceOrganizationOrThrow } from '~/modules/workspace'; |
| 12 | +import { |
| 13 | + CardSkeletonGrid, |
| 14 | + PaginatedList, |
| 15 | + SelectableBadges, |
| 16 | + SelectableBadgesSkeleton, |
| 17 | + useDebouncedPaginatedSearch, |
| 18 | +} from '~/ui'; |
| 19 | + |
| 20 | +import { AppCard } from '../grid/app-card'; |
| 21 | +import { AppsPlaceholder } from '../grid/apps-placeholder'; |
| 22 | + |
| 23 | +type Props = { |
| 24 | + title?: string; |
| 25 | + limit?: number; |
| 26 | + className?: string; |
| 27 | +}; |
| 28 | + |
| 29 | +export function PromotedAppsContainer({ title, limit = 3, className }: Props) { |
| 30 | + const { assignWorkspaceToFilters } = useWorkspaceOrganizationOrThrow(); |
| 31 | + const { sdks } = useSdkForLoggedIn(); |
| 32 | + |
| 33 | + const { loading, pagination, result, silentReload } = useDebouncedPaginatedSearch({ |
| 34 | + storeDataInUrl: false, |
| 35 | + schema: SdkSearchAppsInputV, |
| 36 | + fallbackSearchParams: { |
| 37 | + limit, |
| 38 | + }, |
| 39 | + fetchResultsTask: flow(assignWorkspaceToFilters, sdks.dashboard.apps.search), |
| 40 | + }); |
| 41 | + |
| 42 | + const gridClassName = clsx( |
| 43 | + 'gap-4 grid grid-cols-1', |
| 44 | + 'lg:grid-cols-2 3xl:grid-cols-3', |
| 45 | + ); |
| 46 | + |
| 47 | + const categoriesTree = (useLastNonNullValue(result?.aggs?.categories) || []).map(category => ({ |
| 48 | + ...category, |
| 49 | + icon: <LazyIcon name={category.icon as any} size={16} />, |
| 50 | + })); |
| 51 | + |
| 52 | + return ( |
| 53 | + <div className={className}> |
| 54 | + {title && ( |
| 55 | + <h2 className="mb-8 font-semibold text-xl"> |
| 56 | + {title} |
| 57 | + </h2> |
| 58 | + )} |
| 59 | + |
| 60 | + <div className="mb-6"> |
| 61 | + {loading || !categoriesTree |
| 62 | + ? ( |
| 63 | + <SelectableBadgesSkeleton /> |
| 64 | + ) |
| 65 | + : ( |
| 66 | + <SelectableBadges |
| 67 | + {...pagination.bind.path('categoriesIds', { input: val => val ?? [] })} |
| 68 | + items={categoriesTree} |
| 69 | + multiSelect |
| 70 | + visibilityLimit={5} |
| 71 | + /> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + |
| 75 | + <PaginatedList |
| 76 | + result={result} |
| 77 | + loading={loading} |
| 78 | + pagination={pagination.bind.entire()} |
| 79 | + withEmptyPlaceholder={false} |
| 80 | + loadingFallback={( |
| 81 | + <CardSkeletonGrid className={gridClassName} count={limit} /> |
| 82 | + )} |
| 83 | + > |
| 84 | + {({ items, total }) => { |
| 85 | + if (!total) { |
| 86 | + return <AppsPlaceholder />; |
| 87 | + } |
| 88 | + |
| 89 | + return ( |
| 90 | + <div className={gridClassName}> |
| 91 | + {(items as SdkAppT[]).map(item => ( |
| 92 | + <AppCard |
| 93 | + key={item.id} |
| 94 | + app={item} |
| 95 | + onAfterArchive={silentReload} |
| 96 | + onAfterUnarchive={silentReload} |
| 97 | + onAfterToggleFavorite={silentReload} |
| 98 | + /> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + ); |
| 102 | + }} |
| 103 | + </PaginatedList> |
| 104 | + </div> |
| 105 | + ); |
| 106 | +} |
0 commit comments