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.

feat(search): enter key selects first item, down arrow key goes to next item (#680)

gbirmanc0f77f81d94e
diff --git a/js/app/packages/app/component/SoupContext.tsx b/js/app/packages/app/component/SoupContext.tsx
index 9616f637c0..798c8373b3 100644
--- a/js/app/packages/app/component/SoupContext.tsx
+++ b/js/app/packages/app/component/SoupContext.tsx
@@ -42,6 +42,7 @@ import {
} from 'solid-js';
import {
createStore,
+ produce,
reconcile,
type SetStoreFunction,
type Store,
@@ -80,6 +81,8 @@ import {
type ViewDataMap,
} from './ViewConfig';
+type NavigateListFn = (input: NavigationInput) => Promise<NavigationResult>;
+
export type UnifiedListContext = {
viewsDataStore: Store<ViewDataMap>;
setViewDataStore: SetStoreFunction<Partial<ViewDataMap>>;
@@ -92,6 +95,9 @@ export type UnifiedListContext = {
showHelpDrawer: Accessor<Set<DefaultView>>;
setShowHelpDrawer: Setter<Set<DefaultView>>;
actionRegistry: EntityActionRegistry;
+ navigateThroughList: NavigateListFn;
+ // this is a private method that should be registered once by createNavigationEntityListShortcut
+ _setNavigateThroughList: (fn: NavigateListFn) => void;
};
const DEFAULT_VIEW_ID: DefaultView = 'signal';
@@ -100,7 +106,7 @@ const DEFAULT_VIEW_IDS_SET = new Set(VIEWCONFIG_DEFAULTS_IDS);
export function createSoupContext(): UnifiedListContext {
const [selectedView, setSelectedView] = createSignal<ViewId>(DEFAULT_VIEW_ID);
- const [viewsDataStore, setViewDataStore_] = useAllViews({
+ const [viewsDataStore, setViewDataStore] = useAllViews({
selectedViewSignal: [selectedView, setSelectedView],
});
const virtualizerHandleSignal = createSignal<VirtualizerHandle>();
@@ -111,18 +117,7 @@ export function createSoupContext(): UnifiedListContext {
const [showHelpDrawer, setShowHelpDrawer] = createSignal<Set<DefaultView>>(
!tutorialCompleted() ? new Set(DEFAULT_VIEWS) : new Set()
);
- const setViewDataStore: SetStoreFunction<ViewDataMap> = (...args: any[]) => {
- // need to create new reference, causes bug where first entity persits highlighting
- if (
- args.length === 3 &&
- args[1] === 'selectedEntity' &&
- typeof args[2] !== 'function'
- ) {
- args[2] = { ...args[2] };
- }
- // @ts-ignore narrowing set store function is annoying due to function overloading
- setViewDataStore_(...args);
- };
+ let navigateThroughListFn: NavigateListFn | undefined;
return {
viewsDataStore,
@@ -136,6 +131,18 @@ export function createSoupContext(): UnifiedListContext {
showHelpDrawer,
setShowHelpDrawer,
actionRegistry: createEntityActionRegistry(),
+ navigateThroughList: (input) => {
+ if (!navigateThroughListFn) {
+ throw new Error('navigateThroughList not initialized');
+ }
+ return navigateThroughListFn(input);
+ },
+ _setNavigateThroughList: (fn) => {
+ if (navigateThroughListFn) {
+ console.warn('navigateThroughList already initialized');
+ }
+ navigateThroughListFn = fn;
+ },
};
}
@@ -191,13 +198,13 @@ function createViewData(
};
}
-type NavigationInput = {
+export type NavigationInput = {
axis: 'start' | 'end'; // movement direction
mode: 'step' | 'jump'; // how far: one step or to the end
highlight?: boolean;
};
-type NavigationResult = {
+export type NavigationResult = {
success: boolean;
entity: EntityData | undefined;
};
@@ -232,6 +239,16 @@ export function createNavigationEntityListShortcut({
const [attachEntityHotkeys, _entityHotkeyScope] = useHotkeyDOMScope('entity');
const selectedEntity = () => viewData().selectedEntity;
+ const setSelectedEntity = (entity: EntityData | undefined) => {
+ setViewDataStore(
+ selectedView(),
+ produce((state) => {
+ if (!state) return;
+ state.selectedEntity = entity;
+ })
+ );
+ };
+
const notificationSource = useGlobalNotificationSource();
const userId = useUserId();
@@ -270,7 +287,7 @@ export function createNavigationEntityListShortcut({
setViewDataStore(selectedView(), 'selectedEntities', []);
}
if (entity) {
- setViewDataStore(selectedView(), 'selectedEntity', entity);
+ setSelectedEntity(entity);
setViewDataStore(selectedView(), 'highlightedId', entity.id);
const nextIndex = entities()?.findIndex(({ id }) => id === entity.id);
if (nextIndex !== undefined && nextIndex > -1) {
@@ -887,7 +904,7 @@ export function createNavigationEntityListShortcut({
}
batch(() => {
setViewDataStore(selectedView(), 'highlightedId', selectedEntity.id);
- setViewDataStore(selectedView(), 'selectedEntity', selectedEntity);
+ setSelectedEntity(selectedEntity);
});
}
@@ -928,6 +945,8 @@ export function createNavigationEntityListShortcut({
};
};
+ unifiedListContext._setNavigateThroughList(navigateThroughList);
+
const scrollToEntityFromId = async () => {
const index = getHighlightedEntity()?.index;
if (!index) return;
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 0c7baf8b87..0171d742df 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -105,7 +105,12 @@ import {
Show,
type Signal,
} from 'solid-js';
-import { createStore, type SetStoreFunction, unwrap } from 'solid-js/store';
+import {
+ createStore,
+ produce,
+ type SetStoreFunction,
+ unwrap,
+} from 'solid-js/store';
import { EntityWithEverything } from '../../macro-entity/src/components/EntityWithEverything';
import {
resetCommandCategoryIndex,
@@ -221,31 +226,39 @@ export function UnifiedListView(props: UnifiedListViewProps) {
selectedView,
virtualizerHandleSignal: [, setVirtualizerHandle],
entityListRefSignal: [, setEntityListRef],
- entitiesSignal: [_entities, setEntities],
- emailViewSignal: [_emailView],
+ entitiesSignal: [entities_, setEntities],
} = unifiedListContext;
const view = createMemo(() => viewsData[selectedView()]);
const selectedEntity = createMemo(() => view()?.selectedEntity);
+ const setSelectedEntity = (entity: EntityData | undefined) => {
+ setViewDataStore(
+ selectedView(),
+ produce((state) => {
+ if (!state) return;
+ state.selectedEntity = entity;
+ })
+ );
+ };
+
+ const rawSearchText = createMemo<string>(() => view()?.searchText ?? '');
+ const searchText = createMemo(() => rawSearchText()?.trim() ?? '');
+
createEffect(
on(
- () =>
- [
- localEntityListRef(),
- // access index to properly track
- _entities()?.[0],
- ] as const,
- ([localEntityListRef]) => {
+ [localEntityListRef, () => entities_()?.at(0), searchText],
+ ([localEntityListRef, firstEntity]) => {
if (!localEntityListRef) return;
setEntityListRef(localEntityListRef);
if (view()?.hasUserInteractedEntity) {
- if (selectedEntity()) {
+ const selectedEntityId = selectedEntity()?.id;
+ if (selectedEntityId) {
if (localEntityListRef && localEntityListRef.isConnected) {
// focusing non-first entity causes issue where 100ms later, that focused entity loses focus and document.body is focused
// forcing refocus on that entity works for now
// read TODO inside function for more info
- tryFocusEntity(selectedEntity()!.id, {
+ tryFocusEntity(selectedEntityId, {
forceRefocusOnce: true,
});
}
@@ -253,12 +266,10 @@ export function UnifiedListView(props: UnifiedListViewProps) {
return;
}
- // select first item from entityList until interaction
- if (!_entities() || !_entities()?.length) return;
- const firstEntity = _entities()![0];
+ if (!firstEntity) return;
setViewDataStore(selectedView(), 'highlightedId', firstEntity.id);
- setViewDataStore(selectedView(), 'selectedEntity', firstEntity);
+ setSelectedEntity(firstEntity);
tryFocusEntity(firstEntity.id);
@@ -507,9 +518,6 @@ export function UnifiedListView(props: UnifiedListViewProps) {
return getSuggestedProperties(types);
});
- const rawSearchText = createMemo<string>(() => view()?.searchText ?? '');
- const searchText = createMemo(() => rawSearchText()?.trim() ?? '');
-
const debouncedSearchForLocal = debouncedDependent(
searchText,
LOCAL_FUZZY_SEARCH_DEBOUNCE_MS
@@ -1095,7 +1103,8 @@ export function UnifiedListView(props: UnifiedListViewProps) {
options
) => {
if (preview() && !options?.ignorePreview) {
- setViewDataStore(selectedView(), 'selectedEntity', entity);
+ setSelectedEntity(entity);
+
return;
}
@@ -1533,11 +1542,7 @@ export function UnifiedListView(props: UnifiedListViewProps) {
setHighlightedId(innerProps.entity.id);
if (isPanelActive() && !preview()) {
- setViewDataStore(
- selectedView(),
- 'selectedEntity',
- innerProps.entity
- );
+ setSelectedEntity(innerProps.entity);
}
setContextAndModalState((prev) => {
@@ -1582,11 +1587,7 @@ export function UnifiedListView(props: UnifiedListViewProps) {
setHighlightedId(innerProps.entity.id);
if (isPanelActive() && !preview()) {
- setViewDataStore(
- selectedView(),
- 'selectedEntity',
- innerProps.entity
- );
+ setSelectedEntity(innerProps.entity);
}
}}
onMouseLeave={() => {}}
@@ -1594,11 +1595,7 @@ export function UnifiedListView(props: UnifiedListViewProps) {
setHighlightedId(innerProps.entity.id);
if (isPanelActive() && !preview()) {
- setViewDataStore(
- selectedView(),
- 'selectedEntity',
- innerProps.entity
- );
+ setSelectedEntity(innerProps.entity);
}
}}
showLeftColumnIndicator={
@@ -1824,9 +1821,9 @@ function SearchBar(props: {
viewsDataStore,
selectedView,
setViewDataStore,
- entitiesSignal: [entities],
virtualizerHandleSignal: [virtualizerHandle],
entityListRefSignal: [entityListRef],
+ navigateThroughList,
} = splitContext.unifiedListContext;
const viewData = createMemo(() => viewsDataStore[selectedView()]);
const viewName = createMemo(() => viewData().view);
@@ -1838,44 +1835,19 @@ function SearchBar(props: {
setViewDataStore(selectedView(), 'searchText', text);
};
- const isElementInViewport = (element: Element): Promise<boolean> => {
- return new Promise((resolve) => {
- const observer = new IntersectionObserver(
- (entries) => {
- resolve(entries[0].isIntersecting);
- observer.disconnect();
- },
- { threshold: 0.1 }
- );
- observer.observe(element);
- });
+ const selectionClick = () => {
+ const id = viewsDataStore[selectedView()].highlightedId;
+ if (!id) return;
+ const el = entityListRef()?.querySelector(`[data-entity-id="${id}"]`);
+ if (!(el instanceof HTMLElement)) return;
+ el.click();
};
- const focusFirstEntity = async () => {
- const highlightedId = viewData()?.highlightedId;
- const id = highlightedId;
-
- if (id) {
- const highlightedEntityEl = entityListRef()?.querySelector(
- `[data-entity-id="${id}"]`
- );
-
- if (
- highlightedEntityEl instanceof HTMLElement &&
- (await isElementInViewport(highlightedEntityEl))
- ) {
- highlightedEntityEl.focus();
- const entity = entities()?.find(({ id: entityId }) => entityId === id);
- if (entity) {
- setViewDataStore(selectedView(), 'selectedEntity', entity);
- return;
- }
- }
- }
-
- // Fallback to first entity
- const firstEntity = entityListRef()?.querySelector('[data-entity]');
- if (firstEntity instanceof HTMLElement) firstEntity.focus();
+ const focusNextEntity = () => {
+ navigateThroughList({
+ axis: 'end',
+ mode: 'step',
+ });
};
const [waitForLoadingEnd, setWaitForLoadingEnd] = createSignal(false);
@@ -1884,10 +1856,9 @@ function SearchBar(props: {
createRenderEffect((prevText: string) => {
const text = searchText().trim();
if (text !== prevText) {
- batch(() => {
- setViewDataStore(selectedView(), 'selectedEntity', undefined);
- setViewDataStore(selectedView(), 'highlightedId', undefined);
- });
+ setViewDataStore(selectedView(), 'selectedEntity', undefined);
+ setViewDataStore(selectedView(), 'highlightedId', undefined);
+ setViewDataStore(selectedView(), 'hasUserInteractedEntity', false);
virtualizerHandle()?.scrollToIndex(0);
setWaitForLoadingEnd(true);
}
@@ -1983,14 +1954,17 @@ function SearchBar(props: {
setSearchText(e.target.value);
}}
onKeyDown={(e) => {
- if (
- e.key === 'Escape' ||
- e.key === 'ArrowDown' ||
- e.key === 'Enter'
- ) {
+ if (e.key === 'Escape') {
+ e.preventDefault();
+ e.currentTarget.blur();
+ } else if (e.key === 'Enter') {
+ e.preventDefault();
+ e.currentTarget.blur();
+ selectionClick();
+ } else if (e.key === 'ArrowDown') {
e.preventDefault();
e.currentTarget.blur();
- focusFirstEntity();
+ focusNextEntity();
}
}}
class="p-1 pr-0 border-0 outline-none! focus:outline-none ring-0! focus:ring-0 flex-1 text-ink text-sm truncate"