| 1234567891011121314151617181920212223242526272829303132 |
- import type { SWRResponse } from 'swr';
- import useSWRImmutable from 'swr/immutable';
- import { apiv3Get } from '~/client/util/apiv3-client';
- import type {
- ActivityHasTargetPage,
- PopulatedUserActivitiesResult,
- } from '~/interfaces/activity';
- import type { PaginateResult } from '~/interfaces/mongoose-utils';
- export const useSWRxRecentActivity = (
- limit?: number,
- offset?: number,
- targetUserId?: string,
- ): SWRResponse<PaginateResult<ActivityHasTargetPage>, Error> => {
- const shouldFetch = targetUserId && targetUserId.length > 0;
- const key = shouldFetch
- ? ['/user-activities', limit, offset, targetUserId]
- : null;
- const fetcher = ([endpoint, limitParam, offsetParam, targetUserIdParam]) => {
- const promise = apiv3Get<PopulatedUserActivitiesResult>(endpoint, {
- limit: limitParam,
- offset: offsetParam,
- targetUserId: targetUserIdParam,
- });
- return promise.then((result) => result.data.serializedPaginationResult);
- };
- return useSWRImmutable(key, fetcher);
- };
|