attachment.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { useCallback } from 'react';
  2. import type {
  3. IAttachmentHasId, Nullable,
  4. } from '@growi/core';
  5. import {
  6. type SWRResponseWithUtils, withUtils,
  7. } from '@growi/core/dist/swr';
  8. import { Util } from 'reactstrap';
  9. import useSWR, { useSWRConfig } from 'swr';
  10. import { apiPost } from '~/client/util/apiv1-client';
  11. import { apiv3Get } from '~/client/util/apiv3-client';
  12. import { IResAttachmentList } from '~/interfaces/attachment';
  13. type Util = {
  14. remove(body: { attachment_id: string }): Promise<void>
  15. };
  16. type IDataAttachmentList = {
  17. attachments: IAttachmentHasId[]
  18. totalAttachments: number
  19. limit: number
  20. };
  21. export const useSWRxAttachment = (attachmentId: string): SWRResponseWithUtils<Util, IAttachmentHasId, Error> => {
  22. const swrResponse = useSWR(
  23. ['/attachment', attachmentId],
  24. useCallback(async([endpoint, attachmentId]) => {
  25. const params = { attachmentId };
  26. const res = await apiv3Get(endpoint, params);
  27. return res.data.attachment;
  28. }, []),
  29. );
  30. // Utils
  31. const remove = useCallback(async(body: { attachment_id: string }) => {
  32. try {
  33. await apiPost('/attachments.remove', body);
  34. swrResponse.mutate(body.attachment_id);
  35. }
  36. catch (err) {
  37. throw err;
  38. }
  39. }, [swrResponse]);
  40. return withUtils<Util, IAttachmentHasId, Error>(swrResponse, { remove });
  41. };
  42. export const useSWRxAttachments = (pageId?: Nullable<string>, pageNumber?: number): SWRResponseWithUtils<Util, IDataAttachmentList, Error> => {
  43. const { mutate: mutateUseSWRxAttachment } = useSWRConfig();
  44. const shouldFetch = pageId != null && pageNumber != null;
  45. const fetcher = useCallback(async([endpoint, pageId, pageNumber]) => {
  46. const res = await apiv3Get<IResAttachmentList>(endpoint, { pageId, pageNumber });
  47. const resAttachmentList = res.data;
  48. const { paginateResult } = resAttachmentList;
  49. return {
  50. attachments: paginateResult.docs,
  51. totalAttachments: paginateResult.totalDocs,
  52. limit: paginateResult.limit,
  53. };
  54. }, []);
  55. const swrResponse = useSWR(
  56. shouldFetch ? ['/attachment/list', pageId, pageNumber] : null,
  57. fetcher,
  58. );
  59. // Utils
  60. const remove = useCallback(async(body: { attachment_id: string }) => {
  61. const { mutate } = swrResponse;
  62. try {
  63. await apiPost('/attachments.remove', body);
  64. mutate();
  65. // Mutation for rich attachment rendering
  66. mutateUseSWRxAttachment(['/attachment', body.attachment_id], body.attachment_id);
  67. }
  68. catch (err) {
  69. throw err;
  70. }
  71. }, [mutateUseSWRxAttachment, swrResponse]);
  72. return withUtils<Util, IDataAttachmentList, Error>(swrResponse, { remove });
  73. };