This commit is contained in:
parent
7959fe85cc
commit
b9485e4097
32 changed files with 4692 additions and 65 deletions
|
|
@ -2,10 +2,33 @@ import { useCallback, useEffect, useState } from 'react';
|
|||
import type { UrlState } from './types';
|
||||
import { decodeState, encodeState } from './url-state';
|
||||
|
||||
const readLocation = () => decodeState(window.location.search || window.location.hash);
|
||||
export const PROJECT_STORAGE_KEY = 'floor-planner.project';
|
||||
|
||||
const readLocation = () => {
|
||||
// The landing-page CTA opens the last project; it is not a project override.
|
||||
const link =
|
||||
window.location.search === '?planner' ? '' : window.location.search || window.location.hash;
|
||||
if (link && link !== '?' && link !== '#') return decodeState(link);
|
||||
try {
|
||||
const saved = decodeState(window.localStorage.getItem(PROJECT_STORAGE_KEY) ?? '');
|
||||
if (!saved.error) return saved;
|
||||
} catch {
|
||||
// Browser privacy settings can make localStorage unavailable.
|
||||
}
|
||||
return decodeState('');
|
||||
};
|
||||
|
||||
const saveLocal = (query: string) => {
|
||||
try {
|
||||
window.localStorage.setItem(PROJECT_STORAGE_KEY, query);
|
||||
} catch {
|
||||
// Keep URL persistence working if storage is blocked or full.
|
||||
}
|
||||
};
|
||||
|
||||
const queryUrl = (query: string) => `${window.location.pathname}${query}`;
|
||||
|
||||
/** Readable query parameters are the durable source of truth. */
|
||||
/** Persist every parameter in the URL and locally; explicit links take precedence. */
|
||||
export function useProject() {
|
||||
const [loaded, setLoaded] = useState(readLocation);
|
||||
useEffect(() => {
|
||||
|
|
@ -18,6 +41,7 @@ export function useProject() {
|
|||
const query = encodeState(next.state);
|
||||
if (query !== window.location.search || window.location.hash)
|
||||
window.history.replaceState(null, '', queryUrl(query));
|
||||
saveLocal(query);
|
||||
} catch (error) {
|
||||
next.error = error instanceof Error ? error.message : 'This plan could not be saved.';
|
||||
}
|
||||
|
|
@ -39,6 +63,7 @@ export function useProject() {
|
|||
const query = encodeState(next);
|
||||
if (query !== window.location.search || window.location.hash)
|
||||
window.history[milestone ? 'pushState' : 'replaceState'](null, '', queryUrl(query));
|
||||
saveLocal(query);
|
||||
const canonical = decodeState(query).state;
|
||||
if (next.project === current.state.project) canonical.project = current.state.project;
|
||||
else {
|
||||
|
|
|
|||
Loading…
Reference in a new issue