Duibonduil commited on
Commit
008285f
·
verified ·
1 Parent(s): 96c4830

Upload useAgentId.ts

Browse files
aworld/cmd/web/webui/src/useAgentId.ts ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { useEffect, useState } from 'react';
2
+
3
+ export const useAgentId = () => {
4
+ const [agentId, setAgentId] = useState<string>('');
5
+
6
+ // 从URL参数中获取agent ID
7
+ const getAgentIdFromURL = (): string => {
8
+ const urlParams = new URLSearchParams(window.location.search);
9
+ return urlParams.get('agentid') || '';
10
+ };
11
+
12
+ // 更新URL参数中的agent ID
13
+ const updateURLAgentId = (id: string) => {
14
+ const url = new URL(window.location.href);
15
+ if (id) {
16
+ url.searchParams.set('agentid', id);
17
+ } else {
18
+ url.searchParams.delete('agentid');
19
+ }
20
+ window.history.replaceState({}, '', url.toString());
21
+ };
22
+
23
+ // 设置新的agent ID并更新URL
24
+ const setAgentIdAndUpdateURL = (id: string) => {
25
+ setAgentId(id);
26
+ updateURLAgentId(id);
27
+ };
28
+
29
+ useEffect(() => {
30
+ // 初始化时检查URL中是否有agent ID
31
+ const urlAgentId = getAgentIdFromURL();
32
+
33
+ if (urlAgentId) {
34
+ // 如果URL中有agent ID,使用它
35
+ setAgentId(urlAgentId);
36
+ }
37
+ }, []);
38
+
39
+ return {
40
+ agentId,
41
+ setAgentIdAndUpdateURL,
42
+ updateURLAgentId,
43
+ };
44
+ };