-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
321 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
export type StorageKey = { | ||
version: string; | ||
name: string; | ||
}; | ||
|
||
export const createStorageDriver = (storage: Storage) => { | ||
const get = <V>(key: StorageKey): V | null => { | ||
const value = storage.getItem(key.name); | ||
if (!value) { | ||
return null; | ||
} | ||
try { | ||
const result = JSON.parse(value); | ||
if (result?.version !== key.version) { | ||
remove(key); | ||
return null; | ||
} | ||
return result?.state; | ||
} catch (e) { | ||
// todo: log this | ||
return null; | ||
} | ||
}; | ||
|
||
const set = (key: StorageKey, value: any) => { | ||
storage.setItem( | ||
key.name, | ||
JSON.stringify({ | ||
state: value, | ||
version: key.version, | ||
}), | ||
); | ||
}; | ||
|
||
const remove = (key: StorageKey) => { | ||
storage.removeItem(key.name); | ||
}; | ||
|
||
return { | ||
get, | ||
set, | ||
remove, | ||
}; | ||
}; | ||
|
||
export type StorageDriver = ReturnType<typeof createStorageDriver>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { createStorageDriver } from "src/lib/storage/create-storage-driver.ts"; | ||
|
||
export const localStorageDriver = createStorageDriver(localStorage); | ||
|
||
export const { | ||
get: getFromLocalStorage, | ||
set: setToLocalStorage, | ||
remove: removeFromLocalStorage, | ||
} = localStorageDriver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { createStorageDriver } from "src/lib/storage/create-storage-driver.ts"; | ||
|
||
const memoryStorageMap = new Map(); | ||
|
||
export const memoryStorage: Storage = { | ||
getItem: (storageKey: string) => { | ||
return memoryStorageMap.get(storageKey); | ||
}, | ||
setItem(key: string, value: string) { | ||
return memoryStorageMap.set(key, value); | ||
}, | ||
removeItem(key: string) { | ||
memoryStorageMap.delete(key); | ||
}, | ||
clear() { | ||
memoryStorageMap.clear(); | ||
}, | ||
get length() { | ||
return memoryStorageMap.size; | ||
}, | ||
key(index: number) { | ||
return [...memoryStorageMap.keys()][index]; | ||
}, | ||
}; | ||
|
||
export const memoryStorageDriver = createStorageDriver(memoryStorage); | ||
|
||
export const { | ||
get: getFromMemoryStorage, | ||
set: setToMemoryStorage, | ||
remove: removeFromMemoryStorage, | ||
} = memoryStorageDriver; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { TabModel, ValidTabMeta } from "src/lib/tabs/tabs.types.ts"; | ||
import { | ||
StorageDriver, | ||
StorageKey, | ||
} from "src/lib/storage/create-storage-driver.ts"; | ||
import { useCallback } from "react"; | ||
|
||
export const usePersistTabs = <Meta extends ValidTabMeta = ValidTabMeta>({ | ||
storageKey, | ||
storage, | ||
}: { | ||
storageKey: StorageKey; | ||
storage: StorageDriver; | ||
}) => { | ||
return { | ||
getTabsFromStorage: useCallback( | ||
() => storage.get<TabModel<Meta>[]>(storageKey) || [], | ||
[storageKey, storage], | ||
), | ||
persistTabs: useCallback( | ||
(tabs: TabModel<Meta>[]) => { | ||
const onUnload = () => { | ||
// save on window close | ||
// it doesn't make sense for memory storage | ||
storage.set(storageKey, tabs); | ||
}; | ||
window.addEventListener("unload", onUnload); | ||
return () => { | ||
window.removeEventListener("unload", onUnload); | ||
// save on unmount | ||
storage.set(storageKey, tabs); | ||
}; | ||
}, | ||
[storageKey, storage], | ||
), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.