Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

Extract SidebarOpenInSplitMenu component for reusable split actions (#5386)

Jacob Beckerman1c17688d2429
diff --git a/apps/web/src/components/app/app-sidebar/sidebar.tsx b/apps/web/src/components/app/app-sidebar/sidebar.tsx
index 1817982cec..2155face39 100644
--- a/apps/web/src/components/app/app-sidebar/sidebar.tsx
+++ b/apps/web/src/components/app/app-sidebar/sidebar.tsx
@@ -1607,45 +1607,42 @@ interface SidebarLinkProps extends SidebarItem {
removeAction?: { tooltip: string; onRemove: () => void };
}
-const SidebarLink = (props: SidebarLinkProps) => {
- const [isHovering, setIsHovering] = createSignal(false);
+/** Which action of {@link SidebarOpenInSplitMenu} placed the content. */
+type SidebarOpenAction = 'current-split' | 'new-split' | 'fullscreen';
+
+interface SidebarOpenInSplitMenuProps {
+ /** The content the menu's actions open. */
+ content: () => SplitContent;
+ /**
+ * Runs once an action has placed the content in a split — e.g. the Email
+ * account rows scope the freshly opened mail list to their inbox.
+ */
+ onOpened?: (split: SplitHandle, action: SidebarOpenAction) => void;
+ onOpenChange?: (open: boolean) => void;
+ children: JSX.Element;
+}
+/**
+ * The shared sidebar right-click menu: open the row's content in the current
+ * split, in a new split, or fullscreen. Wraps any sidebar row — the top-level
+ * links and the nested Email account rows both use it.
+ */
+const SidebarOpenInSplitMenu = (props: SidebarOpenInSplitMenuProps) => {
const analytics = useAnalytics();
const layout = useSplitLayout();
- const location = useLocation();
-
- // Always read the manager signal live: it is undefined until the split
- // layout mounts, which happens after the sidebar.
- const isActive = () => {
- const activeContent = globalSplitManager()?.activeSplit()?.content();
-
- // In case we can't match on the active split, use the url path to determine
- // if this link is active
- if (!activeContent) {
- const paths = location.pathname.split('/').filter(Boolean);
- return paths.includes(props.id);
- }
-
- return activeContent?.id === props.id;
- };
-
- const content = () =>
- ({
- type: 'component',
- id: props.id,
- params: props.params,
- }) as const;
const canOpenInNewSplit = () =>
globalSplitManager()?.canAppendSplit() ?? true;
const canOpenFullscreen = () => layout.getSplitCount() > 1;
- const openInCurrentSplit = () =>
- layout.openWithSplit(content(), {
+ const openInCurrentSplit = () => {
+ const split = layout.openWithSplit(props.content(), {
allowDuplicate: true,
mergeHistory: false,
referredFrom: 'sidebar',
});
+ if (split) props.onOpened?.(split, 'current-split');
+ };
const openInNewSplit = () => {
const manager = globalSplitManager();
@@ -1653,181 +1650,27 @@ const SidebarLink = (props: SidebarLinkProps) => {
analytics.track('split_created', { from: 'sidebar' });
- manager.createNewSplit({
- content: content(),
+ const split = manager.createNewSplit({
+ content: props.content(),
activate: true,
allowDuplicate: true,
referredFrom: 'sidebar',
});
+ props.onOpened?.(split, 'new-split');
};
const openFullscreen = () => {
- const split = layout.replaceAllSplits(content(), {
+ const split = layout.replaceAllSplits(props.content(), {
referredFrom: 'sidebar',
});
- if (props.id === 'search' && split) requestSearchFocus(split.id);
+ if (split) props.onOpened?.(split, 'fullscreen');
globalSplitManager()?.returnFocus();
};
return (
- <ContextMenu onOpenChange={props.onContextMenuOpenChange}>
+ <ContextMenu onOpenChange={props.onOpenChange}>
<ContextMenu.Trigger class="w-full h-7">
- <NavRow
- draggable={false}
- data-sidebar-link={props.id}
- data-active={isActive() ? '' : undefined}
- active={isActive() && !props.suppressActiveStyle}
- class="h-7"
- fullWidth
- tooltipPlacement="right"
- onMouseEnter={() => setIsHovering(true)}
- label={`Go to ${props.label}`}
- hotkey={
- props.standaloneHotkey
- ? props.hotkeyToken
- : [TOKENS.sidebar.goToLeader, props.hotkeyToken]
- }
- tooltipDisabled={props.sidebarState !== 'slim'}
- onMouseLeave={() => setIsHovering(false)}
- onMouseDown={(e) => {
- if (e.button !== 0) return;
- analytics.track('sidebar_click', {
- view: props.id,
- });
-
- e.preventDefault();
- let currentContentHandle = globalSplitManager()?.activeSplit();
-
- const currentContent = currentContentHandle?.content();
- const isSameContent =
- currentContent?.type === 'component' &&
- currentContent?.id === props.id;
-
- if (!isSameContent || e.shiftKey) {
- currentContentHandle = navigateToSidebarView({
- viewId: props.id,
- params: props.params,
- shiftKey: e.shiftKey,
- activeSplit: currentContentHandle,
- openWithSplit: layout.openWithSplit,
- referredFrom: 'sidebar',
- });
- } else {
- props.onActiveClick?.();
- }
-
- if (props.id === 'search' && currentContentHandle) {
- requestSearchFocus(currentContentHandle.id);
- }
-
- globalSplitManager()?.returnFocus();
- }}
- >
- <Show when={props.icon}>
- <div class="size-5 shrink-0 flex items-center justify-center [&_svg]:size-3.5">
- <Show
- when={
- isHovering() && props.sidebarState !== 'slim'
- ? props.removeAction
- : undefined
- }
- fallback={
- <Dynamic
- component={props.icon}
- triggerAnimation={isHovering()}
- />
- }
- >
- {(removeAction) => (
- <Tooltip
- label={removeAction().tooltip}
- as="span"
- placement="top"
- >
- <span
- role="button"
- tabIndex={0}
- aria-label={removeAction().tooltip}
- class="flex items-center justify-center text-ink-muted
- rounded-md hover:bg-failure hover:text-surface p-1"
- onMouseDown={(e) => {
- // The row navigates on mousedown; the X must not.
- e.stopPropagation();
- e.preventDefault();
- }}
- onClick={(e) => {
- e.stopPropagation();
- removeAction().onRemove();
- }}
- onKeyDown={(e) => {
- if (e.key === 'Enter' || e.key === ' ') {
- e.preventDefault();
- e.stopPropagation();
- removeAction().onRemove();
- }
- }}
- >
- <XIcon />
- </span>
- </Tooltip>
- )}
- </Show>
- </div>
- </Show>
-
- <div class="flex items-center gap-1 group-data-[slim=true]/sidebar:hidden">
- <span class="whitespace-nowrap">{props.label}</span>
- </div>
-
- <Show
- when={
- isActive() &&
- props.trailingWhenActive !== undefined &&
- !props.hotkeyVisible
- }
- >
- <div class="group-data-[slim=true]/sidebar:hidden ml-auto flex items-center text-ink-muted">
- {props.trailingWhenActive}
- </div>
- </Show>
-
- <Show
- when={
- isHovering() &&
- !props.hotkeyVisible &&
- !(isActive() && props.trailingWhenActive !== undefined)
- }
- >
- <div class="group-data-[slim=true]/sidebar:hidden ml-auto">
- <div class="flex gap-1 items-center text-ink-extra-muted font-normal text-xxs">
- <Show when={!props.standaloneHotkey}>
- <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
- <Hotkey token={TOKENS.sidebar.goToLeader} />
- </div>
- <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
- <Hotkey token={props.hotkeyToken} />
- </div>
- </Show>
- <Show when={props.standaloneHotkey}>
- <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
- <Hotkey token={props.hotkeyToken} />
- </div>
- </Show>
- </div>
- </div>
- </Show>
- <Show when={props.hotkeyVisible}>
- <div
- class={cn(
- 'text-xs size-4 rounded-xs flex items-center justify-center overflow-hidden bg-accent/10 border border-accent/30 text-accent',
- props.sidebarState === 'slim' && 'absolute -bottom-1 -right-1',
- props.sidebarState !== 'slim' && 'relative p-1 ml-auto'
- )}
- >
- <Hotkey token={props.hotkeyToken} />
- </div>
- </Show>
- </NavRow>
+ {props.children}
</ContextMenu.Trigger>
<ContextMenu.Portal>
@@ -1847,6 +1690,205 @@ const SidebarLink = (props: SidebarLinkProps) => {
);
};
+const SidebarLink = (props: SidebarLinkProps) => {
+ const [isHovering, setIsHovering] = createSignal(false);
+
+ const analytics = useAnalytics();
+ const layout = useSplitLayout();
+
+ const location = useLocation();
+
+ // Always read the manager signal live: it is undefined until the split
+ // layout mounts, which happens after the sidebar.
+ const isActive = () => {
+ const activeContent = globalSplitManager()?.activeSplit()?.content();
+
+ // In case we can't match on the active split, use the url path to determine
+ // if this link is active
+ if (!activeContent) {
+ const paths = location.pathname.split('/').filter(Boolean);
+ return paths.includes(props.id);
+ }
+
+ return activeContent?.id === props.id;
+ };
+
+ const content = () =>
+ ({
+ type: 'component',
+ id: props.id,
+ params: props.params,
+ }) as const;
+
+ return (
+ <SidebarOpenInSplitMenu
+ content={content}
+ onOpenChange={props.onContextMenuOpenChange}
+ onOpened={(split, action) => {
+ if (action === 'fullscreen' && props.id === 'search')
+ requestSearchFocus(split.id);
+ }}
+ >
+ <NavRow
+ draggable={false}
+ data-sidebar-link={props.id}
+ data-active={isActive() ? '' : undefined}
+ active={isActive() && !props.suppressActiveStyle}
+ class="h-7"
+ fullWidth
+ tooltipPlacement="right"
+ onMouseEnter={() => setIsHovering(true)}
+ label={`Go to ${props.label}`}
+ hotkey={
+ props.standaloneHotkey
+ ? props.hotkeyToken
+ : [TOKENS.sidebar.goToLeader, props.hotkeyToken]
+ }
+ tooltipDisabled={props.sidebarState !== 'slim'}
+ onMouseLeave={() => setIsHovering(false)}
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ analytics.track('sidebar_click', {
+ view: props.id,
+ });
+
+ e.preventDefault();
+ let currentContentHandle = globalSplitManager()?.activeSplit();
+
+ const currentContent = currentContentHandle?.content();
+ const isSameContent =
+ currentContent?.type === 'component' &&
+ currentContent?.id === props.id;
+
+ if (!isSameContent || e.shiftKey) {
+ currentContentHandle = navigateToSidebarView({
+ viewId: props.id,
+ params: props.params,
+ shiftKey: e.shiftKey,
+ activeSplit: currentContentHandle,
+ openWithSplit: layout.openWithSplit,
+ referredFrom: 'sidebar',
+ });
+ } else {
+ props.onActiveClick?.();
+ }
+
+ if (props.id === 'search' && currentContentHandle) {
+ requestSearchFocus(currentContentHandle.id);
+ }
+
+ globalSplitManager()?.returnFocus();
+ }}
+ >
+ <Show when={props.icon}>
+ <div class="size-5 shrink-0 flex items-center justify-center [&_svg]:size-3.5">
+ <Show
+ when={
+ isHovering() && props.sidebarState !== 'slim'
+ ? props.removeAction
+ : undefined
+ }
+ fallback={
+ <Dynamic
+ component={props.icon}
+ triggerAnimation={isHovering()}
+ />
+ }
+ >
+ {(removeAction) => (
+ <Tooltip
+ label={removeAction().tooltip}
+ as="span"
+ placement="top"
+ >
+ <span
+ role="button"
+ tabIndex={0}
+ aria-label={removeAction().tooltip}
+ class="flex items-center justify-center text-ink-muted
+ rounded-md hover:bg-failure hover:text-surface p-1"
+ onMouseDown={(e) => {
+ // The row navigates on mousedown; the X must not.
+ e.stopPropagation();
+ e.preventDefault();
+ }}
+ onClick={(e) => {
+ e.stopPropagation();
+ removeAction().onRemove();
+ }}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter' || e.key === ' ') {
+ e.preventDefault();
+ e.stopPropagation();
+ removeAction().onRemove();
+ }
+ }}
+ >
+ <XIcon />
+ </span>
+ </Tooltip>
+ )}
+ </Show>
+ </div>
+ </Show>
+
+ <div class="flex items-center gap-1 group-data-[slim=true]/sidebar:hidden">
+ <span class="whitespace-nowrap">{props.label}</span>
+ </div>
+
+ <Show
+ when={
+ isActive() &&
+ props.trailingWhenActive !== undefined &&
+ !props.hotkeyVisible
+ }
+ >
+ <div class="group-data-[slim=true]/sidebar:hidden ml-auto flex items-center text-ink-muted">
+ {props.trailingWhenActive}
+ </div>
+ </Show>
+
+ <Show
+ when={
+ isHovering() &&
+ !props.hotkeyVisible &&
+ !(isActive() && props.trailingWhenActive !== undefined)
+ }
+ >
+ <div class="group-data-[slim=true]/sidebar:hidden ml-auto">
+ <div class="flex gap-1 items-center text-ink-extra-muted font-normal text-xxs">
+ <Show when={!props.standaloneHotkey}>
+ <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
+ <Hotkey token={TOKENS.sidebar.goToLeader} />
+ </div>
+ <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
+ <Hotkey token={props.hotkeyToken} />
+ </div>
+ </Show>
+ <Show when={props.standaloneHotkey}>
+ <div class="text-xxs text-ink-extra-muted rounded-sm ml-auto border border-ink/5 px-1.5 py-0.5 -my-1">
+ <Hotkey token={props.hotkeyToken} />
+ </div>
+ </Show>
+ </div>
+ </div>
+ </Show>
+ <Show when={props.hotkeyVisible}>
+ <div
+ class={cn(
+ 'text-xs size-4 rounded-xs flex items-center justify-center overflow-hidden bg-accent/10 border border-accent/30 text-accent',
+ props.sidebarState === 'slim' && 'absolute -bottom-1 -right-1',
+ props.sidebarState !== 'slim' && 'relative p-1 ml-auto'
+ )}
+ >
+ <Hotkey token={props.hotkeyToken} />
+ </div>
+ </Show>
+ </NavRow>
+ </SidebarOpenInSplitMenu>
+ );
+};
+
/**
* The Email sidebar link, acting as a dropdown for the user's linked inboxes.
* With multiple inboxes linked, the active link swaps its hotkey hint for a
@@ -1863,6 +1905,10 @@ const SidebarLink = (props: SidebarLinkProps) => {
* only when it is the single selected inbox (read from the live mail view, or
* from the filter its history entry captured when something else is on top),
* in which case the parent link yields its own.
+ *
+ * Each row carries the same right-click menu as the parent link (open in the
+ * current split, a new split, or fullscreen), scoping whichever split it opens
+ * to that inbox.
*/
const SidebarMailLink = (props: SidebarLinkProps) => {
const layout = useSplitLayout();
@@ -1880,6 +1926,14 @@ const SidebarMailLink = (props: SidebarLinkProps) => {
const isMailList = (content: SplitContent | undefined) =>
content?.type === 'component' && content.id === 'mail';
+ /** The mail list content an account row's right-click menu opens. */
+ const mailContent = () =>
+ ({
+ type: 'component',
+ id: props.id,
+ params: props.params,
+ }) as const;
+
const canShow = () => props.sidebarState === 'expanded' && links().length > 1;
const showAccounts = () => canShow() && expanded();
@@ -1978,28 +2032,40 @@ const SidebarMailLink = (props: SidebarLinkProps) => {
: '0ms',
}}
>
- <NavRow
- draggable={false}
- disabled={!expanded()}
- data-sidebar-mail-account={link.email_address}
- data-active={onlySelectedId() === link.id ? '' : undefined}
- active={onlySelectedId() === link.id}
- class="h-7 pl-6 pr-2"
- onMouseDown={(e) => {
- if (e.button !== 0) return;
- e.preventDefault();
- selectOnly(link.id);
- }}
+ <SidebarOpenInSplitMenu
+ content={mailContent}
+ // Keeps the hover overlay open over a slim sidebar while
+ // the menu is up, same as the parent link's menu.
+ onOpenChange={props.onContextMenuOpenChange}
+ onOpened={(split) =>
+ requestInboxFilter(split.id, [link.id])
+ }
>
- <UserIcon
- {...inboxIconProps(link.email_address)}
- photoUrl={link.photo_url ?? undefined}
- size="sm"
- suppressClick
- showTooltip={false}
- />
- <span class="truncate">{link.email_address}</span>
- </NavRow>
+ <NavRow
+ draggable={false}
+ disabled={!expanded()}
+ data-sidebar-mail-account={link.email_address}
+ data-active={
+ onlySelectedId() === link.id ? '' : undefined
+ }
+ active={onlySelectedId() === link.id}
+ class="h-7 pl-6 pr-2"
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ e.preventDefault();
+ selectOnly(link.id);
+ }}
+ >
+ <UserIcon
+ {...inboxIconProps(link.email_address)}
+ photoUrl={link.photo_url ?? undefined}
+ size="sm"
+ suppressClick
+ showTooltip={false}
+ />
+ <span class="truncate">{link.email_address}</span>
+ </NavRow>
+ </SidebarOpenInSplitMenu>
</li>
)}
</For>