useLogout.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { useMemo } from 'react';
  2. import { COMMONINFO_KEY, TOKEN_KEY, USERINFO_KEY } from '@common/constants';
  3. import { getAllKeys, removeAllItems } from '@common/StorageHelper.ts';
  4. import { useAuth } from '@common/contexts/useAuth.ts';
  5. /**
  6. * @description: 处理退出登录的 hook,可以合并处理退出登录时的一些操作
  7. */
  8. const useLogout = (): {
  9. actions: {
  10. logout: () => Promise<void>;
  11. };
  12. } => {
  13. const {
  14. actions: { logout },
  15. } = useAuth();
  16. const actions = useMemo(
  17. () => ({
  18. logout: async () => {
  19. // ImManager.getInstance().logout();
  20. // PushManager.getInstance().logout();
  21. // 退出登录将旧模块中的内容清空
  22. // --- 这部分内容等旧模块不再使用后就可以删除了 ---
  23. const keys = await getAllKeys();
  24. console.log('>>>>>>>>', keys);
  25. // 保留的 key
  26. const keysToKeep = [
  27. COMMONINFO_KEY,
  28. TOKEN_KEY,
  29. USERINFO_KEY,
  30. 'follow_first',
  31. ];
  32. // 过滤掉需要保留的 key
  33. const keysToDelete = keys.filter(key => !keysToKeep.includes(key));
  34. // 删除剩余的 key
  35. await removeAllItems(keysToDelete);
  36. // --- 这部分内容等旧模块不再使用后就可以删除了 ---
  37. await logout();
  38. },
  39. }),
  40. // eslint-disable-next-line react-hooks/exhaustive-deps
  41. [logout],
  42. );
  43. return { actions: actions };
  44. };
  45. export { useLogout };