Read workout from url#hash when provided

This commit is contained in:
Rene Saarsoo 2022-12-26 20:40:02 +02:00
parent 0638b04921
commit aa992f43b5
1 changed files with 14 additions and 2 deletions

View File

@ -13,9 +13,21 @@ FreeRide: 20:00
Cooldown: 10:00 70%..30% Cooldown: 10:00 70%..30%
`; `;
export const loadWorkout = (): string => { const loadFromUrlHash = (): string | undefined => {
if (document.location.hash) {
// Skip the leading '#' when decoding
return decodeURIComponent(document.location.hash.slice(1));
}
return undefined;
};
const loadFromLocalStorage = (): string | undefined => {
const text = localStorage.getItem("workout-editor-text"); const text = localStorage.getItem("workout-editor-text");
return (!text || text.trim() === "") ? defaultWorkout : text; return !text || text.trim() === "" ? undefined : text;
};
export const loadWorkout = (): string => {
return loadFromUrlHash() ?? loadFromLocalStorage() ?? defaultWorkout;
}; };
export const saveWorkout = (text: string) => localStorage.setItem("workout-editor-text", text); export const saveWorkout = (text: string) => localStorage.setItem("workout-editor-text", text);