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.

fix: selected entity signal performance enhancements (#1329)

Peter Chinmana9ad78c792ed
diff --git a/js/app/packages/app/component/Soup.tsx b/js/app/packages/app/component/Soup.tsx
index 4dc5779774..27584d0076 100644
--- a/js/app/packages/app/component/Soup.tsx
+++ b/js/app/packages/app/component/Soup.tsx
@@ -20,6 +20,7 @@ import {
type ViewId,
type ViewLabel,
} from '@core/types/view';
+import { throttledDependent } from '@core/util/debounce';
import { handleFileFolderDrop } from '@core/util/upload';
import { Tabs } from '@kobalte/core/tabs';
import {
@@ -638,6 +639,7 @@ export function Soup() {
const previewState = () => splitPanelContext.previewState;
const [preview, setPreview] = previewState();
const selectedEntity = () => view().selectedEntity;
+ const throttledSelectedEntity = throttledDependent(selectedEntity, 100);
createEffect(() => {
handle.setDisplayName('');
@@ -777,7 +779,7 @@ export function Soup() {
</SplitPanelContext.Provider>
<Show when={preview()}>
<PreviewPanel
- selectedEntity={selectedEntity()}
+ selectedEntity={throttledSelectedEntity()}
orchestrator={orchestrator}
splitPanelContext={splitPanelContext}
/>
diff --git a/js/app/packages/app/component/SoupContext.tsx b/js/app/packages/app/component/SoupContext.tsx
index b6f5006bcc..9e72dacbab 100644
--- a/js/app/packages/app/component/SoupContext.tsx
+++ b/js/app/packages/app/component/SoupContext.tsx
@@ -5,7 +5,11 @@ import { HotkeyTags } from '@core/hotkey/constants';
import { activeScope, hotkeyScopeTree } from '@core/hotkey/state';
import { TOKENS } from '@core/hotkey/tokens';
import { HOTKEY_PRIORITY_LOW } from '@core/hotkey/types';
-import { getHotkeyCommand, runCommand } from '@core/hotkey/utils';
+import {
+ getHotkeyCommand,
+ getScopeElement,
+ runCommand,
+} from '@core/hotkey/utils';
import { isModality } from '@core/mobile/inputModality';
import type { DefaultView, ViewId } from '@core/types/view';
import { getActualTarget } from '@core/util/getActualTarget';
@@ -1462,10 +1466,11 @@ export function createNavigationEntityListShortcut({
const getEnterCommand = () => {
const currentActiveScope = activeScope();
if (!currentActiveScope) return undefined;
- let activeScopeNode = hotkeyScopeTree.get(currentActiveScope);
+ const activeScopeNode = hotkeyScopeTree.get(currentActiveScope);
if (!activeScopeNode) return undefined;
if (activeScopeNode?.type !== 'dom') return;
- const dom = activeScopeNode.element;
+ const dom = getScopeElement(currentActiveScope);
+ if (!dom) return undefined;
const closestBlockScope = dom.closest(`[id="block-${entity.id}"]`);
if (
!closestBlockScope ||
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 0f95604a14..e4ed56779b 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -338,32 +338,15 @@ export function UnifiedListView(props: UnifiedListViewProps) {
);
// Auto-select first entity when no selection exists
- createEffect(() => {
- if (isTouchDevice()) return;
- if (view()?.hasUserInteractedEntity) return;
- if (selectedEntity()) return;
-
- const first = entities_()?.at(0);
- if (first) setSelectedEntity(first);
- });
-
- // Always keep an entity selected when the list has items (e.g. after deletes).
createEffect(
- on([entities_, selectedEntity], () => {
+ on(entities_, () => {
if (isTouchDevice()) return;
-
const list = entities_() ?? [];
const first = list.at(0);
if (!first) return;
const current = selectedEntity();
- if (!current) {
- setSelectedEntity(first);
- return;
- }
-
- const existsInList = entityById().has(current.id);
- if (!existsInList) {
+ if (!current || !entityById().has(current.id)) {
setSelectedEntity(first);
}
})
diff --git a/js/app/packages/block-project/component/Block.tsx b/js/app/packages/block-project/component/Block.tsx
index e0c84b7989..f4ab2b52fd 100644
--- a/js/app/packages/block-project/component/Block.tsx
+++ b/js/app/packages/block-project/component/Block.tsx
@@ -18,6 +18,7 @@ import {
type UploadInput,
uploadFiles,
} from '@core/util/upload';
+import { throttledDependent } from '@core/util/debounce';
import {
queryKeys,
useQueryClient as useEntityQueryClient,
@@ -102,6 +103,7 @@ const Block: Component = () => {
const [preview, setPreview] = splitPanelContext.previewState;
const view = createMemo(() => viewsData[selectedView()]);
const selectedEntity = () => view().selectedEntity;
+ const throttledSelectedEntity = throttledDependent(selectedEntity, 150);
if (!isRenderedFromPreview) {
registerHotkey({
@@ -169,7 +171,7 @@ const Block: Component = () => {
</SplitPanelContext.Provider>
<Show when={preview()}>
<PreviewPanel
- selectedEntity={selectedEntity()}
+ selectedEntity={throttledSelectedEntity()}
orchestrator={orchestrator}
splitPanelContext={splitPanelContext}
/>
diff --git a/js/app/packages/core/hotkey/hotkeys.ts b/js/app/packages/core/hotkey/hotkeys.ts
index 066534bcb2..645f9fde5d 100644
--- a/js/app/packages/core/hotkey/hotkeys.ts
+++ b/js/app/packages/core/hotkey/hotkeys.ts
@@ -375,7 +375,6 @@ export function useHotkeyDOMScope(
}
if (DOMScope?.type === 'dom') {
DOMScope.parentScopeId = parentScopeId;
- DOMScope.element = el;
}
el.addEventListener('focusin', handleFocusIn);
diff --git a/js/app/packages/core/hotkey/state.ts b/js/app/packages/core/hotkey/state.ts
index 3928ea1c07..8ee3b9356f 100644
--- a/js/app/packages/core/hotkey/state.ts
+++ b/js/app/packages/core/hotkey/state.ts
@@ -10,7 +10,6 @@ const initialTree = new Map<string, ScopeNode>([
{
scopeId: 'global',
type: 'dom',
- element: document.body,
childScopeIds: [],
hotkeyCommands: new Map(),
unkeyedCommands: [],
diff --git a/js/app/packages/core/hotkey/types.ts b/js/app/packages/core/hotkey/types.ts
index d998513a8b..afcd41d609 100644
--- a/js/app/packages/core/hotkey/types.ts
+++ b/js/app/packages/core/hotkey/types.ts
@@ -174,7 +174,6 @@ export type ScopeNodeBase = {
export type DOMScopeNode = {
type: 'dom';
- element: Element;
};
export type CommandScopeNode = {
diff --git a/js/app/packages/core/hotkey/utils.ts b/js/app/packages/core/hotkey/utils.ts
index 83a66018ac..60878e6f04 100644
--- a/js/app/packages/core/hotkey/utils.ts
+++ b/js/app/packages/core/hotkey/utils.ts
@@ -147,7 +147,6 @@ type RegisterScopeArgsBase = {
type RegisterDOMScopeArgs = RegisterScopeArgsBase & {
type: 'dom';
- element?: Element;
runWithInputFocused?: never;
condition?: never;
};
@@ -178,7 +177,6 @@ export function registerScope(args: RegisterScopeArgs) {
? {
...baseScope,
type: 'dom',
- element: args.element ?? document.body,
}
: {
...baseScope,
@@ -228,11 +226,12 @@ export function removeScope(scopeId: string) {
let parentScope = hotkeyScopeTree.get(currentScope.parentScopeId ?? '');
let foundDOMScopeParent = false;
while (parentScope) {
- if (
- parentScope.type === 'dom' &&
- parentScope.element instanceof HTMLElement
- ) {
- parentScope.element.focus();
+ const parentElement =
+ parentScope.type === 'dom'
+ ? getScopeElement(parentScope.scopeId)
+ : null;
+ if (parentElement instanceof HTMLElement) {
+ parentElement.focus();
setActiveScope(parentScope.scopeId);
foundDOMScopeParent = true;
break;
@@ -279,11 +278,12 @@ export function activateClosestDOMScope() {
let activeScopeId = 'global';
// find the closest active DOM scope
while (currentScope) {
- if (
- currentScope.type === 'dom' &&
- currentScope.element instanceof HTMLElement
- ) {
- currentScope.element.focus();
+ const scopeElement =
+ currentScope.type === 'dom'
+ ? getScopeElement(currentScope.scopeId)
+ : null;
+ if (scopeElement instanceof HTMLElement) {
+ scopeElement.focus();
activeScopeId = currentScope.scopeId;
break;
}
@@ -566,6 +566,10 @@ export function getKeyString(pressedKeys: Set<string>): ValidHotkey {
.join('+') as ValidHotkey;
}
+export function getScopeElement(scopeId: string): Element | null {
+ return document.querySelector(`[data-hotkey-scope="${scopeId}"]`);
+}
+
// Returns the id of the closest parent scope, or 'global' if no parent scope is found.
export function findClosestParentScopeId(element: Element) {
const parentElement = element.parentElement;
diff --git a/js/app/packages/core/util/debounce.ts b/js/app/packages/core/util/debounce.ts
index 55c07303bd..c45ca728ce 100644
--- a/js/app/packages/core/util/debounce.ts
+++ b/js/app/packages/core/util/debounce.ts
@@ -1,4 +1,8 @@
-import { debounce as solidDebounce } from '@solid-primitives/scheduled';
+import {
+ debounce as solidDebounce,
+ leadingAndTrailing,
+ throttle as solidThrottle,
+} from '@solid-primitives/scheduled';
import { createEffect, createSignal, onCleanup } from 'solid-js';
export function debounce<T extends (...args: any[]) => any>(
@@ -19,6 +23,23 @@ export function debouncedDependent<T>(source: () => T, delay = 300): () => T {
return debounced;
}
+/**
+ * Create a throttled view of a signal. Updates immediately on the first change,
+ * then skips intermediate values during the cooldown period, catching the last
+ * value at the trailing edge.
+ */
+export function throttledDependent<T>(source: () => T, delay = 150): () => T {
+ const [throttled, setThrottled] = createSignal(source());
+ const schedule = leadingAndTrailing(
+ solidThrottle,
+ (v: T) => setThrottled(() => v),
+ delay
+ );
+ createEffect(() => schedule(source()));
+ onCleanup(() => schedule.clear());
+ return throttled;
+}
+
/**
* Create a lagged-on-true signal. A debounced view that is only debounced on
* the leading edge. In the examples below (_) is the false state and (#) is the