2023-10-23 08:54:26 +00:00
|
|
|
"use client";
|
|
|
|
|
2023-10-23 09:06:58 +00:00
|
|
|
import { createStore, del, get, set, UseStore } from "idb-keyval";
|
2023-10-23 08:54:26 +00:00
|
|
|
import { StateStorage } from "zustand/middleware";
|
|
|
|
|
2023-10-23 09:06:58 +00:00
|
|
|
let storage: UseStore | undefined = undefined;
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
storage = createStore("scoresaber-reloaded", "scoresaber");
|
|
|
|
}
|
2023-10-23 09:03:57 +00:00
|
|
|
|
2023-10-23 08:54:26 +00:00
|
|
|
export const IDBStorage: StateStorage = {
|
|
|
|
getItem: async (name: string): Promise<string | null> => {
|
|
|
|
//console.log(name, "has been retrieved");
|
2023-10-23 09:03:57 +00:00
|
|
|
|
|
|
|
return (await get(name, storage)) || null;
|
2023-10-23 08:54:26 +00:00
|
|
|
},
|
|
|
|
setItem: async (name: string, value: string): Promise<void> => {
|
|
|
|
//console.log(name, "with value", value, "has been saved");
|
2023-10-23 09:03:57 +00:00
|
|
|
await set(name, value, storage);
|
2023-10-23 08:54:26 +00:00
|
|
|
},
|
|
|
|
removeItem: async (name: string): Promise<void> => {
|
|
|
|
//console.log(name, "has been deleted");
|
2023-10-23 09:03:57 +00:00
|
|
|
await del(name, storage);
|
2023-10-23 08:54:26 +00:00
|
|
|
},
|
|
|
|
};
|