Skip to content

Commit

Permalink
Persist files datatable sorting across view loads (#318)
Browse files Browse the repository at this point in the history
* persist files datatable sorting with local storage

* fix type error

* use jotai for persistence

* format

---------

Co-authored-by: Francisco Salgueiro <fgcdbs@gmail.com>
  • Loading branch information
zackschuster and franciscoBSalgueiro authored Jun 22, 2024
1 parent 3cc5a78 commit 661f97f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/components/files/DirectoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { activeTabAtom, deckAtomFamily, tabsAtom } from "@/state/atoms";
import { read_games } from "@/utils/db";
import { capitalize } from "@/utils/format";
import { createTab } from "@/utils/tabs";
import { Badge, Box, Group, Text } from "@mantine/core";
import { Badge, Box, Group } from "@mantine/core";
import {
IconChevronRight,
IconEye,
Expand All @@ -15,6 +15,7 @@ import clsx from "clsx";
import dayjs from "dayjs";
import Fuse from "fuse.js";
import { useAtom, useSetAtom } from "jotai";
import { atomWithStorage } from "jotai/utils";
import { useContextMenu } from "mantine-contextmenu";
import { DataTable, type DataTableSortStatus } from "mantine-datatable";
import { useCallback, useMemo, useState } from "react";
Expand Down Expand Up @@ -56,6 +57,18 @@ function recursiveSort(
});
}

type SortStatus = DataTableSortStatus<MetadataOrEntry>;
const sortStatusStorageId = `${DirectoryTable.name}-sort-status` as const;
const sortStatusAtom = atomWithStorage<SortStatus>(
sortStatusStorageId,
{
columnAccessor: "lastModified",
direction: "desc",
},
undefined,
{ getOnInit: true },
);

export default function DirectoryTable({
files,
isLoading,
Expand All @@ -73,10 +86,7 @@ export default function DirectoryTable({
search: string;
filter: string;
}) {
const [sort, setSort] = useState<DataTableSortStatus<MetadataOrEntry>>({
columnAccessor: "lastModified",
direction: "desc",
});
const [sort, setSort] = useAtom<SortStatus>(sortStatusAtom);

const flattedFiles = useMemo(() => flattenFiles(files ?? []), [files]);
const fuse = useMemo(
Expand Down Expand Up @@ -155,9 +165,7 @@ function Table({
selected: FileMetadata | null;
setSelectedFile: (file: FileMetadata) => void;
sort: DataTableSortStatus<MetadataOrEntry>;
setSort: React.Dispatch<
React.SetStateAction<DataTableSortStatus<MetadataOrEntry>>
>;
setSort: (sort: SortStatus) => void;
}) {
const { t } = useTranslation();

Expand Down

0 comments on commit 661f97f

Please sign in to comment.