Skip to content

Commit

Permalink
Allow to reload a project by giving its url in the query string
Browse files Browse the repository at this point in the history
  • Loading branch information
mthh committed Aug 23, 2024
1 parent a139561 commit 3eb0439
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
62 changes: 47 additions & 15 deletions src/AppPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { toggleDarkMode } from './helpers/darkmode';
import { clickLinkFromBlob } from './helpers/exports';
import { draggedElementsAreFiles, droppedElementsAreFiles, prepareFilterAndStoreFiles } from './helpers/fileUpload';
import { round } from './helpers/math';
import parseQueryString from './helpers/query-string';
import { initDb, storeProject } from './helpers/storage';

// Sub-components
Expand Down Expand Up @@ -262,6 +263,8 @@ const reloadFromProjectObject = async (
await yieldOrContinue('smooth');

// The state we want to use
// TODO: we should check the integrity of the project object
// either by using a schema, by checking the presence of the required fields...
const {
version,

Check warning on line 269 in src/AppPage.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

'version' is assigned a value but never used
applicationSettings,
Expand All @@ -286,6 +289,30 @@ const reloadFromProjectObject = async (
setReloadingProject(false);
};

const projectFromQueryString = async (): Promise<ProjectDescription | undefined> => {
let projectDefinition;
if (window.location.search) {
const qs = parseQueryString(window.location.search);
const reloadUrl = qs.reload.startsWith('http') ? qs.reload : undefined;
if (typeof (window.history.replaceState) !== 'undefined') {
// replaceState should avoid creating a new entry on the history
const obj = {
Page: window.location.search,
Url: window.location.pathname,
};
window.history.replaceState(obj, obj.Page, obj.Url);
}
if (reloadUrl) {
const response = await fetch(reloadUrl);
if (response.ok) {
const project = await response.json();
projectDefinition = project;
}
}
}
return projectDefinition;
};

const AppPage: () => JSX.Element = () => {
const { setLocale, LL } = useI18nContext();
// Do we have a selected langage stored in the local storage ?
Expand Down Expand Up @@ -466,21 +493,26 @@ const AppPage: () => JSX.Element = () => {
width: '660px',
});
});

// Is there a project in the DB ?
const project = await db.projects.toArray();
// If there is a project, propose to reload it
if (project.length > 0) {
const { date, data } = project[0];
setNiceAlertStore({
show: true,
content: () => <p>{ LL().Alerts.ReloadLastProject(date.toLocaleDateString())}</p>,
confirmCallback: () => {
setGlobalStore({ userHasAddedLayer: true });
reloadFromProjectObject(data);
},
focusOn: 'confirm',
});
// Is there a project in the query string ?
const projectQs = await projectFromQueryString();
if (projectQs) {
reloadFromProjectObject(projectQs);
} else {
// If not, we check if there is a project in the local DB
const projects = await db.projects.toArray();
// If there is a project, propose to reload it
if (projects.length > 0) {
const { date, data } = projects[0];
setNiceAlertStore({
show: true,
content: () => <p>{LL().Alerts.ReloadLastProject(date.toLocaleDateString())}</p>,
confirmCallback: () => {
setGlobalStore({ userHasAddedLayer: true });
reloadFromProjectObject(data);
},
focusOn: 'confirm',
});
}
}
// We only keep the last project in the DB
// so at this point we can delete all projects
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/query-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default function parseQueryString(search: string) {
const args = search.substring(1).split('&');
const argsParsed: { [key: string]: (string | true) } & { 'reload': string } = {
reload: '',
};
let arg;
let kvp;
let key;
for (let i = 0; i < args.length; i += 1) {
arg = args[i];
if (arg.indexOf('=') === -1) {
argsParsed[decodeURIComponent(arg).trim()] = true;
} else {
kvp = arg.split('=');
key = decodeURIComponent(kvp[0]).trim();
// const value = decodeURIComponent(kvp[1]).trim();
argsParsed[key] = decodeURIComponent(kvp[1]).trim();
}
}
return argsParsed;
}

0 comments on commit 3eb0439

Please sign in to comment.