From 22caeb7405d6e9edd9ac410cefb1603dbd076e9c Mon Sep 17 00:00:00 2001 From: Taylor Ho Date: Sun, 23 Aug 2026 20:27:14 -0700 Subject: [PATCH] refactor(desktop): extract agent activity opening actions Co-authored-by: Rizz <302abe414ca6e3134763d2539bfcf145aea2a63fe5f8455204ed602fd40cf381@buzz.block.builderlab.xyz> Signed-off-by: Taylor Ho --- .../features/channels/ui/ChannelScreen.tsx | 36 +++------------ .../ui/useOpenAgentActivityActions.ts | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 31 deletions(-) create mode 100644 desktop/src/features/channels/ui/useOpenAgentActivityActions.ts diff --git a/desktop/src/features/channels/ui/ChannelScreen.tsx b/desktop/src/features/channels/ui/ChannelScreen.tsx index ab565ced57..604d313c2b 100644 --- a/desktop/src/features/channels/ui/ChannelScreen.tsx +++ b/desktop/src/features/channels/ui/ChannelScreen.tsx @@ -1,5 +1,4 @@ import * as React from "react"; -import { toast } from "sonner"; import { useAppShell } from "@/app/AppShellContext"; import { useAppNavigation } from "@/app/navigation/useAppNavigation"; import { useActiveChannelHeader } from "@/features/channels/useActiveChannelHeader"; @@ -58,7 +57,6 @@ import type { TimelineMessage } from "@/features/messages/types"; import { useUsersBatchQuery } from "@/features/profile/hooks"; import { useRelaySelfQuery } from "@/features/moderation/hooks"; import type { RelayEvent, RespondToMode } from "@/shared/api/types"; -import { openAgentActivityWindow } from "@/shared/api/agentActivityWindow"; import { isAgentActivityWindow } from "@/features/agents/lib/agentActivityWindow"; import { ChannelScreenLoadingFallback } from "@/features/channels/ui/ChannelScreenLoadingFallback"; import { @@ -79,6 +77,7 @@ import { normalizePubkey } from "@/shared/lib/pubkey"; import { useChannelActivityTyping } from "./useChannelActivityTyping"; import { useChannelAgentSessions } from "./useChannelAgentSessions"; import { useMessageProfiles } from "./useMessageProfiles"; +import { useOpenAgentActivityActions } from "./useOpenAgentActivityActions"; import { useChannelPanelHistoryState } from "./useChannelPanelHistoryState"; import { useChannelProfilePanel } from "./useChannelProfilePanel"; import { useChannelRouteTarget } from "./useChannelRouteTarget"; @@ -587,35 +586,10 @@ export function ChannelScreen({ setThreadReplyTargetId, setThreadScrollTargetId, }); - const handleOpenAgentActivityExternal = React.useCallback( - (pubkey: string, channelId?: string | null) => { - const destinationChannelId = channelId ?? activeChannelId; - if (!destinationChannelId) return; - - if (isAgentActivityWindow()) { - handleOpenAgentSession(pubkey, destinationChannelId); - return; - } - - void openAgentActivityWindow(destinationChannelId, pubkey) - .then((openedNativeWindow) => { - if (!openedNativeWindow) { - handleOpenAgentSession(pubkey, destinationChannelId); - } - }) - .catch((error) => { - console.error("Failed to open agent activity window:", error); - toast.error("Couldn't open the agent activity window."); - }); - }, - [activeChannelId, handleOpenAgentSession], - ); - const handleOpenAgentActivity = React.useCallback( - (pubkey: string, channelId?: string | null) => { - handleOpenAgentSession(pubkey, channelId ?? activeChannelId); - }, - [activeChannelId, handleOpenAgentSession], - ); + const { + openInApp: handleOpenAgentActivity, + openInExternalWindow: handleOpenAgentActivityExternal, + } = useOpenAgentActivityActions(activeChannelId, handleOpenAgentSession); const { handleOpenProfilePanel, handleCloseProfilePanel, handleOpenDm } = useChannelProfilePanel({ closeAgentSession: handleCloseAgentSession, diff --git a/desktop/src/features/channels/ui/useOpenAgentActivityActions.ts b/desktop/src/features/channels/ui/useOpenAgentActivityActions.ts new file mode 100644 index 0000000000..54558c41ba --- /dev/null +++ b/desktop/src/features/channels/ui/useOpenAgentActivityActions.ts @@ -0,0 +1,44 @@ +import * as React from "react"; +import { toast } from "sonner"; + +import { isAgentActivityWindow } from "@/features/agents/lib/agentActivityWindow"; +import { openAgentActivityWindow } from "@/shared/api/agentActivityWindow"; + +type OpenAgentSession = (pubkey: string, channelId?: string | null) => void; + +export function useOpenAgentActivityActions( + activeChannelId: string | null, + openAgentSession: OpenAgentSession, +) { + const openInExternalWindow = React.useCallback( + (pubkey: string, channelId?: string | null) => { + const destinationChannelId = channelId ?? activeChannelId; + if (!destinationChannelId) return; + + if (isAgentActivityWindow()) { + openAgentSession(pubkey, destinationChannelId); + return; + } + + void openAgentActivityWindow(destinationChannelId, pubkey) + .then((openedNativeWindow) => { + if (!openedNativeWindow) { + openAgentSession(pubkey, destinationChannelId); + } + }) + .catch((error) => { + console.error("Failed to open agent activity window:", error); + toast.error("Couldn't open the agent activity window."); + }); + }, + [activeChannelId, openAgentSession], + ); + const openInApp = React.useCallback( + (pubkey: string, channelId?: string | null) => { + openAgentSession(pubkey, channelId ?? activeChannelId); + }, + [activeChannelId, openAgentSession], + ); + + return { openInApp, openInExternalWindow }; +}