forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Redirect.tsx
56 lines (47 loc) · 1.51 KB
/
Redirect.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use client';
import { useRouter, useSearchParams } from 'next/navigation';
import { memo, useEffect } from 'react';
import { messageService } from '@/services/message';
import { sessionService } from '@/services/session';
import { useGlobalStore } from '@/store/global';
import { useSessionStore } from '@/store/session';
const checkHasConversation = async () => {
const hasMessages = await messageService.hasMessages();
const hasAgents = await sessionService.hasSessions();
return hasMessages || hasAgents;
};
const Redirect = memo(() => {
const router = useRouter();
const [switchSession] = useSessionStore((s) => [s.switchSession]);
// get settings str from query
const searchParams = useSearchParams();
const setting = searchParams.get('settings');
const [setConfig] = useGlobalStore((s) => [s.setModelProviderConfig]);
useEffect(() => {
checkHasConversation().then((hasData) => {
if (hasData) {
router.replace('/chat');
switchSession();
} else {
router.replace('/welcome');
}
});
// try to parse and set settings
if (setting) {
try {
const settings = JSON.parse(setting);
const { url, key } = settings as {
key?: string;
url?: string;
};
// set settings
setTimeout(() => {
if (key) setConfig('openAI', { OPENAI_API_KEY: key });
if (url) setConfig('openAI', { endpoint: url });
}, 5000);
} catch {}
}
}, []);
return null;
});
export default Redirect;