Просмотр исходного кода

refactor for SidebarContentsType enum

Yuki Takei 4 лет назад
Родитель
Сommit
54f61fd13e

+ 2 - 2
packages/app/src/components/Sidebar/SidebarContents.tsx

@@ -1,6 +1,6 @@
 import React, { FC } from 'react';
 
-import { SidebarContents as SidebarContentType } from '~/interfaces/ui';
+import { SidebarContentsType } from '~/interfaces/ui';
 import { useCurrentSidebarContents } from '~/stores/ui';
 
 import RecentChanges from './RecentChanges';
@@ -15,7 +15,7 @@ const SidebarContents: FC<Props> = (props: Props) => {
 
   let Contents;
   switch (currentSidebarContents) {
-    case SidebarContentType.RECENT:
+    case SidebarContentsType.RECENT:
       Contents = RecentChanges;
       break;
     default:

+ 6 - 6
packages/app/src/components/Sidebar/SidebarNav.tsx

@@ -1,15 +1,15 @@
 import React, { FC, useCallback } from 'react';
 
-import { SidebarContents } from '~/interfaces/ui';
+import { SidebarContentsType } from '~/interfaces/ui';
 import { useCurrentUser, useIsSharedUser } from '~/stores/context';
 import { useCurrentSidebarContents, putUserUISettings } from '~/stores/ui';
 
 
 type PrimaryItemProps = {
-  contents: SidebarContents,
+  contents: SidebarContentsType,
   label: string,
   iconName: string,
-  onItemSelected: (contents: SidebarContents) => void,
+  onItemSelected: (contents: SidebarContentsType) => void,
 }
 
 const PrimaryItem: FC<PrimaryItemProps> = (props: PrimaryItemProps) => {
@@ -62,7 +62,7 @@ const SecondaryItem: FC<SecondaryItemProps> = (props: SecondaryItemProps) => {
 
 
 type Props = {
-  onItemSelected: (contents: SidebarContents) => void,
+  onItemSelected: (contents: SidebarContentsType) => void,
 }
 
 const SidebarNav: FC<Props> = (props: Props) => {
@@ -78,8 +78,8 @@ const SidebarNav: FC<Props> = (props: Props) => {
   return (
     <div className="grw-sidebar-nav">
       <div className="grw-sidebar-nav-primary-container">
-        {!isSharedUser && <PrimaryItem contents={SidebarContents.CUSTOM} label="Custom Sidebar" iconName="code" onItemSelected={onItemSelected} />}
-        {!isSharedUser && <PrimaryItem contents={SidebarContents.RECENT} label="Recent Changes" iconName="update" onItemSelected={onItemSelected} />}
+        {!isSharedUser && <PrimaryItem contents={SidebarContentsType.CUSTOM} label="Custom Sidebar" iconName="code" onItemSelected={onItemSelected} />}
+        {!isSharedUser && <PrimaryItem contents={SidebarContentsType.RECENT} label="Recent Changes" iconName="update" onItemSelected={onItemSelected} />}
         {/* <PrimaryItem id="tag" label="Tags" iconName="icon-tag" /> */}
         {/* <PrimaryItem id="favorite" label="Favorite" iconName="icon-star" /> */}
       </div>

+ 3 - 3
packages/app/src/interfaces/ui.ts

@@ -1,6 +1,6 @@
-export const SidebarContents = {
+export const SidebarContentsType = {
   CUSTOM: 'custom',
   RECENT: 'recent',
 } as const;
-export const AllSidebarContents = Object.values(SidebarContents);
-export type SidebarContents = typeof SidebarContents[keyof typeof SidebarContents];
+export const AllSidebarContentsType = Object.values(SidebarContentsType);
+export type SidebarContentsType = typeof SidebarContentsType[keyof typeof SidebarContentsType];

+ 2 - 2
packages/app/src/interfaces/user-ui-settings.ts

@@ -1,10 +1,10 @@
 import { IUser } from './user';
 
-import { SidebarContents } from './ui';
+import { SidebarContentsType } from './ui';
 
 export interface IUserUISettings {
   userId: IUser | string;
   isSidebarCollapsed: boolean,
-  currentSidebarContents: SidebarContents,
+  currentSidebarContents: SidebarContentsType,
   currentProductNavWidth: number,
 }

+ 3 - 3
packages/app/src/server/models/user-ui-settings.ts

@@ -4,7 +4,7 @@ import {
 
 import { getOrCreateModel } from '@growi/core';
 
-import { SidebarContents as SidebarContentType } from '~/interfaces/ui';
+import { SidebarContentsType } from '~/interfaces/ui';
 import { IUserUISettings } from '~/interfaces/user-ui-settings';
 
 
@@ -16,8 +16,8 @@ const schema = new Schema<IUserUISettings>({
   isSidebarCollapsed: { type: Boolean, default: false },
   currentSidebarContents: {
     type: String,
-    enum: SidebarContentType,
-    default: SidebarContentType.RECENT,
+    enum: SidebarContentsType,
+    default: SidebarContentsType.RECENT,
   },
   currentProductNavWidth: { type: Number },
 });

+ 2 - 2
packages/app/src/server/routes/apiv3/user-ui-settings.ts

@@ -1,6 +1,6 @@
 import express from 'express';
 import { body } from 'express-validator';
-import { AllSidebarContents } from '~/interfaces/ui';
+import { AllSidebarContentsType } from '~/interfaces/ui';
 
 import loggerFactory from '~/utils/logger';
 
@@ -19,7 +19,7 @@ module.exports = (crowi) => {
   const validatorForPut = [
     body('settings').exists().withMessage('The body param \'settings\' is required'),
     body('settings.isSidebarCollapsed').optional().isBoolean(),
-    body('settings.currentSidebarContents').optional().isIn(AllSidebarContents),
+    body('settings.currentSidebarContents').optional().isIn(AllSidebarContentsType),
     body('settings.currentProductNavWidth').optional().isNumeric(),
   ];
 

+ 3 - 4
packages/app/src/stores/ui.tsx

@@ -8,8 +8,7 @@ import { AxiosResponse } from 'axios';
 
 import { Breakpoint, addBreakpointListener } from '@growi/ui';
 
-import { apiv3Get, apiv3Put } from '~/client/util/apiv3-client';
-import { SidebarContents } from '~/interfaces/ui';
+import { SidebarContentsType } from '~/interfaces/ui';
 import loggerFactory from '~/utils/logger';
 
 import { sessionStorageMiddleware } from './middlewares/sync-to-storage';
@@ -150,10 +149,10 @@ export const useSidebarCollapsed = (): SWRResponse<boolean, Error> => {
   );
 };
 
-export const useCurrentSidebarContents = (): SWRResponse<SidebarContents, Error> => {
+export const useCurrentSidebarContents = (): SWRResponse<SidebarContentsType, Error> => {
   const { data } = useSWRxUserUISettings();
   const key = data === undefined ? null : 'sidebarContents';
-  const initialData = data?.currentSidebarContents || SidebarContents.RECENT;
+  const initialData = data?.currentSidebarContents || SidebarContentsType.RECENT;
 
   return useStaticSWR(
     key,