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: fresh sort dm boost (#1696)

gbirmand3d59affeb58
diff --git a/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx b/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
index 6e3f9b538a..304b355f47 100644
--- a/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
+++ b/js/app/packages/app/component/PropertyFilter/FilterValueEntity.tsx
@@ -182,12 +182,12 @@ export const FilterValueEntity: Component<FilterValueEntityProps> = (props) => {
});
// Search function for fuzzy matching (same config as PropertyEntitySelector)
- const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain, currentUserId),
- getEntitySearchText,
- isChannelEntity,
- getEntityTimestampedItem
- );
+ const entitySearch = createFreshSearch<CombinedEntity>({
+ config: createEntitySearchConfig(currentUserDomain, currentUserId),
+ getName: getEntitySearchText,
+ isChannelItem: isChannelEntity,
+ getTimestamp: getEntityTimestampedItem,
+ });
// Get selected entity IDs for filtering
const selectedIds = createMemo(
diff --git a/js/app/packages/app/component/command/useCommandItems.ts b/js/app/packages/app/component/command/useCommandItems.ts
index 67ef96a9a2..142c145873 100644
--- a/js/app/packages/app/component/command/useCommandItems.ts
+++ b/js/app/packages/app/component/command/useCommandItems.ts
@@ -44,16 +44,10 @@ function isUserItem(item: CommandMenuItem): item is UserItem {
return item.kind === 'user';
}
-function isChannelItem(item: CommandMenuItem): boolean {
- // only dms get the "channel boost" in command menu
- return item.bucket === 'dm';
-}
-
-// tune-able freshSearch for query vs non-query sort
function createSearchConfig(hasQuery: boolean) {
return {
useViewedAt: true,
- channelBoost: hasQuery ? 1.8 : 1.0,
+ dmBoost: hasQuery ? 1.8 : 1.0,
fuzzyWeight: hasQuery ? 0.7 : 0.0,
timeWeight: hasQuery ? 0.3 : 0.9,
minFuzzyThreshold: hasQuery ? 0.1 : 0,
@@ -188,12 +182,12 @@ export function useCommandItems(
const search = createMemo(() => {
const q = query();
const hasQuery = q.trim().length > 0;
- return createFreshSearch<CommandMenuItem>(
- createSearchConfig(hasQuery),
- (item) => item.searchText,
- isChannelItem,
- (item) => item.timestamps
- );
+ return createFreshSearch<CommandMenuItem>({
+ config: createSearchConfig(hasQuery),
+ getName: (item) => item.searchText,
+ isDmItem: (item) => item.bucket === 'dm',
+ getTimestamp: (item) => item.timestamps,
+ });
});
const filteredItems = createMemo(() => {
diff --git a/js/app/packages/app/component/next-soup/search-context.tsx b/js/app/packages/app/component/next-soup/search-context.tsx
index 0c07fb45ed..dacfea3db6 100644
--- a/js/app/packages/app/component/next-soup/search-context.tsx
+++ b/js/app/packages/app/component/next-soup/search-context.tsx
@@ -1,16 +1,26 @@
-import type { EntityData } from '@entity';
import {
type Accessor,
createContext,
- createMemo,
type FlowComponent,
useContext,
} from 'solid-js';
import { useQuickAccessEntities } from '@core/component/Properties/component/modal';
import { EntityType } from '@service-properties/generated/schemas/entityType';
+import type { EntityTypeItemMap } from '@core/component/Properties/component/modal/shared/entityUtils';
+
+const SEARCH_ENTITY_TYPES = [
+ EntityType.CHANNEL,
+ EntityType.CHAT,
+ EntityType.DOCUMENT,
+ EntityType.PROJECT,
+] as const;
+
+type SearchEntityType = (typeof SEARCH_ENTITY_TYPES)[number];
+
+export type SearchPoolItem = EntityTypeItemMap[SearchEntityType];
interface SearchContextValue {
- entityPool: Accessor<EntityData[]>;
+ entityPool: Accessor<SearchPoolItem[]>;
}
const SearchContext = createContext<SearchContextValue>();
@@ -24,19 +34,10 @@ export const useSearchContext = () => {
};
export const SearchProvider: FlowComponent = (props) => {
- const { items } = useQuickAccessEntities(() => [
- EntityType.CHANNEL,
- EntityType.CHAT,
- EntityType.DOCUMENT,
- EntityType.PROJECT,
- ]);
-
- const entityPool = createMemo<EntityData[]>(() =>
- items().map((item) => item.data)
- );
+ const { items } = useQuickAccessEntities(() => [...SEARCH_ENTITY_TYPES]);
return (
- <SearchContext.Provider value={{ entityPool }}>
+ <SearchContext.Provider value={{ entityPool: items }}>
{props.children}
</SearchContext.Provider>
);
diff --git a/js/app/packages/app/component/next-soup/search-utils.ts b/js/app/packages/app/component/next-soup/search-utils.ts
index 4eeb5edb5c..f45776b673 100644
--- a/js/app/packages/app/component/next-soup/search-utils.ts
+++ b/js/app/packages/app/component/next-soup/search-utils.ts
@@ -3,6 +3,7 @@ import { mergeAdjacentMacroEmTags } from '@core/util/searchHighlight';
import { createFreshSearch } from '@core/util/freshSort';
import type { EntityData, WithSearch } from '@entity';
import type { FilterConfig } from './filters/create-filter-state';
+import type { SearchPoolItem } from './search-context';
export const getValidSearchFilters = <T>(
filters: readonly FilterConfig<T>[]
@@ -77,16 +78,21 @@ export const nameFuzzySearchFilter = (
};
export const createSoupFreshSearch = () =>
- createFreshSearch<EntityData>(
- {
+ createFreshSearch<SearchPoolItem>({
+ config: {
useViewedAt: true,
channelBoost: 3,
+ dmBoost: 1.5,
fuzzyWeight: 0.7,
timeWeight: 0.3,
minFuzzyThreshold: 0.1,
commaSeparatedChannelMatch: true,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => item
- );
+ getName: (item) => item.data.name,
+ isChannelItem: (item) => item.data.type === 'channel',
+ isDmItem: (item) => item.bucket === 'dm',
+ getTimestamp: (item) => ({
+ viewedAt: item.data.viewedAt,
+ updatedAt: item.data.updatedAt,
+ }),
+ });
diff --git a/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts b/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts
index 482481d1bb..558286850d 100644
--- a/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts
+++ b/js/app/packages/app/component/next-soup/soup-view/create-search-state.ts
@@ -168,7 +168,7 @@ export const createSearchState = ({
// NOTE: this is a temporary hack because the fresh search fuzzy library
// does not give us the highlighted matches
const results = nameFuzzySearchFilter(
- freshSearchResults.map((r) => r.item),
+ freshSearchResults.map((r) => r.item.data),
query
);
return results;
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 6997bca8bb..4c2de11002 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
@@ -140,12 +140,12 @@ export function useEntitiesForProperty(
});
// search function for fuzzy matching
- const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain, currentUserId),
- getEntitySearchText,
- isChannelEntity,
- getEntityTimestampedItem
- );
+ const entitySearch = createFreshSearch<CombinedEntity>({
+ config: createEntitySearchConfig(currentUserDomain, currentUserId),
+ getName: getEntitySearchText,
+ isChannelItem: isChannelEntity,
+ getTimestamp: getEntityTimestampedItem,
+ });
// get filtered entities based on search query
const filteredEntities = createMemo(() => {
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEmailSearchMention.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEmailSearchMention.ts
index 647be13abf..2f69627f66 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEmailSearchMention.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEmailSearchMention.ts
@@ -62,12 +62,11 @@ export function useEmailSearchMention(
);
});
- const emailSearch = createFreshSearch<EntityItem>(
- { timeWeight: 0, brevityWeight: 0.3 },
- (item) => item.searchText,
- (_item) => false,
- (item) => item.timestamps
- );
+ const emailSearch = createFreshSearch<EntityItem>({
+ config: { timeWeight: 0, brevityWeight: 0.3 },
+ getName: (item) => item.searchText,
+ getTimestamp: (item) => item.timestamps,
+ });
const emails = createLazyMemo((): EntityItem[] => {
const term = searchTerm();
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEntityMention.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEntityMention.ts
index 14668ed078..9ccc786e27 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEntityMention.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useEntityMention.ts
@@ -31,12 +31,12 @@ export function useEntityMention(
...(buckets as [EntityBucket, ...EntityBucket[]])
);
- const entitySearch = createFreshSearch<EntityItem>(
- { useViewedAt: true },
- (item) => item.searchText,
- (item) => item.bucket === 'channel',
- (item) => item.timestamps
- );
+ const entitySearch = createFreshSearch<EntityItem>({
+ config: { useViewedAt: true },
+ getName: (item) => item.searchText,
+ isChannelItem: (item) => item.bucket === 'channel',
+ getTimestamp: (item) => item.timestamps,
+ });
const entities = createLazyMemo(() => {
const term = searchTerm();
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useUsersMention.ts b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useUsersMention.ts
index 0289b9f9d5..6d8aefc488 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useUsersMention.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu/hooks/useUsersMention.ts
@@ -68,15 +68,16 @@ export function useUsersMention(
});
const userSearch = () =>
- createFreshSearch<UserItem>(
- FreshSearchPresets.baseUserSearch<UserItem>(
+ createFreshSearch<UserItem>({
+ config: FreshSearchPresets.baseUserSearch<UserItem>(
currentUserDomain,
(item) => item.data.email
),
- (item) => item.searchText,
- (_) => false,
- (item) => ({ lastInteraction: item.timestamps.lastInteraction })
- );
+ getName: (item) => item.searchText,
+ getTimestamp: (item) => ({
+ lastInteraction: item.timestamps.lastInteraction,
+ }),
+ });
const users = createLazyMemo(() => {
const term = searchTerm();
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 e4f31edf1c..ff6f4704d3 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
@@ -216,12 +216,12 @@ export function PropertyEntitySelector(props: EntityInputProps) {
return converted;
});
- const entitySearch = createFreshSearch<CombinedEntity>(
- createEntitySearchConfig(currentUserDomain, currentUserId),
- getEntitySearchText,
- isChannelEntity,
- getEntityTimestampedItem
- );
+ const entitySearch = createFreshSearch<CombinedEntity>({
+ config: createEntitySearchConfig(currentUserDomain, currentUserId),
+ getName: getEntitySearchText,
+ isChannelItem: isChannelEntity,
+ getTimestamp: getEntityTimestampedItem,
+ });
const filteredEntities = createMemo(() => {
const term = searchTerm();
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 0bacc4d9e2..0657c497ba 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
@@ -60,8 +60,9 @@ export function useQuickAccessEntities<T extends EntityType>(
const buckets = () => {
const entityType_ = entityType();
if (!entityType_) return null;
- if (!Array.isArray(entityType_)) return entityTypeToBuckets(entityType_);
- return entityType_.flatMap(entityTypeToBuckets);
+ if (Array.isArray(entityType_))
+ return entityType_.flatMap(entityTypeToBuckets);
+ return entityTypeToBuckets(entityType_);
};
const items = (): EntityTypeItemMap[T][] => {
const b = buckets();
diff --git a/js/app/packages/core/component/RecipientSelector.tsx b/js/app/packages/core/component/RecipientSelector.tsx
index ac5a208491..b881ee976b 100644
--- a/js/app/packages/core/component/RecipientSelector.tsx
+++ b/js/app/packages/core/component/RecipientSelector.tsx
@@ -333,18 +333,18 @@ export function RecipientSelector<K extends CombinedRecipientKind>(
});
// Create search function for recipients - only used for initial sorting with no query
- const recipientSearch = createFreshSearch<CombinedRecipientItem>(
- FreshSearchPresets.baseUserSearch<CombinedRecipientItem>(
+ const recipientSearch = createFreshSearch<CombinedRecipientItem>({
+ config: FreshSearchPresets.baseUserSearch<CombinedRecipientItem>(
currentUserDomain,
getRecipientOptionEmail
),
- getRecipientOptionTextValue,
- (item) => item.kind === 'channel',
- (item) => ({
+ getName: getRecipientOptionTextValue,
+ isChannelItem: (item) => item.kind === 'channel',
+ getTimestamp: (item) => ({
lastInteraction:
item.kind === 'user' ? item.data.lastInteraction : undefined,
- })
- );
+ }),
+ });
const augmentUserWithDmActivity = useAugmentUserWithDmActivity();
const recipients = createMemo(() => {
diff --git a/js/app/packages/core/util/freshSort.test.ts b/js/app/packages/core/util/freshSort.test.ts
index 56c549f0ba..bb38247a25 100644
--- a/js/app/packages/core/util/freshSort.test.ts
+++ b/js/app/packages/core/util/freshSort.test.ts
@@ -16,17 +16,20 @@ interface User extends MockItem {
}
function createSearch(opts: { useViewedAt?: boolean; channelBoost?: number }) {
- return createFreshSearch<MockItem>(
- {
+ return createFreshSearch<MockItem>({
+ config: {
useViewedAt: opts.useViewedAt,
channelBoost: opts.channelBoost,
timeWeight: 0.9,
fuzzyWeight: 0.1,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
}
describe('freshSort ordering', () => {
@@ -126,16 +129,19 @@ describe('freshSort ordering', () => {
describe('createFreshSearch with comma-separated channel matching', () => {
function createCommaSeparatedSearch() {
- return createFreshSearch<MockItem>(
- {
+ return createFreshSearch<MockItem>({
+ config: {
commaSeparatedChannelMatch: true,
fuzzyWeight: 0.9,
timeWeight: 0.1,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
}
it('matches channel with comma-separated query', () => {
@@ -286,16 +292,19 @@ describe('freshSort with all exact matches', () => {
{ id: '4', name: 'Design', type: 'item', viewedAt: now },
];
- const search = createFreshSearch<MockItem>(
- {
+ const search = createFreshSearch<MockItem>({
+ config: {
useViewedAt: true,
fuzzyWeight: 0.5,
timeWeight: 0.5,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, 'Design');
@@ -328,17 +337,20 @@ describe('freshSort with all exact matches', () => {
},
];
- const search = createFreshSearch<MockItem>(
- {
+ const search = createFreshSearch<MockItem>({
+ config: {
useViewedAt: true,
fuzzyWeight: 0.8,
timeWeight: 0.2,
channelBoost: 2.0,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, 'Design');
@@ -379,8 +391,8 @@ describe('boostFn functionality', () => {
];
const currentUserDomain = 'macro.com';
- const search = createFreshSearch<MockItemWithEmail>(
- {
+ const search = createFreshSearch<MockItemWithEmail>({
+ config: {
fuzzyWeight: 0.8,
timeWeight: 0.2,
boostFn: (item) => {
@@ -388,10 +400,13 @@ describe('boostFn functionality', () => {
return itemDomain === currentUserDomain ? 1.0 : 0; // 100% boost for same domain
},
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, 'o'); // All three match with 'o' in their names
@@ -419,8 +434,8 @@ describe('boostFn functionality', () => {
];
const currentUserDomain = 'macro.com';
- const search = createFreshSearch<MockItemWithEmail>(
- {
+ const search = createFreshSearch<MockItemWithEmail>({
+ config: {
fuzzyWeight: 0.8,
timeWeight: 0.2,
boostFn: (item) => {
@@ -428,10 +443,13 @@ describe('boostFn functionality', () => {
return itemDomain === currentUserDomain ? 0.5 : 0;
},
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, 'Ali');
@@ -463,16 +481,19 @@ describe('boostFn functionality', () => {
},
];
- const search = createFreshSearch<MockItemWithEmail>(
- {
+ const search = createFreshSearch<MockItemWithEmail>({
+ config: {
fuzzyWeight: 0.5,
timeWeight: 0.5,
boostFn: () => 0, // No boost
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, '');
@@ -499,8 +520,8 @@ describe('boostFn functionality', () => {
},
];
- const search = createFreshSearch<MockItemWithEmail>(
- {
+ const search = createFreshSearch<MockItemWithEmail>({
+ config: {
fuzzyWeight: 0.5,
timeWeight: 0.5,
channelBoost: 1.5,
@@ -509,10 +530,13 @@ describe('boostFn functionality', () => {
return itemDomain === 'macro.com' ? 0.3 : 0;
},
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, 'Design');
@@ -543,16 +567,19 @@ describe('boostFn functionality', () => {
},
];
- const search = createFreshSearch<MockItemWithEmail>(
- {
+ const search = createFreshSearch<MockItemWithEmail>({
+ config: {
fuzzyWeight: 0.5,
timeWeight: 0.5,
// boostFn is undefined
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ viewedAt: item.viewedAt, updatedAt: item.updatedAt })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({
+ viewedAt: item.viewedAt,
+ updatedAt: item.updatedAt,
+ }),
+ });
const results = search(items, '');
@@ -594,16 +621,16 @@ describe('DM activity timestamps for user ranking', () => {
},
];
- const search = createFreshSearch<User>(
- {
+ const search = createFreshSearch<User>({
+ config: {
fuzzyWeight: 0.6,
timeWeight: 0.3,
brevityWeight: 0.1,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ lastInteraction: item.lastInteraction })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({ lastInteraction: item.lastInteraction }),
+ });
const results = search(users, '');
@@ -650,16 +677,16 @@ describe('DM activity timestamps for user ranking', () => {
},
];
- const search = createFreshSearch<User>(
- {
+ const search = createFreshSearch<User>({
+ config: {
fuzzyWeight: 0.6,
timeWeight: 0.3,
brevityWeight: 0.1,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ lastInteraction: item.lastInteraction })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({ lastInteraction: item.lastInteraction }),
+ });
const results = search(users, 'Ali');
@@ -687,16 +714,16 @@ describe('DM activity timestamps for user ranking', () => {
},
];
- const search = createFreshSearch<User>(
- {
+ const search = createFreshSearch<User>({
+ config: {
fuzzyWeight: 0.6,
timeWeight: 0.3,
brevityWeight: 0.1,
},
- (item) => item.name,
- (item) => item.type === 'channel',
- (item) => ({ lastInteraction: item.lastInteraction })
- );
+ getName: (item) => item.name,
+ isChannelItem: (item) => item.type === 'channel',
+ getTimestamp: (item) => ({ lastInteraction: item.lastInteraction }),
+ });
const results = search(users, '');
diff --git a/js/app/packages/core/util/freshSort.ts b/js/app/packages/core/util/freshSort.ts
index a953c8e56a..2c81cb2a93 100644
--- a/js/app/packages/core/util/freshSort.ts
+++ b/js/app/packages/core/util/freshSort.ts
@@ -19,7 +19,7 @@ type NameFn<T> = (item: T) => string;
type TimestampFn<T> = (item: T) => TimestampedItem;
-type IsChannelFn<T> = (item: T) => boolean;
+type BooleanFn<T> = (item: T) => boolean;
type EmailFn<T> = (item: T) => string | undefined;
@@ -40,6 +40,8 @@ export interface FreshSortConfig<T> {
useViewedAt?: boolean;
/** Boost multiplier for channel items when query is present. Default: 1.0 (no boost) */
channelBoost?: number;
+ /** Boost multiplier for DM items. Default: 1.0 (no boost) */
+ dmBoost?: number;
/** Enable comma-separated matching for channel names. When enabled, query "a,b" matches channel name "a,c,b". Default: false */
commaSeparatedChannelMatch?: boolean;
/** Function to calculate per-item boost. Returns a boost multiplier (e.g., 0.2 for +20% boost). Default: undefined */
@@ -72,6 +74,7 @@ const DEFAULT_CONFIG = {
minFuzzyThreshold: 0.1,
useViewedAt: false,
channelBoost: 1.0,
+ dmBoost: 1.0,
commaSeparatedChannelMatch: false,
boostFn: undefined,
} as const;
@@ -132,7 +135,8 @@ function calculateBrevityScore(text: string): number {
function freshSort<T>(
filterResults: FilterResult<T>[],
config: FreshSortConfig<T> = {},
- isChannelItem: IsChannelFn<T>,
+ isChannelItem: BooleanFn<T>,
+ isDmItem: BooleanFn<T>,
getTimestamp: TimestampFn<T>
): FreshSortResult<T>[] {
const finalConfig = {
@@ -173,6 +177,8 @@ function freshSort<T>(
? finalConfig.channelBoost
: 1.0;
+ const dmMultiplier = isDmItem(result.original) ? finalConfig.dmBoost : 1.0;
+
// Apply per-item boost if boostFn is provided
const itemBoost = finalConfig.boostFn
? finalConfig.boostFn(result.original)
@@ -184,6 +190,7 @@ function freshSort<T>(
normalizedBrevityWeight * brevityScore) *
fuzzyPenalty *
channelMultiplier *
+ dmMultiplier *
(1 + itemBoost);
return {
@@ -200,12 +207,21 @@ function freshSort<T>(
return scoredResults;
}
-export function createFreshSearch<T>(
- config: FreshSortConfig<T> = {},
- getName: NameFn<T>,
- isChannelItem: IsChannelFn<T>,
- getTimestamp: TimestampFn<T>
-) {
+export interface CreateFreshSearchArgs<T> {
+ config?: FreshSortConfig<T>;
+ getName: NameFn<T>;
+ isChannelItem?: BooleanFn<T>;
+ isDmItem?: BooleanFn<T>;
+ getTimestamp: TimestampFn<T>;
+}
+
+export function createFreshSearch<T>({
+ config = {},
+ getName,
+ isChannelItem = () => false,
+ isDmItem = () => false,
+ getTimestamp,
+}: CreateFreshSearchArgs<T>) {
return (items: T[], query: string): FreshSortResult<T>[] => {
const finalConfig = { ...DEFAULT_CONFIG, ...config };
@@ -243,13 +259,25 @@ export function createFreshSearch<T>(
// Combine results
const allResults = [...channelResults, ...nonChannelResults];
- return freshSort(allResults, config, isChannelItem, getTimestamp);
+ return freshSort(
+ allResults,
+ config,
+ isChannelItem,
+ isDmItem,
+ getTimestamp
+ );
}
const fuzzyResults = fuzzy.filter(query, items, {
extract: getName,
});
- return freshSort(fuzzyResults, config, isChannelItem, getTimestamp);
+ return freshSort(
+ fuzzyResults,
+ config,
+ isChannelItem,
+ isDmItem,
+ getTimestamp
+ );
};
}