bookmark.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import type { IUser, IPageHasId } from '@growi/core';
  2. import useSWR, { SWRResponse } from 'swr';
  3. import useSWRImmutable from 'swr/immutable';
  4. import useSWRMutation, { type SWRMutationResponse } from 'swr/mutation';
  5. import { apiv3Get } from '../client/util/apiv3-client';
  6. import { IBookmarkInfo } from '../interfaces/bookmark-info';
  7. import { useCurrentUser } from './context';
  8. export const useSWRxBookmarkedUsers = (pageId: string | null): SWRResponse<IUser[], Error> => {
  9. return useSWR(
  10. pageId != null ? `/bookmarks/info?pageId=${pageId}` : null,
  11. endpoint => apiv3Get<IBookmarkInfo>(endpoint).then(response => response.data.bookmarkedUsers),
  12. );
  13. };
  14. export const useSWRxUserBookmarks = (userId: string | null): SWRResponse<IPageHasId[], Error> => {
  15. return useSWRImmutable(
  16. userId != null ? `/bookmarks/${userId}` : null,
  17. endpoint => apiv3Get(endpoint).then((response) => {
  18. const { userRootBookmarks } = response.data;
  19. return userRootBookmarks.map((item) => {
  20. return {
  21. ...item.page,
  22. };
  23. });
  24. }),
  25. );
  26. };
  27. export const useSWRMUTxCurrentUserBookmarks = (): SWRMutationResponse<IPageHasId[], Error> => {
  28. const { data: currentUser } = useCurrentUser();
  29. return useSWRMutation(
  30. currentUser != null ? `/bookmarks/${currentUser?._id}` : null,
  31. endpoint => apiv3Get(endpoint).then((response) => {
  32. const { userRootBookmarks } = response.data;
  33. return userRootBookmarks.map((item) => {
  34. return {
  35. ...item.page,
  36. };
  37. });
  38. }),
  39. );
  40. };