bookmark.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { IUserHasId } from '@growi/core';
  2. import { SWRResponse } from 'swr';
  3. import useSWRImmutable from 'swr/immutable';
  4. import { IPageHasId } from '~/interfaces/page';
  5. import { apiv3Get } from '../client/util/apiv3-client';
  6. import { IBookmarkInfo } from '../interfaces/bookmark-info';
  7. import { useCurrentUser } from './context';
  8. export const useSWRBookmarkInfo = (pageId: string | null | undefined): SWRResponse<IBookmarkInfo, Error> => {
  9. return useSWRImmutable(
  10. pageId != null ? `/bookmarks/info?pageId=${pageId}` : null,
  11. endpoint => apiv3Get(endpoint).then((response) => {
  12. return {
  13. sumOfBookmarks: response.data.sumOfBookmarks,
  14. isBookmarked: response.data.isBookmarked,
  15. bookmarkedUsers: response.data.bookmarkedUsers,
  16. };
  17. }),
  18. );
  19. };
  20. export const useSWRxCurrentUserBookmarks = (): SWRResponse<IPageHasId[], Error> => {
  21. const { data: currentUser } = useCurrentUser();
  22. const user = currentUser as IUserHasId;
  23. return useSWRImmutable(
  24. currentUser != null ? `/bookmarks/${user._id}` : null,
  25. endpoint => apiv3Get(endpoint).then((response) => {
  26. const { userRootBookmarks } = response.data;
  27. return userRootBookmarks.map((item) => {
  28. return {
  29. ...item.page,
  30. };
  31. });
  32. }),
  33. );
  34. };