mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
added useMemos to get data hooks
This commit is contained in:
parent
1904153bba
commit
d9feff085a
11 changed files with 49 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { useCallback } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { OAuthProvider } from '~/types/auth';
|
import { OAuthProvider } from '~/types/auth';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { apiAttachSocial, apiDropSocial, apiGetSocials, apiLoginWithSocial } from '~/api/auth';
|
import { apiAttachSocial, apiDropSocial, apiGetSocials, apiLoginWithSocial } from '~/api/auth';
|
||||||
|
@ -89,13 +89,15 @@ export const useOAuth = () => {
|
||||||
[mutate]
|
[mutate]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const accounts = useMemo(() => data || [], [data]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
openOauthWindow,
|
openOauthWindow,
|
||||||
loginWithSocial,
|
loginWithSocial,
|
||||||
createSocialAccount,
|
createSocialAccount,
|
||||||
attachAccount,
|
attachAccount,
|
||||||
dropAccount,
|
dropAccount,
|
||||||
accounts: data || [],
|
accounts,
|
||||||
isLoading: !data && isLoading,
|
isLoading: !data && isLoading,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { flatten, isNil } from 'ramda';
|
||||||
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
||||||
import { apiGetNodeComments } from '~/api/node';
|
import { apiGetNodeComments } from '~/api/node';
|
||||||
import { COMMENTS_DISPLAY } from '~/constants/node';
|
import { COMMENTS_DISPLAY } from '~/constants/node';
|
||||||
import { useCallback } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { IComment } from '~/types';
|
import { IComment } from '~/types';
|
||||||
|
|
||||||
const getKey: (nodeId: number) => SWRInfiniteKeyLoader = (nodeId: number) => (
|
const getKey: (nodeId: number) => SWRInfiniteKeyLoader = (nodeId: number) => (
|
||||||
|
@ -41,7 +41,7 @@ export const useGetComments = (nodeId: number) => {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const comments = flatten(data || []);
|
const comments = useMemo(() => flatten(data || []), [data]);
|
||||||
const hasMore = (data?.[size - 1]?.length || 0) >= COMMENTS_DISPLAY;
|
const hasMore = (data?.[size - 1]?.length || 0) >= COMMENTS_DISPLAY;
|
||||||
|
|
||||||
const onLoadMoreComments = useCallback(() => setSize(size + 1), [setSize, size]);
|
const onLoadMoreComments = useCallback(() => setSize(size + 1), [setSize, size]);
|
||||||
|
|
|
@ -2,7 +2,6 @@ import { useFlowLayout } from '~/hooks/flow/useFlowLayout';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
import { useFlowLoader } from '~/hooks/flow/useFlowLoader';
|
import { useFlowLoader } from '~/hooks/flow/useFlowLoader';
|
||||||
import { useFlowStore } from '~/store/flow/useFlowStore';
|
import { useFlowStore } from '~/store/flow/useFlowStore';
|
||||||
import { useInfiniteLoader } from '~/hooks/dom/useInfiniteLoader';
|
|
||||||
import { useFlowSetCellView } from '~/hooks/flow/useFlowSetCellView';
|
import { useFlowSetCellView } from '~/hooks/flow/useFlowSetCellView';
|
||||||
import { useGetLabStats } from '~/hooks/lab/useGetLabStats';
|
import { useGetLabStats } from '~/hooks/lab/useGetLabStats';
|
||||||
|
|
||||||
|
@ -13,10 +12,19 @@ export const useFlow = () => {
|
||||||
const { isFluid, toggleLayout } = useFlowLayout();
|
const { isFluid, toggleLayout } = useFlowLayout();
|
||||||
const lab = useGetLabStats();
|
const lab = useGetLabStats();
|
||||||
|
|
||||||
useInfiniteLoader(loadMore, isSyncing);
|
|
||||||
|
|
||||||
const updates = useMemo(() => [...updated, ...lab.updates].slice(0, 10), [lab.updates, updated]);
|
const updates = useMemo(() => [...updated, ...lab.updates].slice(0, 10), [lab.updates, updated]);
|
||||||
|
|
||||||
const onChangeCellView = useFlowSetCellView();
|
const onChangeCellView = useFlowSetCellView();
|
||||||
return { nodes, heroes, recent, updates, isFluid, toggleLayout, onChangeCellView };
|
|
||||||
|
return {
|
||||||
|
nodes,
|
||||||
|
heroes,
|
||||||
|
recent,
|
||||||
|
updates,
|
||||||
|
isFluid,
|
||||||
|
toggleLayout,
|
||||||
|
onChangeCellView,
|
||||||
|
loadMore,
|
||||||
|
isSyncing,
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { GetLabNodesRequest, ILabNode } from '~/types/lab';
|
||||||
import { getLabNodes } from '~/api/lab';
|
import { getLabNodes } from '~/api/lab';
|
||||||
import { flatten, last, uniqBy } from 'ramda';
|
import { flatten, last, uniqBy } from 'ramda';
|
||||||
import { useLabStore } from '~/store/lab/useLabStore';
|
import { useLabStore } from '~/store/lab/useLabStore';
|
||||||
import { useCallback } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { INode } from '~/types';
|
import { INode } from '~/types';
|
||||||
import { useAuth } from '~/hooks/auth/useAuth';
|
import { useAuth } from '~/hooks/auth/useAuth';
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ export const useGetLabNodes = () => {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const nodes = uniqBy(n => n.node.id, flatten(data || []));
|
const nodes = useMemo(() => uniqBy(n => n.node.id, flatten(data || [])), [data]);
|
||||||
const hasMore = (data?.[size - 1]?.length || 0) >= 1;
|
const hasMore = (data?.[size - 1]?.length || 0) >= 1;
|
||||||
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import useSWR from 'swr';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { apiGetUserMessages } from '~/api/messages';
|
import { apiGetUserMessages } from '~/api/messages';
|
||||||
import { IMessage } from '~/types';
|
import { IMessage } from '~/types';
|
||||||
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
const getKey = (username: string): string | null => {
|
const getKey = (username: string): string | null => {
|
||||||
return username ? `${API.USER.MESSAGES}/${username}` : null;
|
return username ? `${API.USER.MESSAGES}/${username}` : null;
|
||||||
|
@ -11,7 +12,7 @@ export const useMessages = (username: string) => {
|
||||||
apiGetUserMessages({ username })
|
apiGetUserMessages({ username })
|
||||||
);
|
);
|
||||||
|
|
||||||
const messages: IMessage[] = data?.messages || [];
|
const messages: IMessage[] = useMemo(() => data?.messages || [], [data]);
|
||||||
|
|
||||||
return { messages, isLoading: !data && isValidating };
|
return { messages, isLoading: !data && isValidating };
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
||||||
import { flatten } from 'ramda';
|
import { flatten } from 'ramda';
|
||||||
import { getSearchResults } from '~/api/flow';
|
import { getSearchResults } from '~/api/flow';
|
||||||
|
@ -44,7 +44,7 @@ export const useSearch = () => {
|
||||||
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
||||||
const hasMore = (data?.[size - 1]?.length || 0) >= RESULTS_COUNT;
|
const hasMore = (data?.[size - 1]?.length || 0) >= RESULTS_COUNT;
|
||||||
|
|
||||||
const results = flatten(data || []);
|
const results = useMemo(() => flatten(data || []), [data]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const timeout = setTimeout(async () => {
|
const timeout = setTimeout(async () => {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { apiGetTagSuggestions } from '~/api/tags';
|
import { apiGetTagSuggestions } from '~/api/tags';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
|
|
||||||
export const useTagAutocomplete = (input: string, exclude: string[]): string[] => {
|
export const useTagAutocomplete = (input: string, exclude: string[]): string[] => {
|
||||||
const [search, setSearch] = useState('');
|
const [search, setSearch] = useState('');
|
||||||
|
@ -19,5 +19,5 @@ export const useTagAutocomplete = (input: string, exclude: string[]): string[] =
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
return data || [];
|
return useMemo(() => data || [], [data]);
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { INode } from '~/types';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { flatten, isNil } from 'ramda';
|
import { flatten, isNil } from 'ramda';
|
||||||
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';
|
||||||
import { useCallback } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { apiGetNodesOfTag } from '~/api/tags';
|
import { apiGetNodesOfTag } from '~/api/tags';
|
||||||
|
|
||||||
const PAGE_SIZE = 10;
|
const PAGE_SIZE = 10;
|
||||||
|
@ -40,7 +40,7 @@ export const useTagNodes = (tag: string) => {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const nodes = flatten(data || []);
|
const nodes = useMemo(() => flatten(data || []), [data]);
|
||||||
const hasMore = (data?.[size - 1]?.length || 0) >= PAGE_SIZE;
|
const hasMore = (data?.[size - 1]?.length || 0) >= PAGE_SIZE;
|
||||||
|
|
||||||
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
|
||||||
|
|
|
@ -7,6 +7,7 @@ import classNames from 'classnames';
|
||||||
import { FlowSwiperHero } from '~/components/flow/FlowSwiperHero';
|
import { FlowSwiperHero } from '~/components/flow/FlowSwiperHero';
|
||||||
import { useFlowContext } from '~/utils/context/FlowContextProvider';
|
import { useFlowContext } from '~/utils/context/FlowContextProvider';
|
||||||
import { useUser } from '~/hooks/auth/useUser';
|
import { useUser } from '~/hooks/auth/useUser';
|
||||||
|
import { useInfiniteLoader } from '~/hooks/dom/useInfiniteLoader';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
isFluid: boolean;
|
isFluid: boolean;
|
||||||
|
@ -14,9 +15,11 @@ interface Props {
|
||||||
}
|
}
|
||||||
|
|
||||||
const FlowLayout: FC<Props> = ({ isFluid, onToggleLayout }) => {
|
const FlowLayout: FC<Props> = ({ isFluid, onToggleLayout }) => {
|
||||||
const { heroes, nodes, onChangeCellView } = useFlowContext();
|
const { heroes, nodes, onChangeCellView, loadMore, isSyncing } = useFlowContext();
|
||||||
const { user } = useUser();
|
const { user } = useUser();
|
||||||
|
|
||||||
|
useInfiniteLoader(loadMore, isSyncing);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={classNames(styles.container)}>
|
<div className={classNames(styles.container)}>
|
||||||
<div className={styles.grid}>
|
<div className={styles.grid}>
|
||||||
|
|
|
@ -7,7 +7,17 @@ import { observer } from 'mobx-react-lite';
|
||||||
interface Props {}
|
interface Props {}
|
||||||
|
|
||||||
const FlowPage: FC<Props> = observer(() => {
|
const FlowPage: FC<Props> = observer(() => {
|
||||||
const { updates, nodes, heroes, recent, isFluid, toggleLayout, onChangeCellView } = useFlow();
|
const {
|
||||||
|
updates,
|
||||||
|
nodes,
|
||||||
|
heroes,
|
||||||
|
recent,
|
||||||
|
isFluid,
|
||||||
|
toggleLayout,
|
||||||
|
onChangeCellView,
|
||||||
|
loadMore,
|
||||||
|
isSyncing,
|
||||||
|
} = useFlow();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<FlowContextProvider
|
<FlowContextProvider
|
||||||
|
@ -15,6 +25,8 @@ const FlowPage: FC<Props> = observer(() => {
|
||||||
recent={recent}
|
recent={recent}
|
||||||
heroes={heroes}
|
heroes={heroes}
|
||||||
nodes={nodes}
|
nodes={nodes}
|
||||||
|
loadMore={loadMore}
|
||||||
|
isSyncing={isSyncing}
|
||||||
onChangeCellView={onChangeCellView}
|
onChangeCellView={onChangeCellView}
|
||||||
>
|
>
|
||||||
<FlowLayout isFluid={isFluid} onToggleLayout={toggleLayout} />
|
<FlowLayout isFluid={isFluid} onToggleLayout={toggleLayout} />
|
||||||
|
|
|
@ -6,6 +6,8 @@ export interface FlowContextProps {
|
||||||
recent: IFlowNode[];
|
recent: IFlowNode[];
|
||||||
heroes: IFlowNode[];
|
heroes: IFlowNode[];
|
||||||
nodes: IFlowNode[];
|
nodes: IFlowNode[];
|
||||||
|
isSyncing: boolean;
|
||||||
|
loadMore: () => Promise<unknown>;
|
||||||
onChangeCellView: (id: INode['id'], flow: FlowDisplay) => void;
|
onChangeCellView: (id: INode['id'], flow: FlowDisplay) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +16,8 @@ export const FlowContext = createContext<FlowContextProps>({
|
||||||
recent: [],
|
recent: [],
|
||||||
heroes: [],
|
heroes: [],
|
||||||
nodes: [],
|
nodes: [],
|
||||||
|
isSyncing: false,
|
||||||
|
loadMore: async () => {},
|
||||||
|
|
||||||
onChangeCellView: () => {},
|
onChangeCellView: () => {},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue