From aa992f43b529f9c56f805b973cae249ed4848142 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Mon, 26 Dec 2022 20:40:02 +0200 Subject: [PATCH] Read workout from url#hash when provided --- src/storage.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/storage.ts b/src/storage.ts index cb34499..e47aa91 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -13,9 +13,21 @@ FreeRide: 20:00 Cooldown: 10:00 70%..30% `; +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"); + return !text || text.trim() === "" ? undefined : text; +}; + export const loadWorkout = (): string => { - const text = localStorage.getItem("workout-editor-text"); - return (!text || text.trim() === "") ? defaultWorkout : text; + return loadFromUrlHash() ?? loadFromLocalStorage() ?? defaultWorkout; }; export const saveWorkout = (text: string) => localStorage.setItem("workout-editor-text", text);