Repository navigation
Expand file tree
/
Copy pathAppStatuses.tsx
More file actions
165 lines (154 loc) · 4.76 KB
/
Copy pathAppStatuses.tsx
File metadata and controls
165 lines (154 loc) · 4.76 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { cn } from "cn";
import capitalize from "lodash/capitalize";
import { ExternalLinkIcon, FileIcon } from "lucide-react";
import { useState } from "react";
import type {
WorkspaceAppStatus as APIWorkspaceAppStatus,
Workspace,
WorkspaceAgent,
WorkspaceApp,
} from "#/api/typesGenerated";
import { ChevronDownIcon } from "#/components/AnimatedIcons/ChevronDown";
import { Button } from "#/components/Button/Button";
import { ScrollArea } from "#/components/ScrollArea/ScrollArea";
import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "#/components/Tooltip/Tooltip";
import { AppStatusStateIcon } from "#/modules/apps/AppStatusStateIcon";
import { AppLink } from "#/modules/resources/AppLink/AppLink";
import { timeFrom } from "#/utils/time";
import { truncateURI } from "#/utils/uri";
type AppStatusesProps = {
workspace: Workspace;
agent: WorkspaceAgent;
/** Optional reference date for calculating relative time. Defaults to Date.now(). Useful for Storybook. */
referenceDate?: Date;
};
// Extend the API status type to include the app icon and the app itself
type StatusWithAppInfo = APIWorkspaceAppStatus & {
appIcon?: string; // Kept for potential future use, but we'll primarily use app.icon
app?: WorkspaceApp; // Store the full app object
};
export const AppStatuses: React.FC<AppStatusesProps> = ({
workspace,
agent,
referenceDate,
}) => {
const [displayStatuses, setDisplayStatuses] = useState(false);
// Statuses are returned from the API sorted by created_at DESC, id DESC.
const allStatuses: StatusWithAppInfo[] = agent.apps.flatMap((app) =>
app.statuses.map((status) => ({
...status,
app,
})),
);
if (allStatuses.length === 0) {
return null;
}
const comparisonDate = referenceDate ?? new Date();
const latestStatus = allStatuses[0];
const otherStatuses = allStatuses.slice(1);
return (
<div className="flex flex-col border rounded-lg">
<div
className={cn(
"flex items-center justify-between px-4 py-3 gap-6",
"not-last:border-b",
)}
>
<div className="flex flex-col overflow-hidden">
<div className="text-sm text-content-primary flex items-center gap-2">
<AppStatusStateIcon state={latestStatus.state} latest />
<span className="block flex-1 truncate">
{latestStatus.message || capitalize(latestStatus.state)}
</span>
</div>
<time className="text-xs text-content-secondary first-letter:uppercase block pl-6.5">
{timeFrom(new Date(latestStatus.created_at), comparisonDate)}
</time>
</div>
<div className="flex items-center gap-2">
{latestStatus.app && (
<AppLink
app={latestStatus.app}
agent={agent}
workspace={workspace}
size="sm"
/>
)}
{latestStatus.uri &&
(latestStatus.uri.startsWith("file://") ? (
<Tooltip>
<TooltipTrigger>
<span className="flex items-center gap-1">
<FileIcon className="size-icon-xs" />
{truncateURI(latestStatus.uri)}
</span>
</TooltipTrigger>
<TooltipContent>
This file is located in your workspace
</TooltipContent>
</Tooltip>
) : (
<Button asChild variant="outline" size="sm">
<a href={latestStatus.uri} target="_blank" rel="noreferrer">
<ExternalLinkIcon />
{truncateURI(latestStatus.uri)}
</a>
</Button>
))}
<Tooltip>
<TooltipTrigger asChild>
<Button
disabled={otherStatuses.length === 0}
size="icon"
variant="subtle"
onClick={() => {
setDisplayStatuses((display) => !display);
}}
>
<ChevronDownIcon />
</Button>
</TooltipTrigger>
<TooltipContent>
{displayStatuses ? "Hide statuses" : "Show statuses"}
</TooltipContent>
</Tooltip>
</div>
</div>
{displayStatuses && (
<ScrollArea className="h-50">
{otherStatuses.map((status) => {
const statusTime = new Date(status.created_at);
const formattedTimestamp = timeFrom(statusTime, comparisonDate);
return (
<div
key={status.id}
className={cn(
"flex items-center justify-between px-4 py-3",
"not-last:border-b",
)}
>
<div className="flex items-center justify-between w-full text-content-secondary">
<span className="text-xs flex items-center gap-2">
<AppStatusStateIcon
state={status.state}
latest={false}
className="size-icon-xs w-4.5"
/>
{status.message || capitalize(status.state)}
</span>
<span className="text-2xs text-content-secondary first-letter:uppercase block pl-6.5">
{formattedTimestamp}
</span>
</div>
</div>
);
})}
</ScrollArea>
)}
</div>
);
};