Skip to content

Commit

Permalink
Fix clearing url bar when loading a different example
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Jun 26, 2024
1 parent 919ef45 commit ee7d4bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
28 changes: 13 additions & 15 deletions gui/src/app/SPAnalysis/SPAnalysisContextProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, FunctionComponent, PropsWithChildren, useEffect, useReducer } from "react"
import { deserializeAnalysis, initialDataModel, serializeAnalysis, SPAnalysisDataModel, modelHasUnsavedChanges } from "./SPAnalysisDataModel"
import { createContext, FunctionComponent, PropsWithChildren, useCallback, useEffect, useReducer } from "react"
import { deserializeAnalysis, initialDataModel, serializeAnalysis, SPAnalysisDataModel } from "./SPAnalysisDataModel"
import { SPAnalysisReducer, SPAnalysisReducerAction, SPAnalysisReducerType } from "./SPAnalysisReducer"
import { useSearchParams } from "react-router-dom"
import { fromSearchParams, fetchRemoteAnalysis } from "./SPAnalysisQueryLoading"
Expand All @@ -19,10 +19,19 @@ export const SPAnalysisContext = createContext<SPAnalysisContextType>({


const SPAnalysisContextProvider: FunctionComponent<PropsWithChildren<SPAnalysisContextProviderProps>> = ({ children }) => {
const [data, update] = useReducer<SPAnalysisReducerType>(SPAnalysisReducer, initialDataModel)

const [searchParams, setSearchParams] = useSearchParams();

const onDirty = useCallback(() => {
// whenever the data state is 'dirty', we want to
// clear the URL bar as to indiciate that the viewed content is
// no longer what the link would point to
if (searchParams.size !== 0)
setSearchParams(new URLSearchParams())
}, [searchParams, setSearchParams]);


const [data, update] = useReducer<SPAnalysisReducerType>(SPAnalysisReducer(onDirty), initialDataModel)

////////////////////////////////////////////////////////////////////////////////////////
// For convenience, we save the state to local storage so it is available on
// reload of the page But this will be revised in the future to use a more
Expand Down Expand Up @@ -62,17 +71,6 @@ const SPAnalysisContextProvider: FunctionComponent<PropsWithChildren<SPAnalysisC

}, [data, searchParams])


useEffect(() => {
// whenever the data state is 'dirty', we want to
// clear the URL bar as to indiciate that the viewed content is
// no longer what the link would point to
if (searchParams.size !== 0 && modelHasUnsavedChanges(data)) {
setSearchParams(new URLSearchParams());
}
}, [data, searchParams, setSearchParams])


return (
<SPAnalysisContext.Provider value={{ data, update }}>
{children}
Expand Down
9 changes: 0 additions & 9 deletions gui/src/app/SPAnalysis/SPAnalysisDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ export const initialDataModel: SPAnalysisDataModel = {
samplingOpts: defaultSamplingOpts
}


export const getStringKnownFileKeys = () => Object.values(SPAnalysisKnownFiles).filter((v) => isNaN(Number(v)));

export const modelHasUnsavedChanges = (data: SPAnalysisDataModel): boolean => {
const stringFileKeys = getStringKnownFileKeys()
return stringFileKeys.some((k) => data[k] !== data.ephemera[k])
}


export const serializeAnalysis = (data: SPAnalysisDataModel): string => {
const intermediary = {
...data, ephemera: undefined
Expand Down
9 changes: 8 additions & 1 deletion gui/src/app/SPAnalysis/SPAnalysisReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export type SPAnalysisReducerAction = {
type: 'clear'
}

export const SPAnalysisReducer: SPAnalysisReducerType = (s: SPAnalysisDataModel, a: SPAnalysisReducerAction) => {
export const SPAnalysisReducer = (onDirty: () => void) => (s: SPAnalysisDataModel, a: SPAnalysisReducerAction) => {
if (a.type !== "loadInitialData") {
// TextEditor seems to trigger occasional spurious edits where nothing changes
if (a.type !== "editFile" || s[a.filename] != a.content) {
onDirty();
}
}

switch (a.type) {
case "loadStanie": {
const dataFileContent = JSON.stringify(a.stanie.data, null, 2);
Expand Down

0 comments on commit ee7d4bf

Please sign in to comment.