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 | 16x 1x 16x 1576x 1576x 1576x 1576x 2571x 2571x 1576x 1576x 1576x 123x 1576x 1576x 1822x 110x 1576x 1686x 1576x 1576x 8x 8x 3x 5x 8x 2575x 1576x 3x 3x 3574x 1576x 5x 5x 3574x 1576x 977x 23x 8x 8x 2x 2x 8x 1008x 9200x 1576x 999x 999x 999x 8147x 1007x 1007x 1576x 7618x 3590x | import { type PropsWithChildren, useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";
import { toast } from "sonner";
import {
type FlowsheetRevisionRead,
useCoreFlowsheetsRevisionDetailRetrieveQuery,
} from "@/api/apiStore.gen";
import { getRevisionRouteScopeFromUrl } from "@/api/flowsheetRevisionScope";
import {
removeStateLocalSearchParams,
useFlowsheetRevisionRefresh,
} from "@/hooks/cache/useFlowsheetRevisionRefresh";
import { FlowsheetRevisionPreviewContext } from "@/hooks/flowsheetRevisionPreview";
import { useFlowsheetId } from "@/hooks/project";
import {
CONTENT_MAP,
ContentTypes,
} from "../LeftSideBar/LeftSideBarTabDefinitions";
/** Preserve valid workspace panels and fall back to the canvas for malformed links. */
function normalizeTopLevelContent(searchParams: URLSearchParams) {
const content = searchParams.get("content");
if (!content || !Object.prototype.hasOwnProperty.call(CONTENT_MAP, content)) {
searchParams.set("content", ContentTypes.flowsheet);
}
return searchParams;
}
/** Own revision URL state, deep-link metadata, and invalid-preview recovery. */
export function FlowsheetRevisionPreviewProvider({
children,
}: PropsWithChildren) {
const flowsheetId = useFlowsheetId();
const [searchParams, setSearchParams] = useSearchParams();
const revisionScope = getRevisionRouteScopeFromUrl(
`?${searchParams.toString()}`,
);
const revisionStateId = revisionScope.revisionStateId;
const [normalizedRevisionStateId, setNormalizedRevisionStateId] = useState<
number | null
>(null);
const {
data: revision,
isLoading,
isFetching,
isError,
} = useCoreFlowsheetsRevisionDetailRetrieveQuery(
{
id: String(flowsheetId),
stateId: String(revisionStateId ?? 0),
},
{
skip:
!flowsheetId || !revisionScope.isPresent || revisionStateId === null,
},
);
const { resetRevisionTransientState, refreshAfterPreview } =
useFlowsheetRevisionRefresh();
const selectReadState = (
revisionStateId: number | null,
options?: { replace?: boolean },
) => {
setSearchParams((current) => {
const next = removeStateLocalSearchParams(current);
if (revisionStateId === null) {
next.delete("revision");
} else {
next.set("revision", String(revisionStateId));
}
return normalizeTopLevelContent(next);
}, options);
};
const returnToCurrent = () => {
selectReadState(null);
refreshAfterPreview();
};
const openRevisionPreview = (selectedRevision: FlowsheetRevisionRead) => {
resetRevisionTransientState();
selectReadState(selectedRevision.state_id);
};
useEffect(() => {
if (!revisionScope.isPresent || revisionStateId === null) {
setNormalizedRevisionStateId(null);
return;
}
Iif (normalizedRevisionStateId === revisionStateId) return;
// A shared or manually edited preview URL can carry record IDs from a
// different physical state. Clear those IDs once while preserving any
// valid top-level view selected by the user.
const normalizedSearchParams = removeStateLocalSearchParams(searchParams);
normalizeTopLevelContent(normalizedSearchParams);
if (normalizedSearchParams.toString() !== searchParams.toString()) {
resetRevisionTransientState();
setSearchParams(normalizedSearchParams, { replace: true });
}
setNormalizedRevisionStateId(revisionStateId);
}, [
normalizedRevisionStateId,
resetRevisionTransientState,
revisionScope.isPresent,
revisionStateId,
searchParams,
setSearchParams,
]);
useEffect(() => {
const revisionUnavailable =
revisionScope.isPresent && (revisionStateId === null || isError);
Iif (!revisionUnavailable) return;
toast.error("This version is no longer available.");
setSearchParams(
(current) => {
const next = removeStateLocalSearchParams(current);
next.delete("revision");
return normalizeTopLevelContent(next);
},
{ replace: true },
);
refreshAfterPreview();
}, [
isError,
refreshAfterPreview,
revisionScope.isPresent,
revisionStateId,
setSearchParams,
]);
return (
<FlowsheetRevisionPreviewContext.Provider
value={{
isPreview: revisionScope.isPresent,
isLoading:
revisionScope.isPresent &&
(revisionStateId === null ||
normalizedRevisionStateId !== revisionStateId ||
isLoading ||
isFetching ||
!revision),
revisionStateId,
revision,
openRevisionPreview,
returnToCurrent,
}}
>
{children}
</FlowsheetRevisionPreviewContext.Provider>
);
}
|