Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

13.2 KB
Raw Blame
import type { ColumnDef, RowSelectionState, SortingState } from '@tanstack/solid-table';
import type { Accessor, Component, Setter } from 'solid-js';
import type { Document } from '../documents.types';
import type { Pagination } from '@/modules/shared/pagination/pagination.types';
import type { Tag } from '@/modules/tags/tags.types';
import { formatBytes } from '@corentinth/chisels';
import { A } from '@solidjs/router';
import {
  createSolidTable,
  flexRender,
  getCoreRowModel,
  getPaginationRowModel,
} from '@tanstack/solid-table';
import { For, Match, Show, Switch } from 'solid-js';
import { RelativeTime } from '@/modules/i18n/components/RelativeTime';
import { useI18n } from '@/modules/i18n/i18n.provider';
import { cn } from '@/modules/shared/style/cn';
import { DocumentTagsList } from '@/modules/tags/components/tag-list.component';
import { Button } from '@/modules/ui/components/button';
import { Checkbox, CheckboxControl } from '@/modules/ui/components/checkbox';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/modules/ui/components/select';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@/modules/ui/components/table';
import {
  getDocumentIcon,
  getDocumentNameExtension,
  getDocumentNameWithoutExtension,
} from '../document.models';
import { DocumentManagementDropdown } from './document-management-dropdown.component';

const selectionColumn: ColumnDef<Document> = {
  id: 'select',
  header: (props) => {
    const { t } = useI18n();

    return (
      <Checkbox
        checked={props.table.getIsAllPageRowsSelected()}
        indeterminate={
          props.table.getIsSomePageRowsSelected() && !props.table.getIsAllPageRowsSelected()
        }
        onChange={(value) => props.table.toggleAllPageRowsSelected(value)}
        aria-label={t('documents.list.select.all')}
      >
        <CheckboxControl />
      </Checkbox>
    );
  },
  cell: (props) => {
    const { t } = useI18n();

    return (
      <Checkbox
        checked={props.row.getIsSelected()}
        onChange={(value) => props.row.toggleSelected(value)}
        aria-label={t('documents.list.select.row')}
      >
        <CheckboxControl />
      </Checkbox>
    );
  },
  enableSorting: false,
  enableHiding: false,
};

export const documentDateColumn: ColumnDef<Document> = {
  header: () => {
    const { t } = useI18n();
    return <span class="hidden sm:block">{t('documents.list.table.headers.document-date')}</span>;
  },
  accessorKey: 'documentDate',
  enableSorting: true,
  cell: (data) => {
    const { t } = useI18n();
    const value = data.getValue<Date | null | undefined>();
    return (
      <span class="text-muted-foreground hidden sm:block">
        <Show
          when={value}
          fallback={<span class="text-muted-foreground/50">{t('documents.info.no-date')}</span>}
        >
          {(date) => <RelativeTime date={date()} />}
        </Show>
      </span>
    );
  },
};

export const createdAtColumn: ColumnDef<Document> = {
  header: () => {
    const { t } = useI18n();
    return <span class="hidden sm:block">{t('documents.list.table.headers.created')}</span>;
  },
  accessorKey: 'createdAt',
  enableSorting: true,
  cell: (data) => (
    <RelativeTime class="text-muted-foreground hidden sm:block" date={data.getValue<Date>()} />
  ),
};

export const deletedAtColumn: ColumnDef<Document> = {
  header: () => {
    const { t } = useI18n();
    return <span class="hidden sm:block">{t('documents.list.table.headers.deleted')}</span>;
  },
  accessorKey: 'deletedAt',
  cell: (data) => (
    <RelativeTime class="text-muted-foreground hidden sm:block" date={data.getValue<Date>()} />
  ),
};

export const standardActionsColumn: ColumnDef<Document> = {
  header: () => {
    const { t } = useI18n();
    return <span class="block text-right">{t('documents.list.table.headers.actions')}</span>;
  },
  id: 'actions',
  enableSorting: false,
  cell: (data) => (
    <div class="flex items-center justify-end">
      <DocumentManagementDropdown document={data.row.original} />
    </div>
  ),
};

