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.

chg(properties): sort self to top for assignee properties (#1665)

Seamus32d5cadd7273
diff --git a/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx b/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
index f2bffe4981..6e3f9b538a 100644
--- a/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
+++ b/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
@@ -10,10 +10,11 @@ import {
threadMapper,
quickAccessItemToEntity,
userToEntity,
+ sortEntitiesWithSelfFirst,
} from '@core/component/Properties/component/modal/shared/entityUtils';
import { usePropertyEntityDisplay } from '@core/component/Properties/hooks/usePropertyEntityDisplay';
import { useAugmentUserWithDmActivity } from '@core/user';
-import { useEmail } from '@core/context/user';
+import { useEmail, useUserId } from '@core/context/user';
import { createFreshSearch } from '@core/util/freshSort';
import { createEmailsInfiniteQuery } from '@macro-entity';
import type { EmailEntity } from '@entity';
@@ -87,8 +88,9 @@ export const FilterValueEntity: Component<FilterValueEntityProps> = (props) => {
const augmentUserWithDmActivity = useAugmentUserWithDmActivity();
- // Get current user domain for same-domain boost in search
+ // Get current user info for same-domain boost and self-boost in search
const currentUserEmail = useEmail();
+ const currentUserId = useUserId();
const currentUserDomain = createMemo(() => {
const email = currentUserEmail();
return email ? email.split('@')[1] : undefined;
@@ -181,7 +183,7 @@ export const FilterValueEntity: Component<FilterValueEntityProps> = (props) => {
// Search function for fuzzy matching (same config as PropertyEntitySelector)
const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain),
+ createEntitySearchConfig(currentUserDomain, currentUserId),
getEntitySearchText,
isChannelEntity,
getEntityTimestampedItem
@@ -196,11 +198,19 @@ export const FilterValueEntity: Component<FilterValueEntityProps> = (props) => {
const availableEntities = createMemo(() => {
const query = searchTerm();
const ids = selectedIds();
- const available = entities().filter((e) => !ids.has(e.id));
+ const userId = currentUserId();
+ // Sort self to top BEFORE filtering to ensure self appears even if not in top 50 by default
+ const available = sortEntitiesWithSelfFirst(
+ entities().filter((e) => !ids.has(e.id)),
+ userId
+ );
const MAX_RESULTS = 50;
- if (!query) return available.slice(0, MAX_RESULTS);
+ // When no search query, return sorted list
+ if (!query) {
+ return available.slice(0, MAX_RESULTS);
+ }
// Local search results
const localResults = entitySearch(available, query)
diff --git a/js/app/packages/app/component/property-edit-modal/hooks/useEntitiesForProperty.ts b/js/app/packages/app/component/property-edit-modal/hooks/useEntitiesForProperty.ts
index ec33ab756d..6997bca8bb 100644
--- a/js/app/packages/app/component/property-edit-modal/hooks/useEntitiesForProperty.ts
+++ b/js/app/packages/app/component/property-edit-modal/hooks/useEntitiesForProperty.ts
@@ -9,10 +9,11 @@ import {
threadMapper,
quickAccessItemToEntity,
userToEntity,
+ sortEntitiesWithSelfFirst,
} from '@core/component/Properties/component/modal/shared/entityUtils';
import { useAugmentUserWithDmActivity } from '@core/user';
import { createFreshSearch } from '@core/util/freshSort';
-import { useEmail } from '@core/context/user';
+import { useEmail, useUserId } from '@core/context/user';
import { createEmailsInfiniteQuery } from '@macro-entity';
import type { EmailEntity } from '@entity';
import { useSearchSoupQuery } from '@queries/soup/search';
@@ -44,8 +45,9 @@ export function useEntitiesForProperty(
const augmentUserWithDmActivity = useAugmentUserWithDmActivity();
- // Get current user domain for same-domain boost in search
+ // Get current user info for same-domain boost and self-boost in search
const currentUserEmail = useEmail();
+ const currentUserId = useUserId();
const currentUserDomain = createMemo(() => {
const email = currentUserEmail();
return email ? email.split('@')[1] : undefined;
@@ -139,7 +141,7 @@ export function useEntitiesForProperty(
// search function for fuzzy matching
const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain),
+ createEntitySearchConfig(currentUserDomain, currentUserId),
getEntitySearchText,
isChannelEntity,
getEntityTimestampedItem
@@ -149,14 +151,20 @@ export function useEntitiesForProperty(
const filteredEntities = createMemo(() => {
const query = searchTerm();
const available = entities();
+ const userId = currentUserId();
const MAX_RESULTS = 50;
+ // When no search query, sort self to top BEFORE slicing
+ if (!query) {
+ return sortEntitiesWithSelfFirst(available, userId).slice(0, MAX_RESULTS);
+ }
+
const localResults = entitySearch(available, query)
.slice(0, MAX_RESULTS)
.map((result) => result.item);
- if (needsEmailSearch() && query) {
+ if (needsEmailSearch()) {
const localIds = new Set(localResults.map((e) => e.id));
const serverResults = serverEmails().filter((e) => !localIds.has(e.id));
return [...localResults, ...serverResults].slice(0, MAX_RESULTS);
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.tsx
index 21f37061ee..b8e2fcb24a 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
@@ -6,7 +6,7 @@ import SearchIcon from '@icon/regular/magnifying-glass.svg';
import { createEmailsInfiniteQuery } from '@macro-entity';
import type { EmailEntity } from '@entity';
import { useSearchSoupQuery } from '@queries/soup/search';
-import { useEmail } from '@core/context/user';
+import { useEmail, useUserId } from '@core/context/user';
import { debounce } from '@solid-primitives/scheduled';
import {
createEffect,
@@ -30,6 +30,7 @@ import {
threadMapper,
quickAccessItemToEntity,
userToEntity,
+ sortEntitiesWithSelfFirst,
} from './entityUtils';
import { OptionCheckBox } from './OptionCheckBox';
import { useKeyPressed } from '@core/util/useKeyPressed';
@@ -111,8 +112,9 @@ export function PropertyEntitySelector(props: EntityInputProps) {
const augmentUserWithDmActivity = useAugmentUserWithDmActivity();
- // Get current user domain for same-domain boost in search
+ // Get current user info for same-domain boost and self-boost in search
const currentUserEmail = useEmail();
+ const currentUserId = useUserId();
const currentUserDomain = createMemo(() => {
const email = currentUserEmail();
return email ? email.split('@')[1] : undefined;
@@ -215,7 +217,7 @@ export function PropertyEntitySelector(props: EntityInputProps) {
});
const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain),
+ createEntitySearchConfig(currentUserDomain, currentUserId),
getEntitySearchText,
isChannelEntity,
getEntityTimestampedItem
@@ -224,6 +226,7 @@ export function PropertyEntitySelector(props: EntityInputProps) {
const filteredEntities = createMemo(() => {
const term = searchTerm();
const allEntities = entities();
+ const userId = currentUserId();
const MAX_VISIBLE_ENTITIES_NO_SEARCH = 50;
const MAX_SEARCH_RESULTS = 20;
@@ -237,14 +240,16 @@ export function PropertyEntitySelector(props: EntityInputProps) {
: () => true;
// Get visible entities based on search
+ // Sort self to top BEFORE slicing to ensure self appears even if not in top 50 by default
const localResults = term
? entitySearch(allEntities, term)
.slice(0, MAX_SEARCH_RESULTS)
.map((result) => result.item)
.filter(excludeFilter)
- : allEntities
- .filter(excludeFilter)
- .slice(0, MAX_VISIBLE_ENTITIES_NO_SEARCH);
+ : sortEntitiesWithSelfFirst(
+ allEntities.filter(excludeFilter),
+ userId
+ ).slice(0, MAX_VISIBLE_ENTITIES_NO_SEARCH);
// For THREAD or generic entity: merge local + server results
if (needsEmailSearch() && term) {
@@ -269,7 +274,8 @@ export function PropertyEntitySelector(props: EntityInputProps) {
return filteredResults;
}
- // When browsing (no search), show selected entities first
+ // 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));
diff --git a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.test.ts b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.test.ts
index f8a6872054..e0e2d12b1a 100644
--- a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.test.ts
+++ b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest';
import { createSignal } from 'solid-js';
+import type { EntityData } from '@entity';
// Mock @core/context/quickAccess to break the import chain that pulls in
// LexicalMarkdown plugins -> themeSignals -> window.matchMedia
@@ -18,7 +19,10 @@ vi.mock('@core/context/quickAccess', () => ({
exclude: () => () => true,
}));
-import { createEntitySearchConfig } from './entityUtils';
+import {
+ createEntitySearchConfig,
+ sortEntitiesWithSelfFirst,
+} from './entityUtils';
import type { CombinedEntity } from './entityUtils';
import type { IUser } from '@core/user';
@@ -108,4 +112,250 @@ describe('createEntitySearchConfig', () => {
setDomain('other.com');
expect(config.boostFn!(userEntity)).toBe(0);
});
+
+ it('should boost current user (self) to top with high boost value', () => {
+ const [domain] = createSignal('example.com');
+ const [userId] = createSignal('current-user-id');
+ const config = createEntitySearchConfig(domain, userId);
+
+ const selfEntity: CombinedEntity = {
+ kind: 'user',
+ id: 'current-user-id',
+ data: {
+ id: 'current-user-id',
+ email: '[email protected]',
+ name: 'Current User',
+ } as IUser,
+ };
+
+ const boost = config.boostFn!(selfEntity);
+ expect(boost).toBe(10); // High boost for self
+ });
+
+ it('should prioritize self-boost over domain boost', () => {
+ const [domain] = createSignal('example.com');
+ const [userId] = createSignal('current-user-id');
+ const config = createEntitySearchConfig(domain, userId);
+
+ // Self user with same domain - should get self boost (10), not domain boost (0.5)
+ const selfEntity: CombinedEntity = {
+ kind: 'user',
+ id: 'current-user-id',
+ data: {
+ id: 'current-user-id',
+ email: '[email protected]',
+ name: 'Current User',
+ } as IUser,
+ };
+
+ const boost = config.boostFn!(selfEntity);
+ expect(boost).toBe(10);
+ });
+
+ it('should still apply domain boost for non-self users when currentUserId is provided', () => {
+ const [domain] = createSignal('example.com');
+ const [userId] = createSignal('current-user-id');
+ const config = createEntitySearchConfig(domain, userId);
+
+ const otherUserSameDomain: CombinedEntity = {
+ kind: 'user',
+ id: 'other-user-id',
+ data: {
+ id: 'other-user-id',
+ email: '[email protected]',
+ name: 'Other User',
+ } as IUser,
+ };
+
+ const boost = config.boostFn!(otherUserSameDomain);
+ expect(boost).toBe(0.5); // Domain boost for same domain
+ });
+
+ it('should not boost non-self users with different domain', () => {
+ const [domain] = createSignal('example.com');
+ const [userId] = createSignal('current-user-id');
+ const config = createEntitySearchConfig(domain, userId);
+
+ const otherUserDifferentDomain: CombinedEntity = {
+ kind: 'user',
+ id: 'other-user-id',
+ data: {
+ id: 'other-user-id',
+ email: '[email protected]',
+ name: 'Other User',
+ } as IUser,
+ };
+
+ const boost = config.boostFn!(otherUserDifferentDomain);
+ expect(boost).toBe(0);
+ });
+});
+
+describe('sortEntitiesWithSelfFirst', () => {
+ it('should move self to the front of the list', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ {
+ kind: 'user',
+ id: 'current-user',
+ data: {
+ id: 'current-user',
+ email: '[email protected]',
+ name: 'Me',
+ } as IUser,
+ },
+ {
+ kind: 'user',
+ id: 'user-2',
+ data: {
+ id: 'user-2',
+ email: '[email protected]',
+ name: 'User 2',
+ } as IUser,
+ },
+ ];
+
+ const result = sortEntitiesWithSelfFirst(entities, 'current-user');
+
+ expect(result[0].id).toBe('current-user');
+ expect(result[1].id).toBe('user-1');
+ expect(result[2].id).toBe('user-2');
+ });
+
+ it('should return entities unchanged if self is already first', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'user',
+ id: 'current-user',
+ data: {
+ id: 'current-user',
+ email: '[email protected]',
+ name: 'Me',
+ } as IUser,
+ },
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ ];
+
+ const result = sortEntitiesWithSelfFirst(entities, 'current-user');
+
+ expect(result).toEqual(entities);
+ });
+
+ it('should return entities unchanged if self is not in the list', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ {
+ kind: 'user',
+ id: 'user-2',
+ data: {
+ id: 'user-2',
+ email: '[email protected]',
+ name: 'User 2',
+ } as IUser,
+ },
+ ];
+
+ const result = sortEntitiesWithSelfFirst(entities, 'current-user');
+
+ expect(result).toEqual(entities);
+ });
+
+ it('should return entities unchanged if currentUserId is undefined', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ ];
+
+ const result = sortEntitiesWithSelfFirst(entities, undefined);
+
+ expect(result).toEqual(entities);
+ });
+
+ it('should only move user entities, not other entity types', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'entity',
+ id: 'current-user',
+ data: {
+ type: 'channel',
+ id: 'current-user',
+ name: 'Channel',
+ } as EntityData,
+ },
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ ];
+
+ const result = sortEntitiesWithSelfFirst(entities, 'current-user');
+
+ // Should not move the channel entity even though ID matches
+ expect(result[0].kind).toBe('entity');
+ expect(result[1].kind).toBe('user');
+ });
+
+ it('should not mutate the original array', () => {
+ const entities: CombinedEntity[] = [
+ {
+ kind: 'user',
+ id: 'user-1',
+ data: {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'User 1',
+ } as IUser,
+ },
+ {
+ kind: 'user',
+ id: 'current-user',
+ data: {
+ id: 'current-user',
+ email: '[email protected]',
+ name: 'Me',
+ } as IUser,
+ },
+ ];
+
+ const originalFirst = entities[0];
+ sortEntitiesWithSelfFirst(entities, 'current-user');
+
+ expect(entities[0]).toBe(originalFirst);
+ });
});
diff --git a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts
index 125f9a41b7..4cdd38d1b3 100644
--- a/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts
+++ b/js/app/packages/core/component/Properties/component/modal/shared/entityUtils.ts
@@ -164,13 +164,23 @@ export function getEntityTimestampedItem<T extends CombinedEntity>(
}
/**
- * Creates search config for entity searches with same-domain boost.
+ * Creates search config for entity searches with same-domain boost and self-boost.
* Uses the same preset as MentionsMenu and RecipientSelector for consistency.
+ * When currentUserId is provided, the current user will be boosted to the top of the list.
*/
export function createEntitySearchConfig<T extends CombinedEntity>(
- currentUserDomain: Accessor<string | undefined>
+ currentUserDomain: Accessor<string | undefined>,
+ currentUserId?: Accessor<string | undefined>
): FreshSortConfig<T> {
const boostFn = (item: T): number => {
+ // Boost self to top of list (high boost value ensures it appears first)
+ if (currentUserId && item.kind === 'user') {
+ const userId = currentUserId();
+ if (userId && item.data.id === userId) {
+ return 10; // High boost to ensure self appears at top
+ }
+ }
+
const userDomain = currentUserDomain();
if (!userDomain) return 0;
@@ -191,3 +201,28 @@ export function createEntitySearchConfig<T extends CombinedEntity>(
boostFn,
};
}
+
+/**
+ * Sorts entities with the current user (self) at the top of the list.
+ * Use this when displaying entities without a search query to ensure
+ * the current user appears first in the assignees list.
+ */
+export function sortEntitiesWithSelfFirst<T extends CombinedEntity>(
+ entities: T[],
+ currentUserId: string | undefined
+): T[] {
+ if (!currentUserId) return entities;
+
+ const selfIndex = entities.findIndex(
+ (e) => e.kind === 'user' && e.data.id === currentUserId
+ );
+
+ // If self not found or already at top, return as-is
+ if (selfIndex <= 0) return entities;
+
+ // Move self to the front
+ const result = [...entities];
+ const [self] = result.splice(selfIndex, 1);
+ result.unshift(self);
+ return result;
+}
diff --git a/js/app/packages/core/component/Properties/component/modal/shared/index.ts b/js/app/packages/core/component/Properties/component/modal/shared/index.ts
index 52ad23d8e8..b33a6d9fd4 100644
--- a/js/app/packages/core/component/Properties/component/modal/shared/index.ts
+++ b/js/app/packages/core/component/Properties/component/modal/shared/index.ts
@@ -12,6 +12,7 @@ export {
quickAccessItemToEntity,
entityDataToEntity,
userToEntity,
+ sortEntitiesWithSelfFirst,
} from './entityUtils';
export { PropertyEntitySelector } from './PropertyEntitySelector';
export { PropertyOptionSelector } from './PropertyOptionSelector';