user.tsx 1.1 KB

123456789101112131415161718192021222324252627
  1. import useSWR, { SWRResponse } from 'swr';
  2. import { apiv3Get } from '~/client/util/apiv3-client';
  3. import { IUserHasId } from '~/interfaces/user';
  4. import { checkAndUpdateImageUrlCached } from '~/stores/middlewares/user';
  5. import { apiGet } from '../client/util/apiv1-client';
  6. export const useSWRxLikerList = (likerIds: string[] = []): SWRResponse<IUserHasId[], Error> => {
  7. const shouldFetch = likerIds.length > 0;
  8. return useSWR(shouldFetch ? ['/users.list', [...likerIds].join(',')] : null, (endpoint:string, userIds:string) => {
  9. return apiGet(endpoint, { user_ids: userIds }).then((response:any) => response.users);
  10. });
  11. };
  12. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  13. export const useSWRxUsersList = <Data, Error>(userIds: string[]): SWRResponse<IUserHasId[], Error> => {
  14. const distinctUserIds = userIds.length > 0 ? Array.from(new Set(userIds)).sort() : [];
  15. return useSWR(
  16. distinctUserIds.length > 0 ? ['/users/list', distinctUserIds] : null,
  17. (endpoint, userIds) => apiv3Get(endpoint, { userIds: userIds.join(',') }).then((response) => {
  18. return response.data.users;
  19. }),
  20. { use: [checkAndUpdateImageUrlCached] },
  21. );
  22. };