Skip to content

Commit

Permalink
Update key for saving data in useExecutionSessionStorage (#7333)
Browse files Browse the repository at this point in the history
* Update key for saving data in useExecutionSessionStorage

* migrate data from old namespace

* use ref.current

* prettier

* lint
  • Loading branch information
salazarm committed Apr 14, 2022
1 parent 3b55c4e commit be2469b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
22 changes: 19 additions & 3 deletions js_modules/dagit/packages/core/src/app/ExecutionSessionStorage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import * as React from 'react';

import {getJSONForKey} from '../hooks/useStateWithStorage';
import {RepoAddress} from '../workspace/types';

import {AppContext} from './AppContext';

// Internal LocalStorage data format and mutation helpers

Expand Down Expand Up @@ -154,18 +157,31 @@ version flag it can use to trigger a re-render after changes are saved, so chang
namespaces changes the returned data immediately.
*/
export function useExecutionSessionStorage(
repositoryName: string,
pipelineName: string,
repoAddress: RepoAddress,
pipelineOrJobName: string,
initial: Partial<IExecutionSession> = {},
): StorageHook {
const namespace = `${repositoryName}.${pipelineName}`;
const {basePath} = React.useContext(AppContext);

const oldNamespace = `${repoAddress.name}.${pipelineOrJobName}`;
const oldData = getStorageDataForNamespace(oldNamespace);

const namespace = `${basePath}-${repoAddress.location}-${repoAddress.name}-${pipelineOrJobName}`;
const [version, setVersion] = React.useState<number>(0);

const onSave = (newData: IStorageData) => {
writeStorageDataForNamespace(namespace, newData);
setVersion(version + 1); // trigger a React render
};

// TODO: Remove this migration logic in a few patches when we know the old namespace is likely no longer being used
const oldDataMigrated = React.useRef(false);
if (!oldDataMigrated.current) {
onSave(oldData);
window.localStorage.removeItem(oldNamespace);
oldDataMigrated.current = true;
}

return [getStorageDataForNamespace(namespace, initial), onSave];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ const LaunchpadSessionContainer: React.FC<LaunchpadSessionContainerProps> = (pro
return {};
}, [isJob, partitionSets.results, presets]);

const [data, onSave] = useExecutionSessionStorage(
repoAddress.name || '',
pipeline.name,
initialDataForMode,
);
const [data, onSave] = useExecutionSessionStorage(repoAddress, pipeline.name, initialDataForMode);

const currentSession = data.sessions[data.current];
const tagsFromSession = React.useMemo(() => currentSession.tags || [], [currentSession]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const LaunchpadSetupFromRunAllowedRoot: React.FC<Props> = (props) => {

useJobTitle(explorerPath, isJob);

const [storageData, onSave] = useExecutionSessionStorage(repoAddress.name, pipelineName);
const [storageData, onSave] = useExecutionSessionStorage(repoAddress, pipelineName);

const {data, loading} = useQuery<ConfigForRunQuery>(CONFIG_FOR_RUN_QUERY, {variables: {runId}});
const runOrError = data?.runOrError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const LaunchpadSetupAllowedRoot: React.FC<Props> = (props) => {

useJobTitle(explorerPath, isJob);

const [data, onSave] = useExecutionSessionStorage(repoAddress.name, pipelineName);
const [data, onSave] = useExecutionSessionStorage(repoAddress, pipelineName);
const queryString = qs.parse(window.location.search, {ignoreQueryPrefix: true});

React.useEffect(() => {
Expand Down

0 comments on commit be2469b

Please sign in to comment.