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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x 110x | import { useState } from "react";
import { toast } from "sonner";
import {
type FlowsheetRevisionRead,
useCoreFlowsheetsPartialUpdateMutation,
useCoreFlowsheetsRevisionsDestroyMutation,
useCoreFlowsheetsRevisionsPartialUpdateMutation,
useCoreFlowsheetsRevisionsRetrieveQuery,
} from "@/api/apiStore.gen.ts";
import { useFlowsheetRevisionPreview } from "@/hooks/flowsheetRevisionPreview";
import { useFlowsheet } from "@/hooks/project.ts";
import { useFlowsheetRevisionSave } from "@/pages/flowsheet-page/flowsheet/revisions/FlowsheetRevisionSaveContext";
import { useRestoreFlowsheetRevision } from "@/pages/flowsheet-page/flowsheet/revisions/useRestoreFlowsheetRevision";
import { getFlowsheetVersionLabel } from "@/pages/flowsheet-page/flowsheet/revisions/versionLabels";
export type FlowsheetRevisionCategory = "all" | "manual" | "solve";
/** Own revision-history query state and mutations for the Projects sidebar menu. */
export function useFlowsheetRevisionsMenu(flowsheetId: number) {
const [isOpen, setIsOpen] = useState(false);
const [revisionPage, setRevisionPage] = useState(1);
const [revisionCategory, setRevisionCategory] =
useState<FlowsheetRevisionCategory>("all");
const [editingRevisionId, setEditingRevisionId] = useState<number | null>(
null,
);
const [revisionName, setRevisionName] = useState("");
const [restoreTarget, setRestoreTarget] =
useState<FlowsheetRevisionRead | null>(null);
const [deleteTarget, setDeleteTarget] =
useState<FlowsheetRevisionRead | null>(null);
const flowsheet = useFlowsheet();
const {
data: revisionData,
error: revisionError,
isFetching: revisionsFetching,
} = useCoreFlowsheetsRevisionsRetrieveQuery(
{
id: String(flowsheetId),
page: revisionPage,
category: revisionCategory,
},
{ skip: !isOpen },
);
const [deleteRevision, deleteResult] =
useCoreFlowsheetsRevisionsDestroyMutation();
const [renameRevision, renameResult] =
useCoreFlowsheetsRevisionsPartialUpdateMutation();
const [updateFlowsheet, updateResult] =
useCoreFlowsheetsPartialUpdateMutation();
const { saveRevision, isSaving } = useFlowsheetRevisionSave();
const { restoreRevision, isRestoring } = useRestoreFlowsheetRevision({
flowsheetId,
});
const { isPreview, openRevisionPreview, returnToCurrent, revisionStateId } =
useFlowsheetRevisionPreview();
const saveLatestRevision = async () => {
Iif (isPreview) return;
if (await saveRevision()) {
setRevisionCategory("manual");
setRevisionPage(1);
}
};
const updateRevisionCategory = (category: FlowsheetRevisionCategory) => {
setRevisionCategory(category);
setRevisionPage(1);
cancelRenamingRevision();
};
const restoreSelectedRevision = async () => {
Iif (!restoreTarget) return;
Iif (await restoreRevision(restoreTarget)) setRestoreTarget(null);
};
const updateAutoRevisionSetting = async (checked: boolean) => {
Iif (isPreview) return;
try {
await updateFlowsheet({
id: String(flowsheetId),
patchedFlowsheet: { auto_snapshot_after_single_solve: checked },
}).unwrap();
} catch {
toast.error("Automatic version setting could not be updated");
}
};
const deleteSelectedRevision = async () => {
Iif (!deleteTarget || deleteResult.isLoading) return;
try {
await deleteRevision({
id: String(flowsheetId),
stateId: String(deleteTarget.state_id),
}).unwrap();
if (revisionData?.revisions.length === 1 && revisionPage > 1) {
setRevisionPage(revisionPage - 1);
}
setDeleteTarget(null);
toast.success("Version deleted");
} catch {
toast.error("Version could not be deleted");
}
};
const startRenamingRevision = (revision: FlowsheetRevisionRead) => {
setEditingRevisionId(revision.state_id);
setRevisionName(getFlowsheetVersionLabel(revision));
};
const cancelRenamingRevision = () => {
setEditingRevisionId(null);
setRevisionName("");
};
const saveRevisionName = async () => {
const normalizedName = revisionName.trim();
if (
editingRevisionId === null ||
!normalizedName ||
normalizedName.length > 64 ||
renameResult.isLoading
) {
return;
}
try {
await renameRevision({
id: String(flowsheetId),
stateId: String(editingRevisionId),
patchedUpdateFlowsheetRevision: { label: normalizedName },
}).unwrap();
cancelRenamingRevision();
toast.success("Version renamed");
} catch {
toast.error("Version name could not be updated");
}
};
return {
autoSaveEnabled: flowsheet?.auto_snapshot_after_single_solve ?? false,
autoSavePending: updateResult.isLoading,
cancelRenamingRevision,
deletePending: deleteResult.isLoading,
deleteSelectedRevision,
deleteTarget,
editingRevisionId,
isOpen,
isPreview,
isRestoring,
isSaving,
openRevisionPreview,
renamePending: renameResult.isLoading,
restoreSelectedRevision,
restoreTarget,
returnToCurrent,
revisionData,
revisionError,
revisionCategory,
revisionName,
revisionStateId,
revisionsFetching,
saveLatestRevision,
saveRevisionName,
setDeleteTarget,
setIsOpen,
setRestoreTarget,
setRevisionName,
setRevisionPage,
startRenamingRevision,
updateRevisionCategory,
updateAutoRevisionSetting,
};
}
|