Skip to content

Commit 6441976

Browse files
refactor(home): extract home page logic to useHome hook
1 parent 08e5043 commit 6441976

File tree

2 files changed

+77
-43
lines changed

2 files changed

+77
-43
lines changed

wren-ui/src/hooks/useHome.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { ComponentRef, useMemo, useRef } from 'react';
2+
import { useRouter } from 'next/router';
3+
import { useSuggestedQuestionsQuery, useCreateThreadMutation, useThreadLazyQuery } from '@/apollo/client/graphql/home.generated';
4+
import { useGetSettingsQuery } from '@/apollo/client/graphql/settings.generated';
5+
import { CreateThreadInput } from '@/apollo/client/graphql/__types__';
6+
import useHomeSidebar from '@/hooks/useHomeSidebar';
7+
import useAskPrompt from '@/hooks/useAskPrompt';
8+
import { Path } from '@/utils/enum';
9+
10+
export const useHome = () => {
11+
const $prompt = useRef<ComponentRef<typeof Prompt>>(null);
12+
const router = useRouter();
13+
const homeSidebar = useHomeSidebar();
14+
const askPrompt = useAskPrompt();
15+
16+
const { data: suggestedQuestionsData } = useSuggestedQuestionsQuery({
17+
fetchPolicy: 'cache-and-network',
18+
});
19+
const [createThread, { loading: threadCreating }] = useCreateThreadMutation({
20+
onError: (error) => console.error(error),
21+
onCompleted: () => homeSidebar.refetch(),
22+
});
23+
const [preloadThread] = useThreadLazyQuery({
24+
fetchPolicy: 'cache-and-network',
25+
});
26+
27+
const { data: settingsResult } = useGetSettingsQuery();
28+
const settings = settingsResult?.settings;
29+
const isSampleDataset = useMemo(
30+
() => Boolean(settings?.dataSource?.sampleDataset),
31+
[settings],
32+
);
33+
34+
const sampleQuestions = useMemo(
35+
() => suggestedQuestionsData?.suggestedQuestions.questions || [],
36+
[suggestedQuestionsData],
37+
);
38+
39+
const onSelectQuestion = async ({ question }) => {
40+
$prompt.current.submit(question);
41+
};
42+
43+
const onCreateResponse = async (payload: CreateThreadInput) => {
44+
try {
45+
askPrompt.onStopPolling();
46+
const response = await createThread({ variables: { data: payload } });
47+
const threadId = response.data.createThread.id;
48+
await preloadThread({ variables: { threadId } });
49+
router.push(Path.Home + `/${threadId}`);
50+
} catch (error) {
51+
console.error(error);
52+
}
53+
};
54+
55+
return {
56+
$prompt,
57+
homeSidebar,
58+
askPrompt,
59+
isSampleDataset,
60+
sampleQuestions,
61+
onSelectQuestion,
62+
onCreateResponse,
63+
threadCreating,
64+
};
65+
};

wren-ui/src/pages/home/index.tsx

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -87,50 +87,19 @@ function RecommendedQuestionsInstruction(props) {
8787
);
8888
}
8989

90-
export default function Home() {
91-
const $prompt = useRef<ComponentRef<typeof Prompt>>(null);
92-
const router = useRouter();
93-
const homeSidebar = useHomeSidebar();
94-
const askPrompt = useAskPrompt();
95-
96-
const { data: suggestedQuestionsData } = useSuggestedQuestionsQuery({
97-
fetchPolicy: 'cache-and-network',
98-
});
99-
const [createThread, { loading: threadCreating }] = useCreateThreadMutation({
100-
onError: (error) => console.error(error),
101-
onCompleted: () => homeSidebar.refetch(),
102-
});
103-
const [preloadThread] = useThreadLazyQuery({
104-
fetchPolicy: 'cache-and-network',
105-
});
90+
import { useHome } from '@/hooks/useHome';
10691

107-
const { data: settingsResult } = useGetSettingsQuery();
108-
const settings = settingsResult?.settings;
109-
const isSampleDataset = useMemo(
110-
() => Boolean(settings?.dataSource?.sampleDataset),
111-
[settings],
112-
);
113-
114-
const sampleQuestions = useMemo(
115-
() => suggestedQuestionsData?.suggestedQuestions.questions || [],
116-
[suggestedQuestionsData],
117-
);
118-
119-
const onSelectQuestion = async ({ question }) => {
120-
$prompt.current.submit(question);
121-
};
122-
123-
const onCreateResponse = async (payload: CreateThreadInput) => {
124-
try {
125-
askPrompt.onStopPolling();
126-
const response = await createThread({ variables: { data: payload } });
127-
const threadId = response.data.createThread.id;
128-
await preloadThread({ variables: { threadId } });
129-
router.push(Path.Home + `/${threadId}`);
130-
} catch (error) {
131-
console.error(error);
132-
}
133-
};
92+
export default function Home() {
93+
const {
94+
$prompt,
95+
homeSidebar,
96+
askPrompt,
97+
isSampleDataset,
98+
sampleQuestions,
99+
onSelectQuestion,
100+
onCreateResponse,
101+
threadCreating,
102+
} = useHome();
134103

135104
return (
136105
<SiderLayout loading={false} sidebar={homeSidebar}>

0 commit comments

Comments
 (0)