123456789101112131415161718192021222324252627282930313233343536373839 |
- import { STORAGE_TYPE } from '@/Frameworks/SysStorage';
- const useStorage = (storage: Storage) => {
- const setItem = <T>(key: STORAGE_TYPE, value: T) => {
- storage.setItem(key, value ? JSON.stringify(value) : '');
- };
- const getItem = (key: STORAGE_TYPE) => {
- let value: string = storage.getItem(key) || '';
- try {
- value = JSON.parse(value);
- return value;
- } catch {
- return value;
- }
- };
- const removeItem = (key: STORAGE_TYPE) => {
- storage.removeItem(key);
- };
- const clearAll = () => {
- for (const key in storage) {
- if (key) {
- storage.removeItem(key);
- }
- }
- };
- return {
- setItem,
- getItem,
- removeItem,
- clearAll,
- };
- };
- const SessionService = useStorage(window.sessionStorage || sessionStorage);
- const LocalService = useStorage(window.localStorage || sessionStorage);
- export { SessionService, LocalService };
|