2023-01-29 09:42:45 +09:00
|
|
|
import localForage from "localforage";
|
|
|
|
import { useMemo } from "react";
|
2023-06-24 09:52:21 +09:00
|
|
|
import { INDEXEDDB_DB_APP_NAME, INDEXEDDB_DB_NAME } from "../const";
|
2023-01-29 09:42:45 +09:00
|
|
|
|
2023-03-09 06:47:11 +09:00
|
|
|
export type UseIndexedDBProps = {
|
2023-06-24 09:52:21 +09:00
|
|
|
clientType: null
|
2023-03-09 06:47:11 +09:00
|
|
|
}
|
2023-01-29 09:42:45 +09:00
|
|
|
export type IndexedDBState = {
|
|
|
|
dummy: string
|
|
|
|
}
|
|
|
|
export type IndexedDBStateAndMethod = IndexedDBState & {
|
|
|
|
setItem: (key: string, value: unknown) => Promise<void>,
|
|
|
|
getItem: (key: string) => Promise<unknown>
|
|
|
|
removeItem: (key: string) => Promise<void>
|
2023-04-30 16:53:15 +09:00
|
|
|
removeDB: () => Promise<void>
|
2023-01-29 09:42:45 +09:00
|
|
|
}
|
|
|
|
|
2023-03-09 06:47:11 +09:00
|
|
|
export const useIndexedDB = (props: UseIndexedDBProps): IndexedDBStateAndMethod => {
|
2023-04-11 00:21:17 +09:00
|
|
|
const clientType = props.clientType || "default"
|
2023-01-29 09:42:45 +09:00
|
|
|
localForage.config({
|
|
|
|
driver: localForage.INDEXEDDB,
|
|
|
|
name: INDEXEDDB_DB_APP_NAME,
|
|
|
|
version: 1.0,
|
2023-04-11 00:21:17 +09:00
|
|
|
storeName: `${INDEXEDDB_DB_NAME}`,
|
2023-01-29 09:42:45 +09:00
|
|
|
description: 'appStorage'
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
const setItem = useMemo(() => {
|
|
|
|
return async (key: string, value: unknown) => {
|
2023-04-11 00:21:17 +09:00
|
|
|
const clientKey = `${clientType}_${key}`
|
|
|
|
await localForage.setItem(clientKey, value)
|
2023-01-29 09:42:45 +09:00
|
|
|
}
|
2023-04-11 00:21:17 +09:00
|
|
|
}, [props.clientType])
|
|
|
|
|
2023-01-29 09:42:45 +09:00
|
|
|
const getItem = useMemo(() => {
|
|
|
|
return async (key: string) => {
|
2023-04-11 00:21:17 +09:00
|
|
|
const clientKey = `${clientType}_${key}`
|
|
|
|
return await localForage.getItem(clientKey)
|
2023-01-29 09:42:45 +09:00
|
|
|
}
|
2023-04-11 00:21:17 +09:00
|
|
|
}, [props.clientType])
|
2023-01-29 09:42:45 +09:00
|
|
|
|
|
|
|
const removeItem = useMemo(() => {
|
|
|
|
return async (key: string) => {
|
2023-04-11 00:21:17 +09:00
|
|
|
const clientKey = `${clientType}_${key}`
|
2023-04-30 16:53:15 +09:00
|
|
|
console.log("remove key:", clientKey)
|
2023-04-11 00:21:17 +09:00
|
|
|
return await localForage.removeItem(clientKey)
|
2023-01-29 09:42:45 +09:00
|
|
|
}
|
2023-04-11 00:21:17 +09:00
|
|
|
}, [props.clientType])
|
2023-01-29 09:42:45 +09:00
|
|
|
|
2023-04-30 16:53:15 +09:00
|
|
|
const removeDB = useMemo(() => {
|
|
|
|
return async () => {
|
|
|
|
const keys = await localForage.keys()
|
|
|
|
for (const key of keys) {
|
|
|
|
console.log("remove key:", key)
|
|
|
|
await localForage.removeItem(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [props.clientType])
|
|
|
|
|
2023-01-29 09:42:45 +09:00
|
|
|
|
|
|
|
return {
|
|
|
|
dummy: "",
|
|
|
|
setItem,
|
|
|
|
getItem,
|
|
|
|
removeItem,
|
2023-04-30 16:53:15 +09:00
|
|
|
removeDB
|
2023-01-29 09:42:45 +09:00
|
|
|
}
|
|
|
|
}
|