FolderItem.hooks.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { foldersActions, IFolder } from '../../../stores/reducers/folders/slice';
  2. import { useEffect, useState } from 'react';
  3. import { useAppDispatch, useAppSelector } from '../../../stores/store';
  4. import { IPage, pagesActions } from '../../../stores/reducers/pages/slice';
  5. import { ViewLayoutTypePB } from '../../../../services/backend';
  6. import { AppBackendService } from '../../../stores/effects/folder/app/app_bd_svc';
  7. import { WorkspaceBackendService } from '../../../stores/effects/folder/workspace/workspace_bd_svc';
  8. import { useError } from '../../error/Error.hooks';
  9. const initialFolderHeight = 40;
  10. const initialPageHeight = 40;
  11. const animationDuration = 500;
  12. export const useFolderEvents = (folder: IFolder, pages: IPage[]) => {
  13. const appDispatch = useAppDispatch();
  14. const [showPages, setShowPages] = useState(false);
  15. const [showFolderOptions, setShowFolderOptions] = useState(false);
  16. const [showNewPageOptions, setShowNewPageOptions] = useState(false);
  17. const [showRenamePopup, setShowRenamePopup] = useState(false);
  18. const [folderHeight, setFolderHeight] = useState(`${initialFolderHeight}px`);
  19. const workspace = useAppSelector((state) => state.workspace);
  20. const appBackendService = new AppBackendService(folder.id);
  21. const workspaceBackendService = new WorkspaceBackendService(workspace.id || '');
  22. const error = useError();
  23. useEffect(() => {
  24. if (showPages) {
  25. setFolderHeight(`${initialFolderHeight + pages.length * initialPageHeight}px`);
  26. }
  27. }, [pages]);
  28. const onFolderNameClick = () => {
  29. if (showPages) {
  30. setFolderHeight(`${initialFolderHeight}px`);
  31. } else {
  32. setFolderHeight(`${initialFolderHeight + pages.length * initialPageHeight}px`);
  33. }
  34. setShowPages(!showPages);
  35. };
  36. const onFolderOptionsClick = () => {
  37. setShowFolderOptions(!showFolderOptions);
  38. };
  39. const onNewPageClick = () => {
  40. setShowNewPageOptions(!showNewPageOptions);
  41. };
  42. const startFolderRename = () => {
  43. closePopup();
  44. setShowRenamePopup(true);
  45. };
  46. const changeFolderTitle = async (newTitle: string) => {
  47. try {
  48. await appBackendService.update({ name: newTitle });
  49. appDispatch(foldersActions.renameFolder({ id: folder.id, newTitle }));
  50. } catch (e: any) {
  51. error.showError(e?.message);
  52. }
  53. };
  54. const closeRenamePopup = () => {
  55. setShowRenamePopup(false);
  56. };
  57. const deleteFolder = async () => {
  58. closePopup();
  59. try {
  60. await appBackendService.delete();
  61. appDispatch(foldersActions.deleteFolder({ id: folder.id }));
  62. } catch (e: any) {
  63. error.showError(e?.message);
  64. }
  65. };
  66. const duplicateFolder = async () => {
  67. closePopup();
  68. try {
  69. const newApp = await workspaceBackendService.createApp({
  70. name: folder.title,
  71. });
  72. appDispatch(foldersActions.addFolder({ id: newApp.id, title: folder.title }));
  73. } catch (e: any) {
  74. error.showError(e?.message);
  75. }
  76. };
  77. const closePopup = () => {
  78. setShowFolderOptions(false);
  79. setShowNewPageOptions(false);
  80. };
  81. const onAddNewDocumentPage = async () => {
  82. closePopup();
  83. try {
  84. const newView = await appBackendService.createView({
  85. name: 'New Document 1',
  86. layoutType: ViewLayoutTypePB.Document,
  87. });
  88. appDispatch(
  89. pagesActions.addPage({
  90. folderId: folder.id,
  91. pageType: ViewLayoutTypePB.Document,
  92. title: newView.name,
  93. id: newView.id,
  94. })
  95. );
  96. } catch (e: any) {
  97. error.showError(e?.message);
  98. }
  99. };
  100. const onAddNewBoardPage = async () => {
  101. closePopup();
  102. try {
  103. const newView = await appBackendService.createView({
  104. name: 'New Board 1',
  105. layoutType: ViewLayoutTypePB.Board,
  106. });
  107. appDispatch(
  108. pagesActions.addPage({
  109. folderId: folder.id,
  110. pageType: ViewLayoutTypePB.Board,
  111. title: newView.name,
  112. id: newView.id,
  113. })
  114. );
  115. } catch (e: any) {
  116. error.showError(e?.message);
  117. }
  118. };
  119. const onAddNewGridPage = async () => {
  120. closePopup();
  121. try {
  122. const newView = await appBackendService.createView({
  123. name: 'New Grid 1',
  124. layoutType: ViewLayoutTypePB.Grid,
  125. });
  126. appDispatch(
  127. pagesActions.addPage({
  128. folderId: folder.id,
  129. pageType: ViewLayoutTypePB.Grid,
  130. title: newView.name,
  131. id: newView.id,
  132. })
  133. );
  134. } catch (e: any) {
  135. error.showError(e?.message);
  136. }
  137. };
  138. return {
  139. showPages,
  140. onFolderNameClick,
  141. showFolderOptions,
  142. onFolderOptionsClick,
  143. showNewPageOptions,
  144. onNewPageClick,
  145. showRenamePopup,
  146. startFolderRename,
  147. changeFolderTitle,
  148. closeRenamePopup,
  149. deleteFolder,
  150. duplicateFolder,
  151. onAddNewDocumentPage,
  152. onAddNewBoardPage,
  153. onAddNewGridPage,
  154. closePopup,
  155. folderHeight,
  156. animationDuration,
  157. };
  158. };