Workspace.hooks.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { foldersActions } from '../../stores/reducers/folders/slice';
  2. import { useAppDispatch, useAppSelector } from '../../stores/store';
  3. import { pagesActions } from '../../stores/reducers/pages/slice';
  4. import { workspaceActions } from '../../stores/reducers/workspace/slice';
  5. import { UserBackendService } from '../../stores/effects/user/user_bd_svc';
  6. export const useWorkspace = () => {
  7. const currentUser = useAppSelector((state) => state.currentUser);
  8. const appDispatch = useAppDispatch();
  9. const userBackendService: UserBackendService = new UserBackendService(currentUser.id || 0);
  10. const loadWorkspaceItems = async () => {
  11. try {
  12. const workspaceSettingPB = await userBackendService.getCurrentWorkspace();
  13. const workspace = workspaceSettingPB.workspace;
  14. appDispatch(workspaceActions.updateWorkspace({ id: workspace.id, name: workspace.name }));
  15. appDispatch(foldersActions.clearFolders());
  16. appDispatch(pagesActions.clearPages());
  17. const apps = workspace.views;
  18. for (const app of apps) {
  19. appDispatch(foldersActions.addFolder({ id: app.id, title: app.name }));
  20. const views = app.belongings;
  21. for (const view of views) {
  22. appDispatch(pagesActions.addPage({ folderId: app.id, id: view.id, pageType: view.layout, title: view.name }));
  23. }
  24. }
  25. } catch (e1) {
  26. // create workspace for first start
  27. const workspace = await userBackendService.createWorkspace({ name: 'New Workspace', desc: '' });
  28. appDispatch(workspaceActions.updateWorkspace({ id: workspace.id, name: workspace.name }));
  29. appDispatch(foldersActions.clearFolders());
  30. appDispatch(pagesActions.clearPages());
  31. }
  32. };
  33. return {
  34. loadWorkspaceItems,
  35. };
  36. };