forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-mcp-url.ts
More file actions
48 lines (41 loc) · 1.32 KB
/
Copy pathuse-mcp-url.ts
File metadata and controls
48 lines (41 loc) · 1.32 KB
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
'use client'
import { useEffect, useState } from 'react'
import { fetchMcpUrl } from '@/lib/api'
export type McpUrlState =
| { status: 'loading' }
| { status: 'ready'; url: string }
| { status: 'failed' }
/**
* The address to paste into an agent, from the one place that knows it.
*
* Cached at module scope rather than per component: the value changes on
* deploy, and the connect dialog mounts in three places on the settings page
* alone. The promise is cached, not the result, so two mounts in the same tick
* share one request instead of racing.
*
* A rejection clears the cache so a retry can actually retry — a stored
* rejected promise would make every later mount fail instantly with the first
* failure, which is a dead dialog until reload.
*/
let pending: Promise<string> | null = null
function load(): Promise<string> {
pending ??= fetchMcpUrl().catch((err: unknown) => {
pending = null
throw err
})
return pending
}
export function useMcpUrl(): McpUrlState {
const [state, setState] = useState<McpUrlState>({ status: 'loading' })
useEffect(() => {
let cancelled = false
void load().then(
(url) => !cancelled && setState({ status: 'ready', url }),
() => !cancelled && setState({ status: 'failed' }),
)
return () => {
cancelled = true
}
}, [])
return state
}