ContextExtractor.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { FC, useEffect, useState } from 'react';
  2. import { pagePathUtils } from '@growi/core';
  3. import {
  4. useCurrentCreatedAt, useDeleteUsername, useDeletedAt, useHasChildren, useHasDraftOnHackmd, useIsAbleToDeleteCompletely,
  5. useIsDeletable, useIsDeleted, useIsNotCreatable, useIsPageExist, useIsTrashPage, useIsUserPage, useLastUpdateUsername,
  6. useCurrentPageId, usePageIdOnHackmd, usePageUser, useCurrentPagePath, useRevisionCreatedAt, useRevisionId, useRevisionIdHackmdSynced,
  7. useShareLinkId, useShareLinksNumber, useTemplateTagData, useCurrentUpdatedAt, useCreator, useRevisionAuthor, useCurrentUser, useTargetAndAncestors,
  8. useSlackChannels, useNotFoundTargetPathOrId, useIsSearchPage,
  9. } from '../../stores/context';
  10. import {
  11. useIsDeviceSmallerThanMd, useIsDeviceSmallerThanLg,
  12. usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth,
  13. } from '~/stores/ui';
  14. import { IUserUISettings } from '~/interfaces/user-ui-settings';
  15. const { isTrashPage: _isTrashPage } = pagePathUtils;
  16. const jsonNull = 'null';
  17. const ContextExtractorOnce: FC = () => {
  18. const mainContent = document.querySelector('#content-main');
  19. const notFoundContent = document.getElementById('growi-pagetree-not-found-context');
  20. /*
  21. * App Context from DOM
  22. */
  23. const currentUser = JSON.parse(document.getElementById('growi-current-user')?.textContent || jsonNull);
  24. /*
  25. * UserUISettings from DOM
  26. */
  27. const userUISettings: Partial<IUserUISettings> = JSON.parse(document.getElementById('growi-user-ui-settings')?.textContent || jsonNull);
  28. /*
  29. * Page Context from DOM
  30. */
  31. const revisionId = mainContent?.getAttribute('data-page-revision-id');
  32. const path = decodeURI(mainContent?.getAttribute('data-path') || '');
  33. const pageId = mainContent?.getAttribute('data-page-id') || null;
  34. const revisionCreatedAt = +(mainContent?.getAttribute('data-page-revision-created') || '');
  35. // createdAt
  36. const createdAtAttribute = mainContent?.getAttribute('data-page-created-at');
  37. const createdAt: Date | null = (createdAtAttribute != null) ? new Date(createdAtAttribute) : null;
  38. // updatedAt
  39. const updatedAtAttribute = mainContent?.getAttribute('data-page-updated-at');
  40. const updatedAt: Date | null = (updatedAtAttribute != null) ? new Date(updatedAtAttribute) : null;
  41. const deletedAt = mainContent?.getAttribute('data-page-deleted-at') || null;
  42. const isUserPage = JSON.parse(mainContent?.getAttribute('data-page-user') || jsonNull);
  43. const isTrashPage = _isTrashPage(path);
  44. const isDeleted = JSON.parse(mainContent?.getAttribute('data-page-is-deleted') || jsonNull);
  45. const isDeletable = JSON.parse(mainContent?.getAttribute('data-page-is-deletable') || jsonNull);
  46. const isNotCreatable = JSON.parse(mainContent?.getAttribute('data-page-is-not-creatable') || jsonNull);
  47. const isAbleToDeleteCompletely = JSON.parse(mainContent?.getAttribute('data-page-is-able-to-delete-completely') || jsonNull);
  48. const isPageExist = mainContent?.getAttribute('data-page-id') != null;
  49. const pageUser = JSON.parse(mainContent?.getAttribute('data-page-user') || jsonNull);
  50. const hasChildren = JSON.parse(mainContent?.getAttribute('data-page-has-children') || jsonNull);
  51. const templateTagData = mainContent?.getAttribute('data-template-tags') || null;
  52. const shareLinksNumber = mainContent?.getAttribute('data-share-links-number');
  53. const shareLinkId = JSON.parse(mainContent?.getAttribute('data-share-link-id') || jsonNull);
  54. const revisionIdHackmdSynced = mainContent?.getAttribute('data-page-revision-id-hackmd-synced') || null;
  55. const lastUpdateUsername = mainContent?.getAttribute('data-page-last-update-username') || null;
  56. const deleteUsername = mainContent?.getAttribute('data-page-delete-username') || null;
  57. const pageIdOnHackmd = mainContent?.getAttribute('data-page-id-on-hackmd') || null;
  58. const hasDraftOnHackmd = !!mainContent?.getAttribute('data-page-has-draft-on-hackmd');
  59. const creator = JSON.parse(mainContent?.getAttribute('data-page-creator') || jsonNull);
  60. const revisionAuthor = JSON.parse(mainContent?.getAttribute('data-page-revision-author') || jsonNull);
  61. const targetAndAncestors = JSON.parse(document.getElementById('growi-pagetree-target-and-ancestors')?.textContent || jsonNull);
  62. const notFoundTargetPathOrId = JSON.parse(notFoundContent?.getAttribute('data-not-found-target-path-or-id') || jsonNull);
  63. const slackChannels = mainContent?.getAttribute('data-slack-channels') || '';
  64. const isSearchPage = document.getElementById('search-page') != null;
  65. /*
  66. * use static swr
  67. */
  68. // App
  69. useCurrentUser(currentUser);
  70. // UserUISettings
  71. usePreferDrawerModeByUser(userUISettings?.preferDrawerModeByUser);
  72. usePreferDrawerModeOnEditByUser(userUISettings?.preferDrawerModeOnEditByUser);
  73. useSidebarCollapsed(userUISettings?.isSidebarCollapsed);
  74. useCurrentSidebarContents(userUISettings?.currentSidebarContents);
  75. useCurrentProductNavWidth(userUISettings?.currentProductNavWidth);
  76. // Page
  77. useCurrentCreatedAt(createdAt);
  78. useDeleteUsername(deleteUsername);
  79. useDeletedAt(deletedAt);
  80. useHasChildren(hasChildren);
  81. useHasDraftOnHackmd(hasDraftOnHackmd);
  82. useIsAbleToDeleteCompletely(isAbleToDeleteCompletely);
  83. useIsDeletable(isDeletable);
  84. useIsDeleted(isDeleted);
  85. useIsNotCreatable(isNotCreatable);
  86. useIsPageExist(isPageExist);
  87. useIsTrashPage(isTrashPage);
  88. useIsUserPage(isUserPage);
  89. useLastUpdateUsername(lastUpdateUsername);
  90. useCurrentPageId(pageId);
  91. usePageIdOnHackmd(pageIdOnHackmd);
  92. usePageUser(pageUser);
  93. useCurrentPagePath(path);
  94. useRevisionCreatedAt(revisionCreatedAt);
  95. useRevisionId(revisionId);
  96. useRevisionIdHackmdSynced(revisionIdHackmdSynced);
  97. useShareLinkId(shareLinkId);
  98. useShareLinksNumber(shareLinksNumber);
  99. useTemplateTagData(templateTagData);
  100. useCurrentUpdatedAt(updatedAt);
  101. useCreator(creator);
  102. useRevisionAuthor(revisionAuthor);
  103. useTargetAndAncestors(targetAndAncestors);
  104. useNotFoundTargetPathOrId(notFoundTargetPathOrId);
  105. useIsSearchPage(isSearchPage);
  106. // Navigation
  107. usePreferDrawerModeByUser();
  108. usePreferDrawerModeOnEditByUser();
  109. useIsDeviceSmallerThanMd();
  110. // Navigation
  111. usePreferDrawerModeByUser();
  112. usePreferDrawerModeOnEditByUser();
  113. useIsDeviceSmallerThanMd();
  114. // Editor
  115. useSlackChannels(slackChannels);
  116. // SearchResult
  117. useIsDeviceSmallerThanLg();
  118. return null;
  119. };
  120. const ContextExtractor: FC = React.memo(() => {
  121. const [isRunOnce, setRunOnce] = useState(false);
  122. useEffect(() => {
  123. setRunOnce(true);
  124. }, []);
  125. return isRunOnce ? null : <ContextExtractorOnce></ContextExtractorOnce>;
  126. });
  127. export default ContextExtractor;