forked from koshikraj/ottopus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests-view.tsx
More file actions
100 lines (95 loc) · 3.2 KB
/
Copy pathrequests-view.tsx
File metadata and controls
100 lines (95 loc) · 3.2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use client'
import { useRouter } from 'next/navigation'
import { useEffect, useState } from 'react'
import { Otto } from '@/components/brand'
import { BubbleField } from '@/components/motion'
import { Button, EmptyState, ErrorState } from '@/components/ui'
import { ApiError } from '@/lib/api'
import { PlanTable, PlanTableSkeleton } from './plan-table'
import { useRequests } from './provider'
import { usePlans } from './use-plans'
/**
* P5, on the Requests route: every request, waiting-on-you first. The badge
* in the nav counts the pending subset through RequestsProvider; this page
* reads the whole history and re-reads whenever that count moves.
*/
export function RequestsView() {
const { plans: pending, open } = useRequests()
const { state, refresh } = usePlans(pending.map((p) => `${p.id}:${p.version}:${p.status}`).join(','))
const router = useRouter()
const [opening, setOpening] = useState<string | null>(null)
const [error, setError] = useState<string | null>(null)
// A slow clock, so a row reads expired within the minute without a reload.
const [now, setNow] = useState(() => Date.now())
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), 30_000)
return () => clearInterval(id)
}, [])
const review = async (id: string) => {
if (opening) return
setOpening(id)
setError(null)
try {
router.push(await open(id))
} catch (err) {
setError(
err instanceof ApiError && err.status === 404
? 'That request is no longer here. The list is being refreshed.'
: 'Couldn’t open this request. Please try again.',
)
refresh()
} finally {
setOpening(null)
}
}
if (state.status === 'loading') {
return (
<div className="px-5 py-6 sm:px-[26px]">
<PlanTableSkeleton />
</div>
)
}
if (state.status === 'failed' && state.plans.length === 0) {
return (
<ErrorState
title="Couldn’t read your requests"
description="Try again to see what is waiting on you."
action={
<Button variant="secondary" onClick={refresh}>
Try again
</Button>
}
/>
)
}
return (
<div className="px-5 py-6 sm:px-[26px]">
{state.status === 'failed' ? (
<p role="status" className="mb-4 text-sm text-[var(--ot-text-2)]">
Couldn’t refresh. Showing the last requests we read.{' '}
<button className="underline" onClick={refresh}>
Try again
</button>
</p>
) : null}
{error ? (
<p role="alert" className="mb-4 text-sm text-[var(--ot-block-text)]">
{error}
</p>
) : null}
{state.plans.length === 0 ? (
<div className="ot-canvas relative overflow-hidden rounded-2xl">
<BubbleField pattern="calm" />
<EmptyState
className="relative"
title="Calm waters"
description="Nothing yet. Otto surfaces here when your agent asks for something."
illustration={<Otto pose="base" size={150} animated />}
/>
</div>
) : (
<PlanTable plans={state.plans} opening={opening} onOpen={(id) => void review(id)} now={now} />
)}
</div>
)
}