Skip to content

Commit 929d3aa

Browse files
authored
feat(desktop): add channel archive and delete actions (#2067)
Signed-off-by: Pavlenex <[email protected]>
1 parent 2cb7753 commit 929d3aa

7 files changed

Lines changed: 351 additions & 95 deletions

File tree

‎desktop/src/features/channels/ui/ChannelManagementModerationActions.tsx‎

Lines changed: 126 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { 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";
37
import { cn } from "@/shared/lib/cn";
8+
import { normalizePubkey } from "@/shared/lib/pubkey";
49
import {
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+
37153
export 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
);

‎desktop/src/features/channels/ui/ChannelManagementSheet.tsx‎

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ import {
3838
formatTtlDuration,
3939
parseTtlDuration,
4040
} from "@/features/channels/lib/ephemeralChannel";
41-
import { ownsAuthorAgent } from "@/features/profile/lib/identity";
42-
import { useUsersBatchQuery } from "@/features/profile/hooks";
4341
import type { Channel } from "@/shared/api/types";
4442
import { cn } from "@/shared/lib/cn";
45-
import { normalizePubkey } from "@/shared/lib/pubkey";
4643
import { useTheme } from "@/shared/theme/ThemeProvider";
4744
import { Button } from "@/shared/ui/button";
4845
import {
@@ -83,7 +80,10 @@ import {
8380
NarrativeGroup,
8481
ToggleRow,
8582
} from "./ChannelManagementSheetRows";
86-
import { ChannelManagementModerationActions } from "./ChannelManagementModerationActions";
83+
import {
84+
ChannelManagementModerationActions,
85+
useChannelModerationCapabilities,
86+
} from "./ChannelManagementModerationActions";
8787
import { writeTextToClipboard } from "@/shared/lib/clipboard";
8888

8989
type ChannelManagementSheetProps = {
@@ -137,37 +137,8 @@ export function ChannelManagementSheet({
137137
members.find((member) => member.pubkey === currentPubkey) ?? null;
138138
const hasResolvedMembership = membersQuery.data !== undefined;
139139

140-
// Collect owner-role member pubkeys to look up their NIP-OA ownerPubkey.
141-
// This is what surfaces the "you own the agent that owns this channel" path.
142-
const ownerMemberPubkeys = React.useMemo(
143-
() =>
144-
members
145-
.filter((m) => m.role === "owner" && m.pubkey !== currentPubkey)
146-
.map((m) => m.pubkey),
147-
[members, currentPubkey],
148-
);
149-
const ownerProfilesQuery = useUsersBatchQuery(ownerMemberPubkeys, {
150-
enabled: open && ownerMemberPubkeys.length > 0,
151-
});
152-
// True when an owner-role member of this channel is an agent owned by the
153-
// current user — mirrors the relay's is_agent_owner gate.
154-
const canManageOwnedAgentChannel = React.useMemo(() => {
155-
if (!currentPubkey || !ownerProfilesQuery.data) return false;
156-
return ownerMemberPubkeys.some((pubkey) =>
157-
ownsAuthorAgent(
158-
ownerProfilesQuery.data?.profiles[normalizePubkey(pubkey)],
159-
currentPubkey,
160-
),
161-
);
162-
}, [currentPubkey, ownerMemberPubkeys, ownerProfilesQuery.data]);
163-
164-
const isSelfOwner = selfMember?.role === "owner";
165-
// Capability: may delete this channel (self-owner OR owns the agent-owner).
166-
const canDeleteChannel = isSelfOwner || canManageOwnedAgentChannel;
167-
const canManageChannel =
168-
selfMember?.role === "owner" ||
169-
selfMember?.role === "admin" ||
170-
canManageOwnedAgentChannel;
140+
const { canDeleteChannel, canManageChannel } =
141+
useChannelModerationCapabilities(membersQuery.data, currentPubkey, open);
171142
const canEditNarrative =
172143
canManageChannel && selfMember !== null && detail?.channelType !== "dm";
173144
const isArchived =

‎desktop/src/features/sidebar/ui/AppSidebar.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
CreateSectionDialog,
2727
DeleteSectionAlertDialog,
2828
RenameSectionDialog,
29+
useDeleteChannelDialog,
2930
useLeaveChannelDialog,
3031
type SectionDialogValue,
3132
} from "@/features/sidebar/ui/ChannelSectionDialogs";
@@ -373,6 +374,10 @@ export function AppSidebar({
373374
React.useState<ChannelSection | null>(null);
374375
const { requestLeaveChannel, dialog: leaveChannelDialog } =
375376
useLeaveChannelDialog();
377+
const { requestDeleteChannel, dialog: deleteChannelDialog } =
378+
useDeleteChannelDialog((channel) => {
379+
if (channel.id === selectedChannelId) onSelectHome();
380+
});
376381

377382
const streamChannels = React.useMemo(
378383
() => channels.filter((channel) => channel.channelType === "stream"),
@@ -636,6 +641,7 @@ export function AppSidebar({
636641
starredChannelIds={starredChannelIds}
637642
onStarChannel={onStarChannel}
638643
onUnstarChannel={onUnstarChannel}
644+
onDeleteChannel={requestDeleteChannel}
639645
onLeaveChannel={requestLeaveChannel}
640646
/>
641647
) : null}
@@ -705,6 +711,7 @@ export function AppSidebar({
705711
starredChannelIds={starredChannelIds}
706712
onStarChannel={onStarChannel}
707713
onUnstarChannel={onUnstarChannel}
714+
onDeleteChannel={requestDeleteChannel}
708715
onLeaveChannel={requestLeaveChannel}
709716
/>
710717
))}
@@ -744,6 +751,7 @@ export function AppSidebar({
744751
starredChannelIds={starredChannelIds}
745752
onStarChannel={onStarChannel}
746753
onUnstarChannel={onUnstarChannel}
754+
onDeleteChannel={requestDeleteChannel}
747755
onLeaveChannel={requestLeaveChannel}
748756
/>
749757
</SidebarDndContext>
@@ -774,6 +782,7 @@ export function AppSidebar({
774782
mutedChannelIds={mutedChannelIds}
775783
onMuteChannel={onMuteChannel}
776784
onUnmuteChannel={onUnmuteChannel}
785+
onDeleteChannel={requestDeleteChannel}
777786
/>
778787
</FeatureGate>
779788
<SidebarSection
@@ -961,6 +970,7 @@ export function AppSidebar({
961970
setDeleteSectionTarget(null);
962971
}}
963972
/>
973+
{deleteChannelDialog}
964974
{leaveChannelDialog}
965975
<SidebarRail />
966976
</Sidebar>

0 commit comments

Comments
 (0)