recent-activity.ts 1016 B

1234567891011121314151617181920212223242526272829303132
  1. import type { SWRResponse } from 'swr';
  2. import useSWRImmutable from 'swr/immutable';
  3. import { apiv3Get } from '~/client/util/apiv3-client';
  4. import type {
  5. ActivityHasTargetPage,
  6. PopulatedUserActivitiesResult,
  7. } from '~/interfaces/activity';
  8. import type { PaginateResult } from '~/interfaces/mongoose-utils';
  9. export const useSWRxRecentActivity = (
  10. limit?: number,
  11. offset?: number,
  12. targetUserId?: string,
  13. ): SWRResponse<PaginateResult<ActivityHasTargetPage>, Error> => {
  14. const shouldFetch = targetUserId && targetUserId.length > 0;
  15. const key = shouldFetch
  16. ? ['/user-activities', limit, offset, targetUserId]
  17. : null;
  18. const fetcher = ([endpoint, limitParam, offsetParam, targetUserIdParam]) => {
  19. const promise = apiv3Get<PopulatedUserActivitiesResult>(endpoint, {
  20. limit: limitParam,
  21. offset: offsetParam,
  22. targetUserId: targetUserIdParam,
  23. });
  24. return promise.then((result) => result.data.serializedPaginationResult);
  25. };
  26. return useSWRImmutable(key, fetcher);
  27. };