SidebarNav.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { FC, memo, useCallback } from 'react';
  2. import { scheduleToPutUserUISettings } from '~/client/services/user-ui-settings';
  3. import { SidebarContentsType } from '~/interfaces/ui';
  4. import { useCurrentUser, useIsSharedUser } from '~/stores/context';
  5. import { useCurrentSidebarContents } from '~/stores/ui';
  6. type PrimaryItemProps = {
  7. contents: SidebarContentsType,
  8. label: string,
  9. iconName: string,
  10. onItemSelected: (contents: SidebarContentsType) => void,
  11. }
  12. const PrimaryItem: FC<PrimaryItemProps> = (props: PrimaryItemProps) => {
  13. const {
  14. contents, iconName, onItemSelected,
  15. } = props;
  16. // TODO: migrate from NavigationContainer
  17. const { data: currentContents, mutate } = useCurrentSidebarContents();
  18. const isSelected = contents === currentContents;
  19. const itemSelectedHandler = useCallback(() => {
  20. // const { navigationContainer, onItemSelected } = this.props;
  21. if (onItemSelected != null) {
  22. onItemSelected(contents);
  23. }
  24. mutate(contents, false);
  25. scheduleToPutUserUISettings({ currentSidebarContents: contents });
  26. }, [contents, mutate, onItemSelected]);
  27. return (
  28. <button
  29. type="button"
  30. className={`d-block btn btn-primary ${isSelected ? 'active' : ''}`}
  31. onClick={itemSelectedHandler}
  32. >
  33. <i className="material-icons">{iconName}</i>
  34. </button>
  35. );
  36. };
  37. type SecondaryItemProps = {
  38. label: string,
  39. href: string,
  40. iconName: string,
  41. isBlank?: boolean,
  42. }
  43. const SecondaryItem: FC<SecondaryItemProps> = memo((props: SecondaryItemProps) => {
  44. const { iconName, href, isBlank } = props;
  45. return (
  46. <a href={href} className="d-block btn btn-primary" target={`${isBlank ? '_blank' : ''}`}>
  47. <i className="material-icons">{iconName}</i>
  48. </a>
  49. );
  50. });
  51. type Props = {
  52. onItemSelected: (contents: SidebarContentsType) => void,
  53. }
  54. const SidebarNav: FC<Props> = (props: Props) => {
  55. const { data: isSharedUser } = useIsSharedUser();
  56. const { data: currentUser } = useCurrentUser();
  57. const isAdmin = currentUser?.admin;
  58. const isLoggedIn = currentUser != null;
  59. const { onItemSelected } = props;
  60. return (
  61. <div className="grw-sidebar-nav">
  62. <div className="grw-sidebar-nav-primary-container">
  63. {!isSharedUser && <PrimaryItem contents={SidebarContentsType.CUSTOM} label="Custom Sidebar" iconName="code" onItemSelected={onItemSelected} />}
  64. {!isSharedUser && <PrimaryItem contents={SidebarContentsType.RECENT} label="Recent Changes" iconName="update" onItemSelected={onItemSelected} />}
  65. {!isSharedUser && <PrimaryItem contents={SidebarContentsType.TREE} label="Page Tree" iconName="format_list_bulleted" onItemSelected={onItemSelected} />}
  66. {/* <PrimaryItem id="tag" label="Tags" iconName="icon-tag" /> */}
  67. {/* <PrimaryItem id="favorite" label="Favorite" iconName="icon-star" /> */}
  68. </div>
  69. <div className="grw-sidebar-nav-secondary-container">
  70. {isAdmin && <SecondaryItem label="Admin" iconName="settings" href="/admin" />}
  71. {isLoggedIn && <SecondaryItem label="Draft" iconName="file_copy" href="/me/drafts" />}
  72. <SecondaryItem label="Help" iconName="help" href="https://docs.growi.org" isBlank />
  73. <SecondaryItem label="Trash" iconName="delete" href="/trash" />
  74. </div>
  75. </div>
  76. );
  77. };
  78. export default SidebarNav;