gitscope
← marzneshin / v0.4.1 / dashboard/src/features/entity-table/hooks/use-sidebar-entity-table.ts
https://dev.kronoz.win/r/marzneshin.git
dashboard/src/features/entity-table/hooks/use-sidebar-entity-table.ts · TypeScript
Blame Raw
  1import { useMemo } from "react";
  2import { useTranslation } from "react-i18next";
  3import { useQuery } from "@tanstack/react-query";
  4import {
  5    useFiltering,
  6    usePagination,
  7    useEntityTable,
  8    useVisibility,
  9    useSorting,
 10    type SortableSidebarQueryKey,
 11    type EntitySidebarQueryKeyType,
 12    type FetchEntityReturn,
 13    type SidebarQueryKey,
 14    type UseRowSelectionReturn
 15} from ".";
 16import { useScreenBreakpoint } from "@marzneshin/hooks";
 17import {
 18    type SidebarEntityCardSectionsProps
 19} from "@marzneshin/features/entity-table/components";
 20
 21interface UseSidebarEntityTableParams<T, S> {
 22    fetchEntity: ({ queryKey }: EntitySidebarQueryKeyType) => FetchEntityReturn<T>;
 23    sidebarEntities: S[];
 24    setSidebarEntityId: (entity: string | undefined) => void;
 25    sidebarEntityId?: string;
 26    sidebarCardProps: SidebarEntityCardSectionsProps<S>;
 27    secondaryEntityKey: string;
 28    columnsFn: any;
 29    filteredColumn: string;
 30    entityKey: string;
 31    rowSelection?: UseRowSelectionReturn;
 32    manualSorting?: boolean;
 33    onCreate: () => void;
 34    onEdit: (entity: T) => void;
 35    onOpen: (entity: T) => void;
 36    onDelete: (entity: T) => void;
 37}
 38
 39export const useSidebarEntityTable = <T, S>({
 40    fetchEntity,
 41    columnsFn,
 42    filteredColumn,
 43    rowSelection,
 44    entityKey,
 45    manualSorting = false,
 46    onCreate,
 47    onEdit,
 48    onOpen,
 49    onDelete,
 50    sidebarEntityId,
 51    setSidebarEntityId,
 52    sidebarEntities,
 53    sidebarCardProps,
 54    secondaryEntityKey,
 55}: UseSidebarEntityTableParams<T, S>) => {
 56    const { t } = useTranslation();
 57    const filtering = useFiltering({ column: filteredColumn });
 58    const sorting = useSorting();
 59    const visibility = useVisibility();
 60    const desktop = useScreenBreakpoint("md");
 61    const { onPaginationChange, pageIndex, pageSize } = usePagination();
 62
 63    const sortedQuery: SortableSidebarQueryKey = [
 64        entityKey,
 65        sidebarEntityId,
 66        secondaryEntityKey,
 67        pageIndex,
 68        pageSize,
 69        filtering.columnFilters,
 70        sorting.sorting[0]?.id ? sorting.sorting[0].id : "created_at",
 71        sorting.sorting[0]?.desc,
 72    ];
 73
 74    const query: SidebarQueryKey = [
 75        entityKey,
 76        sidebarEntityId,
 77        secondaryEntityKey,
 78        pageIndex,
 79        pageSize,
 80        filtering.columnFilters,
 81    ];
 82
 83    const { data, isFetching } = useQuery({
 84        queryFn: fetchEntity,
 85        queryKey: manualSorting ? sortedQuery : query,
 86        initialData: { entity: [], pageCount: 1 },
 87    });
 88
 89    const columns = columnsFn({ onEdit, onDelete, onOpen });
 90    const table = useEntityTable({
 91        data,
 92        columns,
 93        pageSize,
 94        pageIndex,
 95        rowSelection,
 96        visibility,
 97        sorting,
 98        onPaginationChange,
 99    });
100
101    const entityTableContextValue = useMemo(
102        () => ({
103            table,
104            data: data.entity,
105            filtering,
106            isLoading: isFetching,
107        }),
108        [table, data.entity, filtering, isFetching],
109    );
110
111    const sidebarEntityTableContextValue = useMemo(
112        () => ({
113            sidebarEntities,
114            setSidebarEntityId,
115            sidebarEntityId,
116            sidebarCardProps
117        }),
118        [sidebarEntities, setSidebarEntityId, sidebarEntityId, sidebarCardProps],
119    );
120
121    return {
122        t,
123        desktop,
124        table,
125        entityTableContextValue,
126        sidebarEntityTableContextValue,
127        columns,
128        onCreate
129    };
130};