Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
quick fix: preview tab switching bug (#297)
Peter Chinman3dd3c09bfbfb
diff --git a/js/app/packages/block-chat/component/Chat.tsx b/js/app/packages/block-chat/component/Chat.tsxindex 6879b396c6..9fb2663ad8 100644--- a/js/app/packages/block-chat/component/Chat.tsx+++ b/js/app/packages/block-chat/component/Chat.tsx@@ -18,8 +18,8 @@ import {storeChatState,} from '@core/component/AI/util/storage';import { usePaywallState } from '@core/constant/PaywallState';-import { registerHotkey } from '@core/hotkey/hotkeys';import { TOKENS } from '@core/hotkey/tokens';+import { registerScopeSignalHotkey } from '@core/hotkey/utils';import { createMethodRegistration } from '@core/orchestrator';import {blockElementSignal,@@ -148,30 +148,29 @@ export function Chat(props: { data: ChatData }) {},});- createEffect(() => {- if (scopeId()) {- registerHotkey({- hotkey: 'enter',- scopeId: scopeId(),- description: 'Focus Chat Input',- keyDownHandler: () => {- const editor = chatEditor();- if (editor) {- editor.focus(undefined, { defaultSelection: 'rootStart' });- return true;- }- return false;- },- hotkeyToken: TOKENS.block.focus,- hide: true,- });- }+ registerScopeSignalHotkey(scopeId, {+ hotkey: 'enter',+ description: 'Focus Chat Input',+ keyDownHandler: () => {+ const editor = chatEditor();+ if (editor) {+ editor.focus(undefined, { defaultSelection: 'rootStart' });+ return true;+ }+ return false;+ },+ hotkeyToken: TOKENS.block.focus,+ hide: true,});+ // In preview mode, switching between Soup tabs was causing this createEffect to overflow the stack. We should figure out that root cause, this flag fixes it for now.+ let hasRun = false;createEffect(() => {+ if (hasRun) return;if (!blockElement()) return;if (!navigatedFromJK()) return;blockElement()?.focus();+ hasRun = true;});return (diff --git a/js/app/packages/block-email/component/Email.tsx b/js/app/packages/block-email/component/Email.tsxindex 93188ba011..429e04319e 100644--- a/js/app/packages/block-email/component/Email.tsx+++ b/js/app/packages/block-email/component/Email.tsx@@ -1,6 +1,6 @@import { useGlobalNotificationSource } from '@app/component/GlobalAppState';-import { registerHotkey } from '@core/hotkey/hotkeys';import { TOKENS } from '@core/hotkey/tokens';+import { registerScopeSignalHotkey } from '@core/hotkey/utils';import {blockElementSignal,blockHotkeyScopeSignal,@@ -59,7 +59,6 @@ type EmailProps = {export function Email(props: EmailProps) {const scopeId = blockHotkeyScopeSignal.get;- console.log('scopeId()', scopeId());const setIsScrollingToMessage = isScrollingToMessage.set;const { navigateThread } = useThreadNavigation();const blockElement = blockElementSignal.get;@@ -515,10 +514,14 @@ export function Email(props: EmailProps) {});});+ // In preview mode, switching between Soup tabs was causing this createEffect to overflow the stack. We should figure out that root cause, this flag fixes it for now.+ let hasRun = false;createEffect(() => {+ if (hasRun) return;// Focus the email block on mountif (!blockElement()) return;blockElement()?.focus();+ hasRun = true;});const notificationSource = useGlobalNotificationSource();@@ -550,23 +553,18 @@ export function Email(props: EmailProps) {);let markdownDomRef!: HTMLDivElement;- createEffect(() => {- if (scopeId()) {- registerHotkey({- hotkey: 'enter',- scopeId: scopeId(),- description: 'Focus Email Input',- keyDownHandler: () => {- if (markdownDomRef) {- markdownDomRef.focus();- return true;- }- return false;- },- hotkeyToken: TOKENS.block.focus,- hide: true,- });- }+ registerScopeSignalHotkey(scopeId, {+ hotkey: 'enter',+ description: 'Focus Email Input',+ keyDownHandler: () => {+ if (markdownDomRef) {+ markdownDomRef.focus();+ return true;+ }+ return false;+ },+ hotkeyToken: TOKENS.block.focus,+ hide: true,});return (diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsxindex 4e8104a0f3..5a8e972356 100644--- a/js/app/packages/block-md/component/MarkdownEditor.tsx+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx@@ -120,6 +120,7 @@ import {} from '@core/signal/load';import { trackMention } from '@core/signal/mention';import { useCanComment, useCanEdit } from '@core/signal/permissions';+import { useBlockDocumentName } from '@core/util/currentBlockDocumentName';import { isSourceDSS, isSourceSyncService } from '@core/util/source';import { bufToString } from '@core/util/string';import WarningIcon from '@icon/regular/warning.svg';@@ -154,6 +155,7 @@ import {onCleanup,onMount,Show,+ untrack,} from 'solid-js';import {completionSignal,@@ -183,6 +185,8 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {const blockData = blockDataSignal.get;const blockId = useBlockId();+ const mdDocumentName = useBlockDocumentName('');+const saveMarkdownDocument = useSaveMarkdownDocument();const setMdStore = mdStore.set;const md = mdStore.get;@@ -838,9 +842,13 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {setEditorReady(true);});- // Auto-focus on mount if enabled and editor is ready and empty.+ // Auto-focus on mount if enabled and editor is ready and document name is not empty.createEffect(() => {- if (props.autoFocusOnMount && editorReady() && editorHasNoContent()) {+ if (+ props.autoFocusOnMount &&+ editorReady() &&+ untrack(mdDocumentName) !== ''+ ) {editor.focus(undefined, { defaultSelection: 'rootStart' });}});diff --git a/js/app/packages/block-md/component/Notebook.tsx b/js/app/packages/block-md/component/Notebook.tsxindex 069a13a67a..a02eaf2a27 100644--- a/js/app/packages/block-md/component/Notebook.tsx+++ b/js/app/packages/block-md/component/Notebook.tsx@@ -155,7 +155,8 @@ export function Notebook() {});createEffect(() => {- if (scopeId()) {+ if (!scopeId()) return;+ untrack(() =>registerHotkey({hotkey: 'enter',scopeId: scopeId(),@@ -176,14 +177,17 @@ export function Notebook() {return false;},hide: true,- });- }+ })+ );});+ // In preview mode, switching between Soup tabs was causing this createEffect to overflow the stack. We should figure out that root cause, this flag fixes it for now.+ let hasRun = false;createEffect(() => {+ if (hasRun) return;if (!blockElement()) return;- if (!navigatedFromJK()) return;blockElement()?.focus();+ hasRun = true;});const containerClasses = createMemo(() => {diff --git a/js/app/packages/core/hotkey/hotkeys.ts b/js/app/packages/core/hotkey/hotkeys.tsindex 12aed5fa62..ce69c4deac 100644--- a/js/app/packages/core/hotkey/hotkeys.ts+++ b/js/app/packages/core/hotkey/hotkeys.ts@@ -4,7 +4,7 @@ import { isTouchDevice } from '@core/mobile/isTouchDevice';import { isMobileWidth } from '@core/mobile/mobileWidth';import { isEditableInput } from '@core/util/isEditableInput';import { logger } from '@observability';-import { createMemo, onCleanup, onMount } from 'solid-js';+import { createMemo, onCleanup, onMount, untrack } from 'solid-js';import {EVENT_MODIFIER_KEYS,EVENT_MODIFIER_NAME_MAP,@@ -144,7 +144,7 @@ export function registerHotkey(// Check for duplicate hotkeyTokenconst existingCommand = hotkeyToken- ? hotkeyTokenMap().get(hotkeyToken)+ ? untrack(() => hotkeyTokenMap().get(hotkeyToken)): undefined;if (existingCommand) {const existingHotkeys = new Set(existingCommand.hotkeys);diff --git a/js/app/packages/core/hotkey/utils.ts b/js/app/packages/core/hotkey/utils.tsindex 0fbee1c064..dee0e5a110 100644--- a/js/app/packages/core/hotkey/utils.ts+++ b/js/app/packages/core/hotkey/utils.ts@@ -1,16 +1,22 @@import { IS_MAC } from '@core/constant/isMac';+import { createEffect } from 'solid-js';import {macOptionReverse,shiftPunctuationMap,shiftPunctuationReverseMap,} from './constants';+import { registerHotkey } from './hotkeys';import {activeScope,hotkeyScopeTree,hotkeysAwaitingKeyUp,setActiveScope,} from './state';-import type { ScopeNode, ValidHotkey } from './types';+import type {+ HotkeyRegistrationOptions,+ ScopeNode,+ ValidHotkey,+} from './types';let scopeCounter = 0;export function getScopeId(prefix: string = 'scope'): string {@@ -221,3 +227,52 @@ export function findClosestParentScopeId(element: Element) {export function findClosestParentScopeElement(element: Element) {return element.parentElement?.closest('[data-hotkey-scope]');}++/**+ * Registers a hotkey in situations where the scopeId is a signal that may not have been set yet, or may change.+ * @param scopeSignal - An accessor that returns the scopeId where the hotkey is active.+ * @param args+ */+export function registerScopeSignalHotkey(+ scopeSignal: () => string,+ args: Omit<HotkeyRegistrationOptions, 'scopeId'>+) {+ let disposer: (() => void) | undefined;++ createEffect(() => {+ const scopeId = scopeSignal();++ if (disposer) {+ disposer();+ disposer = undefined;+ }++ if (!scopeId) return;++ const result = registerHotkey({+ hotkeyToken: args.hotkeyToken,+ hotkey: args.hotkey,+ condition: args.condition,+ scopeId,+ description: args.description,+ keyDownHandler: args.keyDownHandler,+ keyUpHandler: args.keyUpHandler,+ activateCommandScope: args.activateCommandScope,+ runWithInputFocused: args.runWithInputFocused,+ displayPriority: args.displayPriority,+ hide: args.hide,+ icon: args.icon,+ tags: args.tags,+ });++ disposer = result.dispose;++ // Return cleanup function for the effect+ return () => {+ if (disposer) {+ disposer();+ disposer = undefined;+ }+ };+ });+}