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
fix(search): selected-first sort + empty-selection + focus restore for In/From filters (#2991)
gbirmancfbcb2af693f
diff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/searchable-multi-select.tsx b/js/app/packages/app/component/next-soup/soup-view/filters-bar/searchable-multi-select.tsxindex 286b5fd2f0..b8c13f4021 100644--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/searchable-multi-select.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/searchable-multi-select.tsx@@ -11,6 +11,7 @@ import {import { Virtualizer, type VirtualizerHandle } from 'virtua/solid';import CheckIcon from '@icon/regular/check.svg';import SearchIcon from '@icon/regular/magnifying-glass.svg';+import { useSelectedFirst } from '@core/util/useSelectedFirst';import type { SearchableOption } from './search-filter-controls';const ITEM_HEIGHT = 36;@@ -119,6 +120,8 @@ const useActiveOptions = (return options().filter((opt) => set.has(opt.id));});+const getOptionId = (opt: SearchableOption) => opt.id;+export const SearchableMultiSelect = (props: SearchableMultiSelectProps) => {const [internalOpen, setInternalOpen] = createSignal(false);const [searchQuery, setSearchQuery] = createSignal('');@@ -131,6 +134,13 @@ export const SearchableMultiSelect = (props: SearchableMultiSelectProps) => {const activeOptions = useActiveOptions(props.options, props.activeIds);const hasMatches = useHasMatches(props.options, searchQuery);+ const sortedOptions = useSelectedFirst({+ items: props.options,+ selectedIds: props.activeIds,+ searchQuery,+ getId: getOptionId,+ sortDeps: [isOpen],+ });const handleChange = (selected: SearchableOption[]) => {props.onChange(selected.map((o) => o.id));@@ -147,7 +157,7 @@ export const SearchableMultiSelect = (props: SearchableMultiSelectProps) => {selectionBehavior="toggle"closeOnSelection={false}open={isOpen()}- options={props.options()}+ options={sortedOptions()}value={activeOptions()}onChange={handleChange}onInputChange={setSearchQuery}@@ -191,7 +201,7 @@ export const SearchableMultiSelect = (props: SearchableMultiSelectProps) => {}><VirtualizedListbox- options={props.options()}+ options={sortedOptions()}class={props.listboxClass}/></Show>@@ -225,6 +235,15 @@ export const SearchableMultiSelectInline = (const activeOptions = useActiveOptions(props.options, props.activeIds);const hasMatches = useHasMatches(props.options, searchQuery);+ // Inline variant is freshly mounted each time the parent submenu opens,+ // so we don't need an explicit "menu opened" trigger — the memo's first+ // run captures the current selection ordering.+ const sortedOptions = useSelectedFirst({+ items: props.options,+ selectedIds: props.activeIds,+ searchQuery,+ getId: getOptionId,+ });const handleChange = (selected: SearchableOption[]) => {props.onChange(selected.map((o) => o.id));@@ -268,7 +287,7 @@ export const SearchableMultiSelectInline = (selectionBehavior="toggle"closeOnSelection={false}open- options={props.options()}+ options={sortedOptions()}value={activeOptions()}onChange={handleChange}onInputChange={setSearchQuery}@@ -297,7 +316,7 @@ export const SearchableMultiSelectInline = (}><VirtualizedListbox- options={props.options()}+ options={sortedOptions()}class={props.listboxClass}/></Show>diff --git a/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx b/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsxindex 453400409e..d3f55951a0 100644--- a/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx+++ b/js/app/packages/app/component/next-soup/soup-view/filters-bar/use-filter-refinements.tsx@@ -265,7 +265,16 @@ export function useFilterRefinements() {seenKeys.add(args.key);filters.push(getOrCreateChip(args.key, () => {- const [isPopupOpen, setPopupOpen] = createSignal(false);+ const [isPopupOpen, _setPopupOpen] = createSignal(false);+ // restores focus to the split panel on close+ const setPopupOpen = (v: boolean) => {+ if (!v) {+ queueMicrotask(() =>+ panel.panelRef()?.focus({ preventScroll: true })+ );+ }+ _setPopupOpen(v);+ };return {categoryLabel: args.categoryLabel,optionId: () => `${args.optionIdPrefix}:${args.getIds().join(',')}`,diff --git a/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx b/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsxindex fa479d81e4..af394835e0 100644--- a/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx+++ b/js/app/packages/core/component/Properties/component/modal/shared/PropertyEntitySelector.tsx@@ -2,6 +2,7 @@ import { Entity, type EntityData } from '@entity';import { UserIcon } from '@core/component/UserIcon';import { useAugmentUserWithDmActivity } from '@core/user';import { createFreshSearch } from '@core/util/freshSort';+import { useSelectedFirst } from '@core/util/useSelectedFirst';import SearchIcon from '@icon/regular/magnifying-glass.svg';import { createEmailsInfiniteQuery } from '@macro-entity';import type { EmailEntity } from '@entity';@@ -13,7 +14,6 @@ import {createEffect,createMemo,createSignal,- on,onCleanup,onMount,Show,@@ -274,50 +274,16 @@ export function PropertyEntitySelector(props: EntityInputProps) {return localResults;});- // Sort entities with selected items first when not searching- const sortedEntities = createMemo(- on([searchTerm, filteredEntities], () => {- const term = searchTerm();- const filteredResults = filteredEntities();-- // When there's a search term, return results as-is- if (term) {- return filteredResults;- }-- // When browsing (no search), show selected entities first, then others- // (self is already sorted to top within filteredEntities)- const selectedIds = props.selectedOptions();- const entityIdsInResults = new Set(filteredResults.map((e) => e.id));-- // Partition filtered results into selected and unselected- const selected: CombinedEntity[] = [];- const unselected: CombinedEntity[] = [];-- for (const entity of filteredResults) {- if (selectedIds.has(entity.id)) {- selected.push(entity);- } else {- unselected.push(entity);- }- }-- // Add missing selected entities that aren't in the visible results- const allAvailableEntities = entities();- for (const selectedId of selectedIds) {- if (!entityIdsInResults.has(selectedId)) {- const actualEntity = allAvailableEntities.find(- (e) => e.id === selectedId- );- if (actualEntity) {- selected.push(actualEntity);- }- }- }-- return [...selected, ...unselected];- })- );+ // Sort entities with selected items first when not searching. Self is+ // already pinned to the top within `filteredEntities`; this layers+ // selected-first on top of that ordering.+ const sortedEntities = useSelectedFirst({+ items: filteredEntities,+ allItems: entities,+ selectedIds: props.selectedOptions,+ searchQuery: searchTerm,+ getId: (e: CombinedEntity) => e.id,+ });const toggleEntity = (entity: CombinedEntity) => {const newSelected = new Set(props.selectedOptions());diff --git a/js/app/packages/core/util/useSelectedFirst.ts b/js/app/packages/core/util/useSelectedFirst.tsnew file mode 100644index 0000000000..83a81a9d3d--- /dev/null+++ b/js/app/packages/core/util/useSelectedFirst.ts@@ -0,0 +1,67 @@+import { type Accessor, createMemo, on } from 'solid-js';++type UseSelectedFirstOptions<T> = {+ /** Items currently visible (after search/filter). */+ items: Accessor<T[]>;+ /** Currently-selected ids. Read non-reactively — see `resortDeps`. */+ selectedIds: Accessor<Iterable<string>>;+ /** Active search/filter query. When non-empty, items are returned as-is. */+ searchQuery: Accessor<string>;+ /** Extracts a stable id from an item. */+ getId: (item: T) => string;+ /**+ * Optional pool of all available items. When `items` is sliced (e.g. top N+ * by recency), selected ids missing from `items` are looked up here so the+ * user can still see and toggle them off.+ */+ allItems?: Accessor<T[]>;+ /**+ * Reactive deps that, when changed, recompute the partition. The memo+ * deliberately does NOT track `selectedIds`, so toggling selection inside+ * an open menu doesn't shuffle items underneath the user. Pass an "is+ * open" accessor to re-sort each time the menu reopens.+ */+ sortDeps?: Accessor<unknown>[];+};++/**+ * Returns `items` with currently-selected entries pinned to the top. Used by+ * the task assignee menu and the In/From search filter menus: stable order+ * during a single open session, re-sorted on (re)open.+ */+export function useSelectedFirst<T>(+ opts: UseSelectedFirstOptions<T>+): Accessor<T[]> {+ return createMemo(+ on([opts.items, opts.searchQuery, ...(opts.sortDeps ?? [])], () => {+ const items = opts.items();+ if (opts.searchQuery()) return items;++ const idSet = new Set<string>();+ for (const id of opts.selectedIds()) idSet.add(id);+ if (idSet.size === 0) return items;++ const selected: T[] = [];+ const unselected: T[] = [];+ const visibleIds = new Set<string>();++ for (const item of items) {+ const id = opts.getId(item);+ visibleIds.add(id);+ if (idSet.has(id)) selected.push(item);+ else unselected.push(item);+ }++ const all = opts.allItems?.();+ if (all) {+ for (const id of idSet) {+ if (visibleIds.has(id)) continue;+ const found = all.find((it) => opts.getId(it) === id);+ if (found) selected.push(found);+ }+ }++ return [...selected, ...unselected];+ })+ );+}