comment.tsx 631 B

12345678910111213141516171819
  1. import useSWR, { SWRResponse } from 'swr';
  2. import { apiGet } from '~/client/util/apiv1-client';
  3. import { ICommentHasIdList } from '../interfaces/comment';
  4. import { Nullable } from '../interfaces/common';
  5. type IResponseComment = {
  6. comments: ICommentHasIdList,
  7. ok: boolean,
  8. }
  9. export const useSWRxPageComment = (pageId: Nullable<string>): SWRResponse<ICommentHasIdList, Error> => {
  10. const shouldFetch: boolean = pageId != null;
  11. return useSWR(
  12. shouldFetch ? ['/comments.get', pageId] : null,
  13. (endpoint, pageId) => apiGet(endpoint, { page_id: pageId }).then((response:IResponseComment) => response.comments),
  14. );
  15. };