-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathContent.tsx
More file actions
308 lines (273 loc) · 11.2 KB
/
Content.tsx
File metadata and controls
308 lines (273 loc) · 11.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {useCallback, useEffect, useMemo, useState} from "react"
import {
revisionsListWithDraftsAtomFamily,
variantsListWithDraftsAtomFamily,
} from "@agenta/entities/legacyAppRevision"
import {message} from "@agenta/ui/app-message"
import {Trash} from "@phosphor-icons/react"
import {Button, Spin, Typography} from "antd"
import {atom, getDefaultStore, useAtomValue, useSetAtom} from "jotai"
import {
deleteVariantMutationAtom,
invalidatePlaygroundQueriesAtom,
moleculeBackedVariantAtomFamily,
} from "@/oss/components/Playground/state/atoms"
import {checkIfResourceValidForDeletion} from "@/oss/lib/evaluations/legacy"
import {deleteSingleVariant} from "@/oss/services/playground/api"
import {selectedAppIdAtom} from "@/oss/state/app/selectors/app"
const {Text} = Typography
interface Props {
revisionIds: string[]
forceVariantIds?: string[]
onClose: () => void
}
interface VariantGroup {
variantId: string
selectedIds: string[]
totalIds: string[]
displayName: string
deleteEntireVariant: boolean
}
const isVisibleServerRevision = (revision: any) => {
if (!revision?.id) return false
if (revision?.isLocalDraft) return false
return Number(revision?.revision ?? 0) > 0
}
const DeleteVariantContent = ({revisionIds, forceVariantIds = [], onClose}: Props) => {
const store = getDefaultStore()
const deleteVariant = useSetAtom(deleteVariantMutationAtom)
const invalidatePlaygroundQueries = useSetAtom(invalidatePlaygroundQueriesAtom)
const [checking, setChecking] = useState(true)
const [canDelete, setCanDelete] = useState<boolean | null>(null)
const [isMutating, setIsMutating] = useState(false)
const appId = useAtomValue(selectedAppIdAtom)
const emptyListAtom = useMemo(
() => atom({data: [], isPending: false, isError: false, error: null}),
[],
)
const variantsListAtom = useMemo(
() => (appId ? variantsListWithDraftsAtomFamily(appId) : emptyListAtom),
[appId, emptyListAtom],
)
const variantsQuery = useAtomValue(variantsListAtom)
const variants = variantsQuery.data ?? []
const uniqueRevisionIds = useMemo(
() => Array.from(new Set([revisionIds].flat().filter(Boolean))) as string[],
[revisionIds],
)
const resolvedRevisions = useMemo(
() =>
uniqueRevisionIds
// Use molecule-backed variant for single source of truth
.map((id) => store.get(moleculeBackedVariantAtomFamily(id)))
.filter(Boolean) as any[],
[store, uniqueRevisionIds],
)
const variantNameMap = useMemo(() => {
const map: Record<string, string> = {}
variants.forEach((variant: any) => {
if (!variant?.id) return
map[variant.id] = (variant.name as string) || (variant.baseName as string) || variant.id
})
return map
}, [variants])
const variantGroups = useMemo(() => {
const groups: Record<string, VariantGroup> = {}
const forceVariantIdSet = new Set(forceVariantIds)
resolvedRevisions.forEach((rev: any) => {
const variantId = (rev?._parentVariant as string) || (rev?.variantId as string)
if (!variantId) return
const existing = groups[variantId]
const selectedIds = [...(existing?.selectedIds || []), rev.id].filter(
Boolean,
) as string[]
const totalIds = existing?.totalIds || []
const displayName = existing?.displayName ?? variantNameMap[variantId] ?? "-"
groups[variantId] = {
variantId,
selectedIds,
totalIds,
displayName,
deleteEntireVariant: false,
}
})
Object.values(groups).forEach((group) => {
const allRevisions = (store.get(revisionsListWithDraftsAtomFamily(group.variantId))
?.data || []) as any[]
const totalIds = allRevisions
.filter(isVisibleServerRevision)
.map((r: any) => r.id)
.filter(Boolean) as string[]
group.totalIds = totalIds.length > 0 ? totalIds : group.selectedIds
const selectedSet = new Set(group.selectedIds)
group.deleteEntireVariant =
forceVariantIdSet.has(group.variantId) ||
(group.totalIds.length > 0 && group.totalIds.every((id) => selectedSet.has(id)))
})
return groups
}, [forceVariantIds, resolvedRevisions, store, variantNameMap])
useEffect(() => {
let mounted = true
const variantIds = Object.keys(variantGroups)
if (variantIds.length === 0) {
setCanDelete(false)
setChecking(false)
return
}
;(async () => {
try {
const ok = await checkIfResourceValidForDeletion({
resourceType: "variant",
resourceIds: variantIds,
})
if (mounted) setCanDelete(ok)
} catch (e) {
if (mounted) setCanDelete(false)
} finally {
if (mounted) setChecking(false)
}
})()
return () => {
mounted = false
}
}, [variantGroups])
const deletionPlan = useMemo(() => {
const variants: string[] = []
const revisions = new Set<string>()
Object.values(variantGroups).forEach((group) => {
if (group.deleteEntireVariant) {
variants.push(group.variantId)
} else {
group.selectedIds.forEach((id) => revisions.add(id))
}
})
return {variants, revisions: Array.from(revisions)}
}, [variantGroups])
const targetVariantCount = Math.max(
deletionPlan.variants.length,
Object.keys(variantGroups).length,
)
const totalSelectedCount = uniqueRevisionIds.length
const isBulkDelete = deletionPlan.variants.length > 0 || totalSelectedCount > 1
// Check if this would delete the last revision of the app
const isLastRevision = useMemo(() => {
// Count total visible revisions across all variants
let totalRevisions = 0
Object.values(variantGroups).forEach((group) => {
totalRevisions += group.totalIds.length
})
// If we're deleting all revisions, this is the last one
return totalRevisions > 0 && totalSelectedCount >= totalRevisions
}, [variantGroups, totalSelectedCount])
const onDeleteVariant = useCallback(async () => {
setIsMutating(true)
try {
for (const variantId of deletionPlan.variants) {
await deleteSingleVariant(variantId)
}
for (const id of deletionPlan.revisions) {
const res = await deleteVariant(id)
if (!res?.success) {
throw new Error(res?.error || "Failed to delete revision")
}
}
// Always invalidate all related queries so registry and playground stay in sync.
await invalidatePlaygroundQueries()
message.success(
deletionPlan.variants.length > 0
? "Deleted selected variants successfully"
: "Deleted selected revision(s) successfully",
)
onClose()
} catch (error) {
console.error("Failed to delete variant(s):", error)
message.error(error instanceof Error ? error.message : "Failed to delete variant(s)")
} finally {
setIsMutating(false)
}
}, [deletionPlan, deleteVariant, invalidatePlaygroundQueries, onClose])
// Loading state during pre-check
if (checking) {
return (
<div className="flex items-center gap-3 py-6">
<Spin />
<Text>Checking if the selected item(s) can be deleted…</Text>
</div>
)
}
// Blocked state if not deletable
if (canDelete === false) {
return (
<section className="flex flex-col gap-4">
<Text>
One or more variants cannot be deleted because they are currently in use.
</Text>
<div className="flex items-center justify-end">
<Button type="primary" onClick={onClose}>
Close
</Button>
</div>
</section>
)
}
return (
<section className="flex flex-col gap-5">
<div className="flex flex-col gap-3">
<div className="flex flex-col gap-1">
<Text>
You are about to delete {targetVariantCount} variant
{targetVariantCount === 1 ? "" : "s"}
{deletionPlan.revisions.length > 0 &&
` (${deletionPlan.revisions.length} revision${
deletionPlan.revisions.length === 1 ? "" : "s"
})`}
.
</Text>
<Text type="secondary">
Selected revisions: {totalSelectedCount}. This action cannot be undone.
</Text>
</div>
<div className="flex flex-col gap-2">
{Object.values(variantGroups).map((group) => (
<div
key={group.variantId}
className="flex items-center justify-between rounded-md bg-gray-50 px-3 py-2"
>
<div className="flex flex-col">
<Text strong>{group.displayName}</Text>
<Text type="secondary">Variant ID: {group.variantId}</Text>
</div>
<Text>
{group.deleteEntireVariant
? "All revisions will be removed"
: `${group.selectedIds.length} of ${
group.totalIds.length || group.selectedIds.length
} revisions`}
</Text>
</div>
))}
</div>
</div>
<div className="flex items-center justify-end gap-2">
<Button onClick={onClose}>Cancel</Button>
<Button
type="primary"
danger
loading={isMutating}
disabled={isMutating || totalSelectedCount === 0 || isLastRevision}
icon={<Trash size={14} />}
onClick={onDeleteVariant}
title={isLastRevision ? "Cannot delete the only revision. Delete the app instead." : undefined}
>
{isBulkDelete ? "Delete selected" : "Delete"}
</Button>
</div>
{isLastRevision && (
<Text type="secondary" className="text-center">
Cannot delete the only revision. Delete the app instead.
</Text>
)}
</section>
)
}
export default DeleteVariantContent