/*
 * @Author: 赵伟
 * @Date: 2024-11-06 14:53:37
 * @Description: SessionStorage hook
 */

import SessionStorage from '@/utils/sessionStorage';
import { useEffect, useState } from 'react';

// 读取缓存数据，组件卸载时清除缓存
export function useSessionStorage<T>(key: string, isObject: boolean, initialValue: T) {
  const [storage, setStorage] = useState<T>(initialValue);

  useEffect(() => {
    const res = SessionStorage.getItem(key, isObject);
    if (res) {
      setStorage(res);
    }
    return () => {
      SessionStorage.removeItem(key);
    };
  }, []);

  return [storage];
}
