hooks.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { useCallback, useState } from 'react';
  2. import type { PageGrant, IGrantedGroup } from '@growi/core';
  3. import { useRouter } from 'next/router';
  4. import { createPage, exist } from '~/client/services/page-operation';
  5. import { toastError } from '~/client/util/toastr';
  6. import { EditorMode, useEditorMode } from '~/stores/ui';
  7. export const useOnNewButtonClicked = (
  8. currentPagePath?: string,
  9. currentPageGrant?: PageGrant,
  10. currentPageGrantedGroups?: IGrantedGroup[],
  11. isLoading?: boolean,
  12. ): {
  13. onClickHandler: () => Promise<void>,
  14. isPageCreating: boolean
  15. } => {
  16. const router = useRouter();
  17. const [isPageCreating, setIsPageCreating] = useState(false);
  18. const { mutate: mutateEditorMode } = useEditorMode();
  19. const onClickHandler = useCallback(async() => {
  20. if (isLoading) return;
  21. try {
  22. setIsPageCreating(true);
  23. /**
  24. * !! NOTICE !! - Verification of page createable or not is checked on the server side.
  25. * since the new page path is not generated on the client side.
  26. * need shouldGeneratePath flag.
  27. */
  28. const shouldUseRootPath = currentPagePath == null || currentPageGrant == null;
  29. const parentPath = shouldUseRootPath
  30. ? '/'
  31. : currentPagePath;
  32. const params = {
  33. isSlackEnabled: false,
  34. slackChannels: '',
  35. grant: shouldUseRootPath ? 1 : currentPageGrant,
  36. grantUserGroupIds: shouldUseRootPath ? undefined : currentPageGrantedGroups,
  37. shouldGeneratePath: true,
  38. };
  39. // !! NOTICE !! - if shouldGeneratePath is flagged, send the parent page path
  40. const response = await createPage(parentPath, '', params);
  41. await router.push(`/${response.page.id}#edit`);
  42. mutateEditorMode(EditorMode.Editor);
  43. }
  44. catch (err) {
  45. toastError(err);
  46. }
  47. finally {
  48. setIsPageCreating(false);
  49. }
  50. }, [currentPageGrant, currentPageGrantedGroups, currentPagePath, isLoading, mutateEditorMode, router]);
  51. return { onClickHandler, isPageCreating };
  52. };
  53. export const useOnTodaysButtonClicked = (
  54. todaysPath: string | null,
  55. ): {
  56. onClickHandler: () => Promise<void>,
  57. isPageCreating: boolean
  58. } => {
  59. const router = useRouter();
  60. const [isPageCreating, setIsPageCreating] = useState(false);
  61. const { mutate: mutateEditorMode } = useEditorMode();
  62. const onClickHandler = useCallback(async() => {
  63. if (todaysPath == null) {
  64. return;
  65. }
  66. try {
  67. setIsPageCreating(true);
  68. // TODO: get grant, grantUserGroupId data from parent page
  69. // https://redmine.weseek.co.jp/issues/133892
  70. const params = {
  71. isSlackEnabled: false,
  72. slackChannels: '',
  73. grant: 4,
  74. };
  75. const res = await exist(JSON.stringify([todaysPath]));
  76. if (!res.pages[todaysPath]) {
  77. await createPage(todaysPath, '', params);
  78. }
  79. await router.push(`${todaysPath}#edit`);
  80. mutateEditorMode(EditorMode.Editor);
  81. }
  82. catch (err) {
  83. toastError(err);
  84. }
  85. finally {
  86. setIsPageCreating(false);
  87. }
  88. }, [mutateEditorMode, router, todaysPath]);
  89. return { onClickHandler, isPageCreating };
  90. };