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

improve useSWRxSlackChannels types

kaori 3 лет назад
Родитель
Сommit
e8d52910cb

+ 3 - 2
packages/app/src/components/PageEditor/EditorNavbarBottom.tsx

@@ -6,7 +6,7 @@ import { Collapse, Button } from 'reactstrap';
 
 import AppContainer from '~/client/services/AppContainer';
 import EditorContainer from '~/client/services/EditorContainer';
-import { useSlackChannels } from '~/stores/context';
+import { useCurrentPagePath } from '~/stores/context';
 import { useSWRxSlackChannels, useIsSlackEnabled } from '~/stores/editor';
 import {
   EditorMode, useDrawerOpened, useEditorMode, useIsDeviceSmallerThanMd,
@@ -32,7 +32,8 @@ const EditorNavbarBottom = (props) => {
   const { mutate: mutateDrawerOpened } = useDrawerOpened();
   const { data: isDeviceSmallerThanMd } = useIsDeviceSmallerThanMd();
   const { data: isSlackEnabled, mutate: mutateIsSlackEnabled } = useIsSlackEnabled();
-  const { data: slackChannelsData } = useSWRxSlackChannels('/aa');
+  const { data: currentPagePath } = useCurrentPagePath();
+  const { data: slackChannelsData } = useSWRxSlackChannels(currentPagePath);
   const additionalClasses = ['grw-editor-navbar-bottom'];
 
   const [slackChannels, setSlackChannels] = useState<string>('');

+ 4 - 4
packages/app/src/stores/editor.tsx

@@ -1,4 +1,4 @@
-import { SWRResponse } from 'swr';
+import useSWR, { SWRResponse } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
 import { apiGet } from '~/client/util/apiv1-client';
@@ -72,10 +72,10 @@ export const useCurrentIndentSize = (): SWRResponse<number, Error> => {
   );
 };
 
-export const useSWRxSlackChannels = (path: string): SWRResponse<Nullable<string>, Error> => {
+export const useSWRxSlackChannels = (path: Nullable<string>): SWRResponse<Nullable<string[]>, Error> => {
   const shouldFetch: boolean = path != null;
-  return useSWRImmutable(
+  return useSWR(
     shouldFetch ? ['/pages.updatePost', path] : null,
-    (endpoint, path) => apiGet(endpoint, { path }).then(response => response.updatePost),
+    (endpoint, path) => apiGet(endpoint, { path }).then((response: {[updatePost: string]: string[]}) => response.updatePost),
   );
 };