All files / src/pages/flowsheet-page/flowsheet/revisions FlowsheetRevisionPreviewSwitcher.tsx

86.2% Statements 25/29
62.06% Branches 18/29
50% Functions 1/2
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                            103x     35x 35x 35x 35x   35x 35x 47x 46x         6x       35x         35x 29x                           2x 2x                                 180x 9x                     44x 93x 93x      
import { History, Loader2 } from "lucide-react";
import { useState } from "react";
import { Button } from "@/ahuora-design-system/ui/button";
import Paginator from "@/ahuora-design-system/ui/paginator";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/ahuora-design-system/ui/popover";
import { useCoreFlowsheetsRevisionsRetrieveQuery } from "@/api/apiStore.gen";
import { useFlowsheetRevisionPreview } from "@/hooks/flowsheetRevisionPreview";
import { useFlowsheetId } from "@/hooks/project";
import { FlowsheetRevisionList } from "./FlowsheetRevisionList";
 
const noop = () => undefined;
 
/** Paginated revision chooser that remains reachable from preview mode. */
export function FlowsheetRevisionPreviewSwitcher() {
  const flowsheetId = useFlowsheetId();
  const [isOpen, setIsOpen] = useState(false);
  const [page, setPage] = useState(1);
  const { openRevisionPreview, returnToCurrent, revisionStateId } =
    useFlowsheetRevisionPreview();
  const { data, isFetching } = useCoreFlowsheetsRevisionsRetrieveQuery(
    { id: String(flowsheetId), page },
    { skip: !isOpen },
  );
 
  return (
    <Popover open={isOpen} onOpenChange={setIsOpen}>
      <PopoverTrigger asChild>
        <Button type="button" variant="outline" size="sm" className="h-7">
          Switch version
        </Button>
      </PopoverTrigger>
      <PopoverContent align="center" className="w-96 space-y-2 p-3">
        <p className="flex items-center gap-2 text-sm font-medium">
          <History className="h-4 w-4" aria-hidden="true" />
          Choose a version
        </p>
        {isFetching ? (
          <div className="flex items-center gap-2 py-3 text-sm text-muted-foreground">
            <Loader2 className="h-4 w-4 animate-spin" />
            Loading versions...
          </div>
        ) : data?.revisions.length ? (
          <FlowsheetRevisionList
            revisions={data.revisions}
            previewedRevisionStateId={revisionStateId}
            canEdit={false}
            editingRevisionId={null}
            revisionName=""
            renamePending={false}
            onPreview={(revision) => {
              openRevisionPreview(revision);
              setIsOpen(false);
            }}
            onStopPreview={() => {
              returnToCurrent();
              setIsOpen(false);
            }}
            onRestore={noop}
            onDelete={noop}
            onStartRename={noop}
            onCancelRename={noop}
            onRevisionNameChange={noop}
            onSaveRevisionName={noop}
          />
        ) : (
          <p className="py-2 text-sm text-muted-foreground">
            No saved versions
          </p>
        )}
        {data && data.pages > 1 && (
          <Paginator
            minimal
            page={data.page}
            setPage={setPage}
            data={{
              previous: data.page > 1 ? `?page=${data.page - 1}` : null,
              next: data.page < data.pages ? `?page=${data.page + 1}` : null,
              pages: data.pages,
            }}
          />
        )}
      </PopoverContent>
    </Popover>
  );
}