|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useParams, useSearchParams } from 'next/navigation'; |
| 4 | +import { useAtom, useAtomValue, useSetAtom } from 'jotai'; |
| 5 | +import { useState } from 'react'; |
| 6 | + |
| 7 | +import snakeCase from 'lodash/snakeCase'; |
| 8 | +import compact from 'lodash/compact'; |
| 9 | +import get from 'lodash/get'; |
| 10 | + |
| 11 | +import { useDataTableColumns } from '@/ui/segments/data-table/elements/use-data-table-columns'; |
| 12 | +import { useQueryExtendedEntityType } from '@/ui/hooks/use-query-extended-entity-type'; |
| 13 | +import { |
| 14 | + coreActiveColumnsAtom, |
| 15 | + corePageNumberAtom, |
| 16 | + coreSortStateAtom, |
| 17 | +} from '@/ui/segments/data-table/elements/context'; |
| 18 | +import { getEntityByExtendedType } from '@/entity-configuration/domain/helpers'; |
| 19 | +import { MiniDetailView } from '@/ui/segments/mini-detail-view'; |
| 20 | +import { useWorkspace } from '@/ui/hooks/use-workspace'; |
| 21 | +import { MainTable } from '@/ui/segments/data-table'; |
| 22 | +import { DEFAULT_PAGE_NUMBER } from '@/constants'; |
| 23 | +import { cn } from '@/utils/css-class'; |
| 24 | + |
| 25 | +import type { TExtendedEntitiesTypeDict } from '@/api/entitycore/types/extended-entity-type'; |
| 26 | +import type { EntityCoreIdentifiableNamed } from '@/api/entitycore/types/shared/global'; |
| 27 | +import type { EntityCoreResponse } from '@/api/entitycore/types/shared/response'; |
| 28 | +import type { WorkspaceContext } from '@/types/common'; |
| 29 | +import type { TWorkspaceScope } from '@/constants'; |
| 30 | +import type { KebabCase } from '@/utils/type'; |
| 31 | +import { |
| 32 | + makeSelectEntityClickEvent, |
| 33 | + useSelectEntityClickEvent, |
| 34 | +} from '@/ui/segments/mini-detail-view/event'; |
| 35 | + |
| 36 | +export function BrowseStandardScope() { |
| 37 | + const searchParams = useSearchParams(); |
| 38 | + const scope = searchParams.get('scope') as TWorkspaceScope; |
| 39 | + const { type } = useParams<WorkspaceContext & { type: KebabCase<TExtendedEntitiesTypeDict> }>(); |
| 40 | + const { virtualLabId, projectId } = useWorkspace(); |
| 41 | + const dataKey = compact([virtualLabId, projectId, type, scope]).join('/'); |
| 42 | + const dataType = snakeCase(type) as TExtendedEntitiesTypeDict; |
| 43 | + |
| 44 | + const entity = getEntityByExtendedType({ type: dataType }); |
| 45 | + const setPageNumber = useSetAtom(corePageNumberAtom(dataKey)); |
| 46 | + const [sortState, setSortState] = useAtom(coreSortStateAtom({ key: dataKey })); |
| 47 | + const [miniViewPresent, updateDisplayMiniView] = useState(false); |
| 48 | + |
| 49 | + const onSortChange = (newSortState: any) => { |
| 50 | + setPageNumber(DEFAULT_PAGE_NUMBER); |
| 51 | + setSortState(newSortState); |
| 52 | + }; |
| 53 | + |
| 54 | + const allColumns = useDataTableColumns<EntityCoreIdentifiableNamed>({ |
| 55 | + dataType, |
| 56 | + sortState, |
| 57 | + setSortState: onSortChange, |
| 58 | + }); |
| 59 | + |
| 60 | + const activeColumns = useAtomValue(coreActiveColumnsAtom({ dataType, key: dataKey })); |
| 61 | + const columns = allColumns.filter(({ key }) => (activeColumns || []).includes(key as string)); |
| 62 | + |
| 63 | + const { data, error, isPlaceholderData, isFetching, isLoading } = useQueryExtendedEntityType({ |
| 64 | + context: { |
| 65 | + key: dataKey, |
| 66 | + workspaceScope: scope, |
| 67 | + extendedEntityType: snakeCase(type) as TExtendedEntitiesTypeDict, |
| 68 | + }, |
| 69 | + workspace: { virtualLabId, projectId }, |
| 70 | + queryFn: async ({ queryKey }) => { |
| 71 | + const [{ workspace, queryParameters }] = queryKey; |
| 72 | + return entity?.api?.query.list?.({ |
| 73 | + withFacets: true, |
| 74 | + filters: { ...queryParameters }, |
| 75 | + context: workspace, |
| 76 | + }); |
| 77 | + }, |
| 78 | + enabled: ({ queryKey }) => { |
| 79 | + const [{ queryParameters }] = queryKey; |
| 80 | + if (!get(queryParameters, 'within_brain_region_brain_region_id', null)) return false; |
| 81 | + return true; |
| 82 | + }, |
| 83 | + }); |
| 84 | + |
| 85 | + const dataSource = (data as EntityCoreResponse<EntityCoreIdentifiableNamed>)?.data; |
| 86 | + const facets = (data as EntityCoreResponse<EntityCoreIdentifiableNamed>)?.facets; |
| 87 | + const pagination = (data as EntityCoreResponse<EntityCoreIdentifiableNamed>)?.pagination; |
| 88 | + |
| 89 | + const onCellClick = (_: string, record: EntityCoreIdentifiableNamed) => { |
| 90 | + // navigate( |
| 91 | + // `${V2_MIGRATION_TEMPORARY_BASE_PATH}/${virtualLabId}/${projectId}/explore/view/${kebabCase(record.type)}/${record.id}` |
| 92 | + // ); |
| 93 | + makeSelectEntityClickEvent({ |
| 94 | + display: true, |
| 95 | + data: record, |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + useSelectEntityClickEvent((event) => { |
| 100 | + updateDisplayMiniView(event.detail.display); |
| 101 | + }); |
| 102 | + |
| 103 | + if (error) |
| 104 | + return ( |
| 105 | + <div> |
| 106 | + <pre>{JSON.stringify(error, null, 2)}</pre> |
| 107 | + </div> |
| 108 | + ); |
| 109 | + |
| 110 | + return ( |
| 111 | + <> |
| 112 | + <div |
| 113 | + id="explore-body-container" |
| 114 | + data-testid="explore-body-container" |
| 115 | + className="h-full max-h-[calc(100vh-11.8rem)] min-h-0 w-full min-w-0 overflow-hidden rounded-2xl [grid-area:body]" |
| 116 | + > |
| 117 | + <div id="main-listing-table-container" className={cn('h-full w-full')}> |
| 118 | + <MainTable<EntityCoreIdentifiableNamed> |
| 119 | + controlsVisible |
| 120 | + showLoadingState |
| 121 | + sticky={{ offsetHeader: 75.5 }} |
| 122 | + isLoading={(isPlaceholderData && isFetching) || isLoading} |
| 123 | + dataScope={scope} |
| 124 | + dataSource={dataSource ?? []} |
| 125 | + dataType={dataType} |
| 126 | + workspace={{ virtualLabId, projectId }} |
| 127 | + dataKey={dataKey} |
| 128 | + columns={columns} |
| 129 | + facets={facets} |
| 130 | + onCellClick={onCellClick} |
| 131 | + resultPagination={{ |
| 132 | + pagination, |
| 133 | + totalData: dataSource?.length, |
| 134 | + }} |
| 135 | + cls={{ |
| 136 | + table: cn( |
| 137 | + '[&_.ant-table]:bg-neutral-1! [&_.ant-table-header_th]:bg-neutral-1!', |
| 138 | + '[&_.ant-table-placeholder]:bg-neutral-1! [&_.ant-table-tbody_tr.ant-table-placeholder]:bg-neutral-1!' |
| 139 | + ), |
| 140 | + }} |
| 141 | + /> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + <div |
| 145 | + id="mini-detail-view-container" |
| 146 | + className={cn( |
| 147 | + 'h-full max-h-[calc(100vh-11.8rem)] w-full min-w-0', |
| 148 | + '[grid-area:mini-view]', |
| 149 | + { |
| 150 | + hidden: !miniViewPresent, |
| 151 | + } |
| 152 | + )} |
| 153 | + > |
| 154 | + <MiniDetailView /> |
| 155 | + </div> |
| 156 | + </> |
| 157 | + ); |
| 158 | +} |
0 commit comments