Skip to content

Commit 9b64974

Browse files
baxennpub1qvn3cujt28pg06ehlstrxyz6ayzp06t4uc7r566vxwwgrv24hglq9zju0n
andauthored
feat(desktop): create channels in sections (#1996)
Signed-off-by: npub1qvn3cujt28pg06ehlstrxyz6ayzp06t4uc7r566vxwwgrv24hglq9zju0n <03271c724b51c287eb37fc1633105ae90417e975e63c3a6b4c339c81b155ba3e@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qvn3cujt28pg06ehlstrxyz6ayzp06t4uc7r566vxwwgrv24hglq9zju0n <03271c724b51c287eb37fc1633105ae90417e975e63c3a6b4c339c81b155ba3e@sprout-oss.stage.blox.sqprod.co>
1 parent f06b59f commit 9b64974

5 files changed

Lines changed: 183 additions & 38 deletions

File tree

‎desktop/src/app/AppShell.tsx‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import { Outlet, useLocation } from "@tanstack/react-router";
44

55
import { deriveShellRoute } from "@/app/AppShell.helpers";
66
import { AppShellProvider } from "@/app/AppShellContext";
7-
import {
8-
AppShellOverlays,
9-
type BrowseDialogType,
10-
} from "@/app/AppShellOverlays";
7+
import { AppShellOverlays } from "@/app/AppShellOverlays";
118
import { AppTopChrome } from "@/app/AppTopChrome";
129
import { useAppNavigation } from "@/app/navigation/useAppNavigation";
1310
import { useBackForwardControls } from "@/app/navigation/useBackForwardControls";
1411
import { useLiveHomeFeedActions } from "@/app/useLiveHomeFeedActions";
12+
import { useChannelBrowserDialog } from "@/app/useChannelBrowserDialog";
1513
import { useMarkAsReadShortcuts } from "@/app/useMarkAsReadShortcuts";
1614
import { useSettingsShortcuts } from "@/app/useSettingsShortcuts";
1715
import { useAppShellDesktopNotifications } from "@/app/useAppShellDesktopNotifications";
@@ -110,8 +108,6 @@ export function AppShell() {
110108
null,
111109
);
112110
const [searchFocusRequest, setSearchFocusRequest] = React.useState(0);
113-
const [browseDialogType, setBrowseDialogType] =
114-
React.useState<BrowseDialogType>(null);
115111
const [isCreateChannelOpen, setIsCreateChannelOpen] = React.useState(false);
116112
const [isSendFeedbackOpen, setIsSendFeedbackOpen] = React.useState(false);
117113
const [isHuddleDrawerOpen, setIsHuddleDrawerOpen] = React.useState(false);
@@ -415,27 +411,22 @@ export function AppShell() {
415411
[unfollowThread, muteThread],
416412
);
417413

418-
const createChannelMutation = useCreateChannelMutation();
419-
const createForumMutation = useCreateChannelMutation();
414+
const createChannelMutation = useCreateChannelMutation(),
415+
createForumMutation = useCreateChannelMutation();
420416
const { applyCanvas, applyAgents } = useApplyTemplate();
421-
422417
const openDmMutation = useOpenDmMutation();
423418
const hideDmMutation = useHideDmMutation();
424-
const handleOpenBrowseChannels = React.useCallback(() => {
425-
setBrowseDialogType("stream");
426-
void refetchChannels();
427-
}, [refetchChannels]);
419+
const {
420+
browseDialogType,
421+
openBrowseChannels: handleOpenBrowseChannels,
422+
onBrowseDialogOpenChange: handleBrowseDialogOpenChange,
423+
getCreateSuccess,
424+
} = useChannelBrowserDialog(() => void refetchChannels());
428425
const handleOpenSearch = React.useCallback(() => {
429426
setSearchFocusRequest((request) => request + 1);
430427
void refetchChannels();
431428
}, [refetchChannels]);
432429

433-
const handleBrowseDialogOpenChange = React.useCallback((open: boolean) => {
434-
if (!open) {
435-
setBrowseDialogType(null);
436-
}
437-
}, []);
438-
439430
const handleBrowseChannelJoin = React.useCallback(
440431
async (channelId: string) => {
441432
await joinChannel(channelId);
@@ -445,19 +436,22 @@ export function AppShell() {
445436
);
446437

447438
const handleCreateChannel = React.useCallback(
448-
async ({
449-
description,
450-
name,
451-
visibility,
452-
ttlSeconds,
453-
templateId,
454-
}: {
455-
name: string;
456-
description?: string;
457-
visibility: ChannelVisibility;
458-
ttlSeconds?: number;
459-
templateId?: string;
460-
}) => {
439+
async (
440+
{
441+
description,
442+
name,
443+
visibility,
444+
ttlSeconds,
445+
templateId,
446+
}: {
447+
name: string;
448+
description?: string;
449+
visibility: ChannelVisibility;
450+
ttlSeconds?: number;
451+
templateId?: string;
452+
},
453+
onCreated?: (channelId: string) => void,
454+
) => {
461455
const createdChannel = await createChannelMutation.mutateAsync({
462456
name,
463457
description,
@@ -468,6 +462,7 @@ export function AppShell() {
468462

469463
await applyCanvas(templateId, createdChannel.id, name);
470464
await goChannel(createdChannel.id);
465+
onCreated?.(createdChannel.id);
471466
void applyAgents(templateId, createdChannel.id);
472467
},
473468
[applyAgents, applyCanvas, createChannelMutation, goChannel],
@@ -515,10 +510,15 @@ export function AppShell() {
515510
if (browseDialogType === "forum") {
516511
await handleCreateForum(input);
517512
} else {
518-
await handleCreateChannel(input);
513+
await handleCreateChannel(input, getCreateSuccess() ?? undefined);
519514
}
520515
},
521-
[browseDialogType, handleCreateChannel, handleCreateForum],
516+
[
517+
browseDialogType,
518+
handleCreateChannel,
519+
handleCreateForum,
520+
getCreateSuccess,
521+
],
522522
);
523523

524524
const handleHideDm = React.useCallback(
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as React from "react";
2+
3+
import type { BrowseDialogType } from "@/app/AppShellOverlays";
4+
5+
type CreatedCallback = (channelId: string) => void;
6+
7+
export function useChannelBrowserDialog(onOpen: () => void) {
8+
const [browseDialogType, setBrowseDialogType] =
9+
React.useState<BrowseDialogType>(null);
10+
const createSuccessRef = React.useRef<CreatedCallback | null>(null);
11+
12+
const openBrowseChannels = React.useCallback(
13+
(onCreated?: CreatedCallback) => {
14+
createSuccessRef.current = onCreated ?? null;
15+
setBrowseDialogType("stream");
16+
onOpen();
17+
},
18+
[onOpen],
19+
);
20+
21+
const onBrowseDialogOpenChange = React.useCallback((open: boolean) => {
22+
if (!open) {
23+
createSuccessRef.current = null;
24+
setBrowseDialogType(null);
25+
}
26+
}, []);
27+
28+
const getCreateSuccess = React.useCallback(
29+
() => createSuccessRef.current,
30+
[],
31+
);
32+
33+
return {
34+
browseDialogType,
35+
openBrowseChannels,
36+
onBrowseDialogOpenChange,
37+
getCreateSuccess,
38+
};
39+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ type AppSidebarProps = {
128128
lastMessageAt: string | null | undefined,
129129
) => void;
130130
onMarkAllChannelsRead: () => void;
131-
onBrowseChannels?: () => void;
131+
onBrowseChannels?: (onCreated?: (channelId: string) => void) => void;
132132
onOpenDm: (input: { pubkeys: string[] }) => Promise<void>;
133133
onUpdateCommunity: (
134134
id: string,
@@ -530,6 +530,13 @@ export function AppSidebar({
530530
openCreateDialog("stream");
531531
}, [onCreateChannelOpenChange, openCreateDialog]);
532532

533+
const handleCreateChannelInSection = React.useCallback(
534+
(sectionId: string) => {
535+
onBrowseChannels?.((channelId) => assignChannel(channelId, sectionId));
536+
},
537+
[assignChannel, onBrowseChannels],
538+
);
539+
533540
return (
534541
<Sidebar
535542
className="!border-r-0"
@@ -667,6 +674,9 @@ export function AppSidebar({
667674
onAssignChannel={assignChannel}
668675
onUnassignChannel={unassignChannel}
669676
onCreateSectionForChannel={handleCreateSectionForChannel}
677+
onCreateChannel={() =>
678+
handleCreateChannelInSection(section.id)
679+
}
670680
onRenameSection={() => setRenameSectionTarget(section)}
671681
onDeleteSection={() => setDeleteSectionTarget(section)}
672682
onMoveSectionUp={() => moveSectionUp(section.id)}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ export function CustomChannelSection({
557557
onAssignChannel,
558558
onUnassignChannel,
559559
onCreateSectionForChannel,
560+
onCreateChannel,
560561
onRenameSection,
561562
onDeleteSection,
562563
onMoveSectionUp,
@@ -595,6 +596,7 @@ export function CustomChannelSection({
595596
onAssignChannel: (channelId: string, sectionId: string) => void;
596597
onUnassignChannel: (channelId: string) => void;
597598
onCreateSectionForChannel: (channelId: string) => void;
599+
onCreateChannel: () => void;
598600
onRenameSection: () => void;
599601
onDeleteSection: () => void;
600602
onMoveSectionUp: () => void;
@@ -664,6 +666,11 @@ export function CustomChannelSection({
664666
</button>
665667
</SidebarGroupLabel>
666668
<div className="absolute right-1 top-1/2 z-10 flex -translate-y-1/2 items-center gap-0.5">
669+
<SectionQuickAction
670+
label={`Add channel to ${section.name}`}
671+
onClick={onCreateChannel}
672+
testId={`section-actions-${section.id}-quick-create`}
673+
/>
667674
<SectionActionsMenu
668675
sectionLabel={section.name}
669676
testId={`section-actions-${section.id}`}

‎desktop/tests/e2e/channel-browser.spec.ts‎

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,29 @@
1-
import { expect, test } from "@playwright/test";
1+
import { expect, test, type Page } from "@playwright/test";
22

33
import { installMockBridge, openChannelBrowser } from "../helpers/bridge";
44

5-
test.beforeEach(async ({ page }) => {
6-
await installMockBridge(page);
5+
const MOCK_PUBKEY = "deadbeef".repeat(8);
6+
const CUSTOM_SECTION = { id: "sec-projects", name: "Projects", order: 0 };
7+
8+
async function seedCustomSection(page: Page) {
9+
await page.addInitScript(
10+
({ pubkey, section }) => {
11+
window.localStorage.setItem(
12+
`buzz-channel-sections.v1:${pubkey}`,
13+
JSON.stringify({ version: 1, sections: [section], assignments: {} }),
14+
);
15+
},
16+
{ pubkey: MOCK_PUBKEY, section: CUSTOM_SECTION },
17+
);
18+
}
19+
20+
test.beforeEach(async ({ page }, testInfo) => {
21+
await installMockBridge(
22+
page,
23+
testInfo.title.includes("failed section create")
24+
? { createChannelErrors: ["Create failed"] }
25+
: undefined,
26+
);
727
});
828

929
test("keyboard shortcut opens the channel browser dialog", async ({ page }) => {
@@ -172,6 +192,75 @@ test("sidebar add-channel button opens the browser", async ({ page }) => {
172192
await expect(page.getByTestId("channel-browser-dialog")).toBeVisible();
173193
});
174194

195+
test("custom section add button creates directly into that section", async ({
196+
page,
197+
}) => {
198+
await seedCustomSection(page);
199+
await page.goto("/");
200+
201+
const addButton = page.getByTestId(
202+
`section-actions-${CUSTOM_SECTION.id}-quick-create`,
203+
);
204+
await expect(addButton).toHaveAccessibleName("Add channel to Projects");
205+
await addButton.click();
206+
await expect(page.getByTestId("channel-browser-dialog")).toBeVisible();
207+
208+
const channelName = `section-created-${Date.now()}`;
209+
await page.getByTestId("channel-browser-search").fill(channelName);
210+
await page.getByTestId("channel-browser-create-row").click();
211+
await page.getByTestId("create-channel-submit").click();
212+
213+
await expect(page.getByTestId("channel-browser-dialog")).not.toBeVisible();
214+
await expect(
215+
page.getByTestId(`section-title-${CUSTOM_SECTION.id}`),
216+
).toBeVisible();
217+
await expect(page.getByTestId(`channel-${channelName}`)).toBeVisible();
218+
await expect(page.getByTestId("stream-list")).not.toContainText(channelName);
219+
});
220+
221+
test("canceling section create does not affect the next global create", async ({
222+
page,
223+
}) => {
224+
await seedCustomSection(page);
225+
await page.goto("/");
226+
227+
await page
228+
.getByTestId(`section-actions-${CUSTOM_SECTION.id}-quick-create`)
229+
.click();
230+
await page.keyboard.press("Escape");
231+
await expect(page.getByTestId("channel-browser-dialog")).not.toBeVisible();
232+
233+
await page.getByTestId("section-actions-channels-quick-create").click();
234+
const channelName = `global-after-cancel-${Date.now()}`;
235+
await page.getByTestId("channel-browser-search").fill(channelName);
236+
await page.getByTestId("channel-browser-create-row").click();
237+
await page.getByTestId("create-channel-submit").click();
238+
239+
await expect(page.getByTestId("stream-list")).toContainText(channelName);
240+
});
241+
242+
test("failed section create retry still assigns to the section", async ({
243+
page,
244+
}) => {
245+
await seedCustomSection(page);
246+
await page.goto("/");
247+
248+
await page
249+
.getByTestId(`section-actions-${CUSTOM_SECTION.id}-quick-create`)
250+
.click();
251+
const channelName = `section-retry-${Date.now()}`;
252+
await page.getByTestId("channel-browser-search").fill(channelName);
253+
await page.getByTestId("channel-browser-create-row").click();
254+
await page.getByTestId("create-channel-submit").click();
255+
await expect(page.getByText("Create failed")).toBeVisible();
256+
257+
await page.getByTestId("create-channel-submit").click();
258+
259+
await expect(page.getByTestId("channel-browser-dialog")).not.toBeVisible();
260+
await expect(page.getByTestId(`channel-${channelName}`)).toBeVisible();
261+
await expect(page.getByTestId("stream-list")).not.toContainText(channelName);
262+
});
263+
175264
test("create affordance is visible on open before typing", async ({ page }) => {
176265
await page.goto("/");
177266

0 commit comments

Comments
 (0)