export const tagsColumn: ColumnDef<Document> = {
  header: () => {
    const { t } = useI18n();
    return <span class="hidden sm:block">{t('documents.list.table.headers.tags')}</span>;
  },
  accessorKey: 'tags',
  enableSorting: false,
  cell: (data) => (
    <DocumentTagsList
      tags={data.getValue<Tag[]>()}
      tagClass="text-xs text-muted-foreground"
      triggerClass="size-6"
      documentId={data.row.original.id}
      organizationId={data.row.original.organizationId}
      asLink
    />
  ),
};

export const DocumentsPaginatedList: Component<{
  documents: Document[];
  documentsCount: number;
  getPagination?: Accessor<Pagination>;
  setPagination?: Setter<Pagination>;
  extraColumns?: ColumnDef<Document>[];
  showPagination?: boolean;
  enableBatchSelection?: boolean;
  getRowSelection?: Accessor<RowSelectionState>;
  setRowSelection?: Setter<RowSelectionState>;
  getSorting?: Accessor<SortingState>;
  setSorting?: Setter<SortingState>;
}> = (props) => {
  const { t } = useI18n();
  const table = createSolidTable({
    get data() {
      return props.documents ?? [];
    },
    getRowId: (row) => row.id,
    columns: [
      ...(props.enableBatchSelection ? [selectionColumn] : []),
      {
        header: () => t('documents.list.table.headers.file-name'),
        id: 'name',
        accessorFn: (row) => row.name,
        enableSorting: true,
        cell: (data) => (
          <div class="overflow-hidden flex gap-4 items-center max-w-500px">
            <div class="bg-muted flex items-center justify-center p-2 rounded-lg">
              <div
                class={cn(getDocumentIcon({ document: data.row.original }), 'size-6 text-primary')}
              />
            </div>

            <div class="flex-1 flex flex-col gap-1 truncate">
              <A
                href={`/organizations/${data.row.original.organizationId}/documents/${data.row.original.id}`}
                class="font-bold truncate block hover:underline"
                title={data.row.original.name}
              >
                {getDocumentNameWithoutExtension({
                  name: data.row.original.name,
                })}
              </A>

              <div class="text-xs text-muted-foreground lh-tight">
                {[
                  formatBytes({ bytes: data.row.original.originalSize, base: 1000 }),
                  getDocumentNameExtension({ name: data.row.original.name }),
                ]
                  .filter(Boolean)
                  .join(' - ')}{' '}
                - <RelativeTime date={data.row.original.createdAt} />
              </div>
            </div>
          </div>
        ),
      },
      ...(props.extraColumns ?? []),
    ],
    get rowCount() {
      return props.documentsCount;
    },
    getCoreRowModel: getCoreRowModel(),
    getPaginationRowModel: getPaginationRowModel(),
    onPaginationChange: props.setPagination,
    onRowSelectionChange: props.setRowSelection,
    onSortingChange: props.setSorting,
    enableRowSelection: props.enableBatchSelection ?? false,
    enableSorting: Boolean(props.setSorting),
    state: {
      get pagination() {
        return props.getPagination?.();
      },
      get rowSelection() {
        return props.getRowSelection?.() ?? {};
      },
      get sorting() {
        return props.getSorting?.() ?? [];
      },
    },
    manualPagination: true,
    manualSorting: true,
  });

  return (
    <div>
      <Switch>
        <Match when={props.documentsCount > 0}>
          <Table>
            <TableHeader>
              <For each={table.getHeaderGroups()}>
                {(headerGroup) => (
                  <TableRow>
                    <For each={headerGroup.headers}>
                      {(header) => {
                        return (
                          <TableHead>
                            <Show when={!header.isPlaceholder}>
                              <Show
                                when={header.column.getCanSort()}
                                fallback={flexRender(
                                  header.column.columnDef.header,
                                  header.getContext(),
                                )}
                              >
                                <button
                                  type="button"
                                  class="flex items-center gap-1 cursor-pointer select-none"
                                  onClick={header.column.getToggleSortingHandler()}
                                >
                                  {flexRender(header.column.columnDef.header, header.getContext())}
                                  <Show when={header.column.getIsSorted() === 'asc'}>
                                    <div class="i-tabler-arrow-up size-3.5" />
                                  </Show>
                                  <Show when={header.column.getIsSorted() === 'desc'}>
                                    <div class="i-tabler-arrow-down size-3.5" />
                                  </Show>
                                  <Show when={!header.column.getIsSorted()}>
                                    <div class="i-tabler-arrows-sort size-3.5 opacity-40" />
                                  </Show>
                                </button>
                              </Show>
                            </Show>
                          </TableHead>
                        );
                      }}
                    </For>
                  </TableRow>
                )}
              </For>
            </TableHeader>

            <TableBody>
              <Show when={table.getRowModel().rows?.length}>
                <For each={table.getRowModel().rows}>
                  {(row) => (
                    <TableRow data-state={row.getIsSelected() && 'selected'}>
                      <For each={row.getVisibleCells()}>
                        {(cell) => (
                          <TableCell>
                            {flexRender(cell.column.columnDef.cell, cell.getContext())}
                          </TableCell>
                        )}
                      </For>
                    </TableRow>
                  )}
                </For>
              </Show>
            </TableBody>
          </Table>

          <Show when={props.showPagination ?? true}>
            <div class="flex flex-col-reverse items-center gap-4 sm:flex-row sm:justify-end mt-4">
              <div class="flex items-center space-x-2">
                <p class="whitespace-nowrap text-sm font-medium">
                  {t('common.tables.rows-per-page')}
                </p>
                <Select
                  value={table.getState().pagination.pageSize}
                  onChange={(value) => value && table.setPageSize(value)}
                  options={[15, 50, 100]}
                  itemComponent={(props) => (
                    <SelectItem item={props.item}>{props.item.rawValue}</SelectItem>
                  )}
                >
                  <SelectTrigger class="h-8 w-[4.5rem]">
                    <SelectValue<string>>{(state) => state.selectedOption()}</SelectValue>
                  </SelectTrigger>
                  <SelectContent />
                </Select>
              </div>
              <div class="flex items-center justify-center whitespace-nowrap text-sm font-medium">
                {t('common.tables.pagination-info', {
                  currentPage: table.getState().pagination.pageIndex + 1,
                  totalPages: table.getPageCount(),
                })}
              </div>
              <div class="flex items-center space-x-2">
                <Button
                  aria-label={t('common.tables.first-page')}
                  variant="outline"
                  class="flex size-8 p-0"
                  onClick={() => table.setPageIndex(0)}
                  disabled={!table.getCanPreviousPage()}
                >
                  <div class="size-4 i-tabler-chevrons-left" />
                </Button>
                <Button
                  aria-label={t('common.tables.previous-page')}
                  variant="outline"
                  size="icon"
                  class="size-8"
                  onClick={() => table.previousPage()}
                  disabled={!table.getCanPreviousPage()}
                >
                  <div class="size-4 i-tabler-chevron-left" />
                </Button>
                <Button
                  aria-label={t('common.tables.next-page')}
                  variant="outline"
                  size="icon"
                  class="size-8"
                  onClick={() => table.nextPage()}
                  disabled={!table.getCanNextPage()}
                >
                  <div class="size-4 i-tabler-chevron-right" />
                </Button>
                <Button
                  aria-label={t('common.tables.last-page')}
                  variant="outline"
                  size="icon"
                  class="flex size-8"
                  onClick={() => table.setPageIndex(table.getPageCount() - 1)}
                  disabled={!table.getCanNextPage()}
                >
                  <div class="size-4 i-tabler-chevrons-right" />
                </Button>
              </div>
            </div>
          </Show>
        </Match>
      </Switch>
    </div>
  );
};