11import { Archive , ArchiveRestore , Trash2 } from "lucide-react" ;
2+ import { useMemo , type ReactNode } from "react" ;
23
4+ import { ownsAuthorAgent } from "@/features/profile/lib/identity" ;
5+ import { useUsersBatchQuery } from "@/features/profile/hooks" ;
6+ import type { ChannelMember } from "@/shared/api/types" ;
37import { cn } from "@/shared/lib/cn" ;
8+ import { normalizePubkey } from "@/shared/lib/pubkey" ;
49import {
510 AlertDialog ,
611 AlertDialogAction ,
@@ -34,6 +39,117 @@ type ChannelManagementModerationActionsProps = {
3439 unarchiveChannelMutation : ChannelMutation ;
3540} ;
3641
42+ type ChannelDeleteConfirmationDialogProps = {
43+ channelName : string ;
44+ error : unknown ;
45+ isPending : boolean ;
46+ onConfirm : ( ) => void ;
47+ onOpenChange : ( open : boolean ) => void ;
48+ open : boolean ;
49+ trigger ?: ReactNode ;
50+ } ;
51+
52+ export function useChannelModerationCapabilities (
53+ members : ChannelMember [ ] | undefined ,
54+ currentPubkey : string | undefined ,
55+ enabled : boolean ,
56+ ) {
57+ const normalizedCurrentPubkey = currentPubkey
58+ ? normalizePubkey ( currentPubkey )
59+ : undefined ;
60+ const selfRole = members ?. find (
61+ ( member ) => normalizePubkey ( member . pubkey ) === normalizedCurrentPubkey ,
62+ ) ?. role ;
63+ const ownerMemberPubkeys = useMemo (
64+ ( ) =>
65+ ( members ?? [ ] )
66+ . filter (
67+ ( member ) =>
68+ member . role === "owner" &&
69+ normalizePubkey ( member . pubkey ) !== normalizedCurrentPubkey ,
70+ )
71+ . map ( ( member ) => member . pubkey ) ,
72+ [ members , normalizedCurrentPubkey ] ,
73+ ) ;
74+ const shouldResolveAgentOwnership =
75+ enabled && selfRole !== "owner" && ownerMemberPubkeys . length > 0 ;
76+ const ownerProfilesQuery = useUsersBatchQuery ( ownerMemberPubkeys , {
77+ enabled : shouldResolveAgentOwnership ,
78+ } ) ;
79+ const canManageOwnedAgentChannel = ownerMemberPubkeys . some ( ( pubkey ) =>
80+ ownsAuthorAgent (
81+ ownerProfilesQuery . data ?. profiles [ normalizePubkey ( pubkey ) ] ,
82+ currentPubkey ,
83+ ) ,
84+ ) ;
85+
86+ return {
87+ canDeleteChannel : selfRole === "owner" || canManageOwnedAgentChannel ,
88+ canManageChannel :
89+ selfRole === "owner" ||
90+ selfRole === "admin" ||
91+ canManageOwnedAgentChannel ,
92+ error : shouldResolveAgentOwnership ? ownerProfilesQuery . error : null ,
93+ isLoading : shouldResolveAgentOwnership && ownerProfilesQuery . isLoading ,
94+ } ;
95+ }
96+
97+ export function ChannelDeleteConfirmationDialog ( {
98+ channelName,
99+ error,
100+ isPending,
101+ onConfirm,
102+ onOpenChange,
103+ open,
104+ trigger,
105+ } : ChannelDeleteConfirmationDialogProps ) {
106+ return (
107+ < AlertDialog onOpenChange = { onOpenChange } open = { open } >
108+ { trigger ? (
109+ < AlertDialogTrigger asChild > { trigger } </ AlertDialogTrigger >
110+ ) : null }
111+ < AlertDialogContent data-testid = "channel-delete-confirmation-dialog" >
112+ < AlertDialogHeader >
113+ < AlertDialogTitle > Delete channel?</ AlertDialogTitle >
114+ < AlertDialogDescription >
115+ Delete { channelName } from the community list. This action cannot be
116+ undone.
117+ </ AlertDialogDescription >
118+ </ AlertDialogHeader >
119+ { error instanceof Error ? (
120+ < p className = "text-sm text-destructive" > { error . message } </ p >
121+ ) : null }
122+ < AlertDialogFooter >
123+ < AlertDialogCancel asChild >
124+ < Button
125+ data-testid = "channel-delete-cancel"
126+ disabled = { isPending }
127+ type = "button"
128+ variant = "outline"
129+ >
130+ Cancel
131+ </ Button >
132+ </ AlertDialogCancel >
133+ < AlertDialogAction asChild >
134+ < Button
135+ data-testid = "channel-delete-confirm"
136+ disabled = { isPending }
137+ onClick = { ( event ) => {
138+ event . preventDefault ( ) ;
139+ onConfirm ( ) ;
140+ } }
141+ type = "button"
142+ variant = "destructive"
143+ >
144+ { isPending ? "Deleting..." : "Delete channel" }
145+ </ Button >
146+ </ AlertDialogAction >
147+ </ AlertDialogFooter >
148+ </ AlertDialogContent >
149+ </ AlertDialog >
150+ ) ;
151+ }
152+
37153export function ChannelManagementModerationActions ( {
38154 archiveChannelMutation,
39155 canManageChannel,
@@ -105,11 +221,16 @@ export function ChannelManagementModerationActions({
105221 </ Button >
106222 ) }
107223 { canDeleteChannel ? (
108- < AlertDialog
224+ < ChannelDeleteConfirmationDialog
225+ channelName = { resolvedChannelName }
226+ error = { deleteChannelMutation . error }
227+ isPending = { deleteChannelMutation . isPending }
228+ onConfirm = { ( ) => {
229+ void handleDeleteChannel ( ) ;
230+ } }
109231 onOpenChange = { handleDeleteDialogOpenChange }
110232 open = { isDeleteDialogOpen }
111- >
112- < AlertDialogTrigger asChild >
233+ trigger = {
113234 < Button
114235 aria-label = "Delete channel"
115236 data-testid = "channel-management-delete"
@@ -121,50 +242,8 @@ export function ChannelManagementModerationActions({
121242 >
122243 < Trash2 className = "h-4 w-4" />
123244 </ Button >
124- </ AlertDialogTrigger >
125- < AlertDialogContent data-testid = "channel-delete-confirmation-dialog" >
126- < AlertDialogHeader >
127- < AlertDialogTitle > Delete channel?</ AlertDialogTitle >
128- < AlertDialogDescription >
129- Delete { resolvedChannelName } from the community list. This
130- action cannot be undone.
131- </ AlertDialogDescription >
132- </ AlertDialogHeader >
133- { deleteChannelMutation . error instanceof Error ? (
134- < p className = "text-sm text-destructive" >
135- { deleteChannelMutation . error . message }
136- </ p >
137- ) : null }
138- < AlertDialogFooter >
139- < AlertDialogCancel asChild >
140- < Button
141- data-testid = "channel-delete-cancel"
142- disabled = { deleteChannelMutation . isPending }
143- type = "button"
144- variant = "outline"
145- >
146- Cancel
147- </ Button >
148- </ AlertDialogCancel >
149- < AlertDialogAction asChild >
150- < Button
151- data-testid = "channel-delete-confirm"
152- disabled = { deleteChannelMutation . isPending }
153- onClick = { ( event ) => {
154- event . preventDefault ( ) ;
155- void handleDeleteChannel ( ) ;
156- } }
157- type = "button"
158- variant = "destructive"
159- >
160- { deleteChannelMutation . isPending
161- ? "Deleting..."
162- : "Delete channel" }
163- </ Button >
164- </ AlertDialogAction >
165- </ AlertDialogFooter >
166- </ AlertDialogContent >
167- </ AlertDialog >
245+ }
246+ />
168247 ) : null }
169248 </ div >
170249 ) ;
0 commit comments