use-post-get-conversation-list.tsx 1.0 KB

1234567891011121314151617181920212223242526272829
  1. import { useQuery } from '@tanstack/react-query';
  2. import { chatQueryKeys } from '../chat-query-keys';
  3. import { chatApi, type PostGetChatsListReturn } from '../chat-api';
  4. import type { BaseAxiosError } from '../../../../types';
  5. import { storage, StoreType } from 'src/storage';
  6. export const usePostGetChatsListQuery = (token: string, archive: 0 | 1, enabled: boolean) => {
  7. return useQuery<PostGetChatsListReturn, BaseAxiosError>({
  8. queryKey: chatQueryKeys.getChatsList(token, archive),
  9. queryFn: async () => {
  10. const response = await chatApi.getChatsList(token, archive);
  11. storage.set('chats', JSON.stringify(response.data.conversations));
  12. return response.data;
  13. },
  14. enabled,
  15. initialData: () => {
  16. try {
  17. const storedChats = storage.get('chats', StoreType.STRING) as string;
  18. return storedChats
  19. ? ({ conversations: JSON.parse(storedChats), result: 'OK' } as PostGetChatsListReturn)
  20. : undefined;
  21. } catch {
  22. return undefined;
  23. }
  24. }
  25. });
  26. };