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: channel search linking (#328)

gbirman9b8368a1b0e1
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 2c21371048..8b5563aac8 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -6,6 +6,7 @@ import {
emailRefetchInterval,
useEmailLinksStatus,
} from '@app/signal/emailAuth';
+import { URL_PARAMS as CHANNEL_PARAMS } from '@block-channel/constants';
import { URL_PARAMS as MD_PARAMS } from '@block-md/constants';
import { URL_PARAMS as PDF_PARAMS } from '@block-pdf/signal/location';
import { Button } from '@core/component/FormControls/Button';
@@ -53,6 +54,7 @@ import {
type EntityFilter,
type EntityType,
importantFilterFn,
+ isSearchEntity,
notDoneFilterFn,
type SortOption,
sortByCreatedAt,
@@ -466,7 +468,7 @@ export function UnifiedListView(props: UnifiedListViewProps) {
...result.item,
search: {
nameHighlight: result.nameHighlight,
- contentHighlights: null,
+ contentHitData: null,
source: 'local',
},
} as WithNotification<WithSearch<EntityData>>;
@@ -903,35 +905,34 @@ export function UnifiedListView(props: UnifiedListViewProps) {
? insertSplit({ type: blockName, id })
: replaceOrInsertSplit({ type: blockName, id });
- const location =
- 'search' in entity && entity.search.contentHighlights?.at(0)?.location;
- if (location) {
- handle?.activate();
- const blockHandle = await blockOrchestrator.getBlockHandle(id);
- switch (location.type) {
- case 'md':
- await blockHandle?.goToLocationFromParams({
- [MD_PARAMS.nodeId]: location.nodeId,
- });
- break;
- case 'pdf':
- console.log('go to pdf location', location);
- await blockHandle?.goToLocationFromParams({
- [PDF_PARAMS.searchPage]: location.searchPage.toString(),
- [PDF_PARAMS.searchRawQuery]: location.searchRawQuery,
- [PDF_PARAMS.searchHighlightTerms]: JSON.stringify(
- location.highlightTerms
- ),
- [PDF_PARAMS.searchSnippet]: location.searchSnippet,
- });
- break;
- }
- } else {
- handle?.activate();
+ handle?.activate();
+
+ if (!isSearchEntity(entity)) return;
+
+ const location = entity.search.contentHitData?.at(0)?.location;
+ if (!location) return;
+
+ const blockHandle = await blockOrchestrator.getBlockHandle(id);
+ switch (location.type) {
+ case 'md':
+ await blockHandle?.goToLocationFromParams({
+ [MD_PARAMS.nodeId]: location.nodeId,
+ });
+ break;
+ case 'pdf':
+ await blockHandle?.goToLocationFromParams({
+ [PDF_PARAMS.searchPage]: location.searchPage.toString(),
+ [PDF_PARAMS.searchRawQuery]: location.searchRawQuery,
+ [PDF_PARAMS.searchHighlightTerms]: JSON.stringify(
+ location.highlightTerms
+ ),
+ [PDF_PARAMS.searchSnippet]: location.searchSnippet,
+ });
+ break;
}
};
- const entityClickHandler: EntityClickHandler<EntityData> = (
+ const entityClickHandler: EntityClickHandler<EntityData> = async (
entity,
event,
options
@@ -947,7 +948,17 @@ export function UnifiedListView(props: UnifiedListViewProps) {
const handle = event.altKey
? insertSplit({ type: entity.type, id: entity.id })
: replaceOrInsertSplit({ type: entity.type, id: entity.id });
+
handle?.activate();
+
+ if (entity.type === 'channel' && isSearchEntity(entity)) {
+ const location = entity.search.contentHitData?.at(0)?.location;
+ if (!location) return;
+ const blockHandle = await blockOrchestrator.getBlockHandle(entity.id);
+ await blockHandle?.goToLocationFromParams({
+ [CHANNEL_PARAMS.message]: location.messageId,
+ });
+ }
};
const StyledTriggerLabel = (props: ParentProps) => {
diff --git a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
index b906d60963..4d7082b849 100644
--- a/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
+++ b/js/app/packages/macro-entity/src/components/EntityWithEverything.tsx
@@ -30,9 +30,14 @@ import {
isProjectContainedEntity,
type ProjectContainedEntity,
} from '../queries/project';
+import { isSearchEntity } from '../queries/search';
import type { EntityData, ProjectEntity } from '../types/entity';
import type { Notification, WithNotification } from '../types/notification';
-import type { WithSearch } from '../types/search';
+import type {
+ ChannelContentHitData,
+ ContentHitData,
+ WithSearch,
+} from '../types/search';
import type { EntityClickEvent, EntityClickHandler } from './Entity';
function UnreadIndicator(props: { active?: boolean }) {
@@ -64,6 +69,46 @@ function SharedBadge(props: { ownerId: string }) {
);
}
+function GenericContentHit(props: { data: ContentHitData }) {
+ return (
+ <div class="text-sm text-ink-muted truncate flex items-center">
+ <StaticMarkdown
+ markdown={props.data.content}
+ theme={unifiedListMarkdownTheme}
+ singleLine={true}
+ />
+ </div>
+ );
+}
+
+function ChannelMessageContentHit(props: { data: ChannelContentHitData }) {
+ const [userName] = useDisplayName(props.data.senderId);
+ const formattedDate = createFormattedDate(props.data.sentAt);
+
+ return (
+ <div class="flex gap-2 items-center min-w-0">
+ <div class="flex size-5 shrink-0 items-center justify-center">
+ <UserIcon id={props.data.senderId} size="xs" />
+ </div>
+ <div class="flex gap-2 text-sm w-full min-w-0 overflow-hidden items-baseline">
+ <div class="text-sm shrink-0 truncate min-w-0 font-medium">
+ {userName()}
+ </div>
+ <div class="shrink-0 font-mono text-xs uppercase text-ink-extra-muted">
+ {formattedDate()}
+ </div>
+ <div class="text-sm text-ink-muted truncate flex items-center flex-1 min-w-0">
+ <StaticMarkdown
+ markdown={props.data.content}
+ theme={unifiedListMarkdownTheme}
+ singleLine={true}
+ />
+ </div>
+ </div>
+ </div>
+ );
+}
+
// function ImportantBadge(props: { active?: boolean }) {
// return (
// <Show when={props.active}>
@@ -161,11 +206,11 @@ export function EntityWithEverything(
};
const searchHighlightName = () =>
- 'search' in props.entity && props.entity.search.nameHighlight;
+ isSearchEntity(props.entity) && props.entity.search.nameHighlight;
- const contentHighlights = () => {
- if (!('search' in props.entity)) return [];
- return props.entity.search.contentHighlights ?? [];
+ const contentHitData = () => {
+ if (!isSearchEntity(props.entity)) return [];
+ return props.entity.search.contentHitData ?? [];
};
const EntityTitle = () => {
@@ -258,7 +303,7 @@ export function EntityWithEverything(
props.entity.type === 'channel' ? props.entity : null
);
- const lastMessageContent = createMemo(
+ const latestMessageContent = createMemo(
() => channelEntity()?.latestMessage?.content
);
@@ -269,6 +314,15 @@ export function EntityWithEverything(
return userName();
});
+ const showLatestMessageInfo = () => {
+ return (
+ !props.showUnrollNotifications &&
+ props.entity.type === 'channel' &&
+ !isSearchEntity(props.entity) &&
+ !!props.entity.latestMessage?.content
+ );
+ };
+
return (
<div class="flex gap-2 items-center min-w-0 w-fit max-w-full overflow-hidden">
<span class="flex gap-1 truncate font-medium text-sm shrink-0 items-center">
@@ -289,25 +343,24 @@ export function EntityWithEverything(
</Show>
</span>
- <Show when={!props.showUnrollNotifications}>
+ <Show when={showLatestMessageInfo()}>
<div class="flex items-center gap-1">
{/*<ImportantBadge active={props.importantIndicatorActive} />*/}
<span class="font-medium shrink-0 truncate">
{userNameFromSender()}
</span>
</div>
- </Show>
-
- <Show when={!props.showUnrollNotifications && lastMessageContent()}>
- {(lastMessageContent) => (
- <div class="truncate shrink grow opacity-60 flex items-center">
- <StaticMarkdown
- markdown={lastMessageContent()}
- theme={unifiedListMarkdownTheme}
- singleLine={true}
- />
- </div>
- )}
+ <Show when={latestMessageContent()}>
+ {(lastMessageContent) => (
+ <div class="truncate shrink grow opacity-60 flex items-center">
+ <StaticMarkdown
+ markdown={lastMessageContent()}
+ theme={unifiedListMarkdownTheme}
+ singleLine={true}
+ />
+ </div>
+ )}
+ </Show>
</Show>
</span>
</div>
@@ -503,18 +556,17 @@ export function EntityWithEverything(
</Show>
</div>
</div>
- {/* Content Highlights from Search */}
- <Show when={contentHighlights().length > 0}>
+ {/* Content Hits from Search */}
+ <Show when={contentHitData().length > 0}>
<div class="relative row-2 grid gap-2 col-2 col-end-4 pb-2">
- <For each={contentHighlights()}>
- {(highlight) => (
- <div class="text-sm text-ink-muted truncate flex items-center">
- <StaticMarkdown
- markdown={highlight.content}
- theme={unifiedListMarkdownTheme}
- singleLine={true}
- />
- </div>
+ <For each={contentHitData()}>
+ {(data) => (
+ <Show
+ when={data.type === 'channel' && data}
+ fallback={<GenericContentHit data={data} />}
+ >
+ {(data) => <ChannelMessageContentHit data={data()} />}
+ </Show>
)}
</For>
</div>
@@ -524,7 +576,7 @@ export function EntityWithEverything(
when={
props.showUnrollNotifications &&
hasNotifications() &&
- contentHighlights().length === 0
+ contentHitData().length === 0
}
>
<div class="relative col-2 col-end-4 200 pb-2 gap-2">
diff --git a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
index dab81cc78b..6b1cad6613 100644
--- a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
+++ b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
@@ -26,6 +26,7 @@ import type {
EntityQueryOperations,
EntityQueryWithOperations,
} from '../queries/entity';
+import { isSearchEntity } from '../queries/search';
import type {
EntitiesFilter,
EntityComparator,
@@ -38,9 +39,6 @@ import type { WithSearch } from '../types/search';
import { CustomScrollbar } from './CustomScrollbar';
import { Entity } from './Entity';
-const isSearchEntity = (entity: EntityData): entity is WithSearch<EntityData> =>
- 'search' in entity;
-
/**
* Merges search data from two entities, preferring service source with local as fallback.
* - Uses service entity as base
@@ -48,9 +46,9 @@ const isSearchEntity = (entity: EntityData): entity is WithSearch<EntityData> =>
* - Falls back to local contentHighlights if service doesn't have any
*/
const mergeSearchEntities = <T extends EntityData>(
- first: T & WithSearch<EntityData>,
- second: T & WithSearch<EntityData>
-): T => {
+ first: WithSearch<T>,
+ second: WithSearch<T>
+): WithSearch<T> => {
const serviceEntity = first.search.source === 'service' ? first : second;
const localEntity = first.search.source === 'local' ? first : second;
@@ -60,11 +58,11 @@ const mergeSearchEntities = <T extends EntityData>(
...serviceEntity.search,
nameHighlight:
serviceEntity.search.nameHighlight || localEntity.search.nameHighlight,
- contentHighlights: serviceEntity.search.contentHighlights?.length
- ? serviceEntity.search.contentHighlights
- : localEntity.search.contentHighlights,
+ contentHitData: serviceEntity.search.contentHitData?.length
+ ? serviceEntity.search.contentHitData
+ : localEntity.search.contentHitData,
},
- } as T;
+ };
};
/**
@@ -204,8 +202,6 @@ interface UnifiedInfiniteListContext<T extends EntityData> {
entitySort?: Accessor<EntityComparator<T>>;
searchFilter?: Accessor<EntitiesFilter<T> | undefined>;
isSearchActive?: Accessor<boolean>;
- // TODO: deduplicate entities for same match
- deduplicate?: Accessor<(prev: T, next: T) => boolean>;
}
export function createUnifiedInfiniteList<T extends EntityData>({
diff --git a/js/app/packages/macro-entity/src/queries/search.ts b/js/app/packages/macro-entity/src/queries/search.ts
index 085f25e6c2..40f9594ec1 100644
--- a/js/app/packages/macro-entity/src/queries/search.ts
+++ b/js/app/packages/macro-entity/src/queries/search.ts
@@ -20,11 +20,7 @@ import { useHistory } from '@service-storage/history';
import { useInfiniteQuery } from '@tanstack/solid-query';
import { type Accessor, createMemo } from 'solid-js';
import type { EntityData } from '../types/entity';
-import type {
- FileTypeWithLocation,
- SearchLocation,
- WithSearch,
-} from '../types/search';
+import type { ContentHitData, SearchData, WithSearch } from '../types/search';
import type { EntityInfiniteQuery } from './entity';
import { queryKeys } from './key';
@@ -37,73 +33,86 @@ type InnerSearchResult =
| ChannelSearchResult
| ProjectSearchResult;
-const getLocationHighlights = (
- innerResults: DocumentSearchResult[],
- fileType: FileTypeWithLocation,
- searchQuery: string
-) => {
- const contentHighlights = innerResults.flatMap((r) => {
- const contents = r.highlight.content ?? [];
+type TypedInnerSearchResult =
+ | { results: InnerSearchResult[]; type?: undefined }
+ | { results: DocumentSearchResult[]; type: 'pdf'; searchQuery: string }
+ | { results: DocumentSearchResult[]; type: 'md' }
+ | { results: ChannelSearchResult[]; type: 'channel' };
+
+export const isSearchEntity = <T extends EntityData>(
+ entity: T
+): entity is WithSearch<T> => 'search' in entity;
+
+const getSearchData = (data: TypedInnerSearchResult): SearchData => {
+ let contentHitData: ContentHitData[] = [];
- return contents.map((content) => {
- const mergedContent = mergeAdjacentMacroEmTags(content);
- let location: SearchLocation | undefined;
- switch (fileType) {
- case 'md':
- location = { type: 'md' as const, nodeId: r.node_id };
- break;
- case 'pdf':
- try {
- const searchPage = parseInt(r.node_id);
- location = {
+ switch (data.type) {
+ case 'channel': {
+ contentHitData = data.results.flatMap((r) => {
+ const contents = r.highlight.content ?? [];
+ return contents.map((content) => ({
+ type: 'channel' as const,
+ id: r.message_id,
+ content: mergeAdjacentMacroEmTags(content),
+ senderId: r.sender_id,
+ sentAt: r.created_at,
+ location: {
+ type: 'channel' as const,
+ messageId: r.message_id,
+ },
+ }));
+ });
+ break;
+ }
+ case 'pdf': {
+ contentHitData = data.results.flatMap((r) => {
+ const contents = r.highlight.content ?? [];
+ return contents.map((content) => {
+ const mergedContent = mergeAdjacentMacroEmTags(content);
+ return {
+ type: 'pdf' as const,
+ content: mergeAdjacentMacroEmTags(content),
+ location: {
type: 'pdf' as const,
- searchPage,
+ searchPage: Number(r.node_id),
searchSnippet: extractSearchSnippet(mergedContent),
- searchRawQuery: searchQuery,
+ searchRawQuery: data.searchQuery,
highlightTerms: extractSearchTerms(mergedContent),
- };
- } catch (_e) {
- console.error('Cannot parse pdf serach info', r);
- location = undefined;
- }
- break;
- }
-
- return {
- content: mergedContent,
- location,
- };
- });
- });
-
- const nameHighlight = innerResults.at(0)?.highlight.name ?? null;
-
- return {
- nameHighlight: nameHighlight
- ? mergeAdjacentMacroEmTags(nameHighlight)
- : null,
- contentHighlights: contentHighlights.length > 0 ? contentHighlights : null,
- source: 'service' as const,
- };
-};
-
-const getHighlights = (innerResults: InnerSearchResult[]) => {
- const contentHighlights = innerResults.flatMap((r) => {
- const contents = r.highlight.content ?? [];
-
- return contents.map((content) => ({
- content: mergeAdjacentMacroEmTags(content),
- location: undefined,
- }));
- });
+ },
+ };
+ });
+ });
+ break;
+ }
+ case 'md': {
+ contentHitData = data.results.flatMap((r) => {
+ const contents = r.highlight.content ?? [];
+ return contents.map((content) => ({
+ type: 'md' as const,
+ content: mergeAdjacentMacroEmTags(content),
+ location: { type: 'md' as const, nodeId: r.node_id },
+ }));
+ });
+ break;
+ }
+ default: {
+ contentHitData = data.results.flatMap((r) => {
+ const contents = r.highlight.content ?? [];
+ return contents.map((content) => ({
+ content: mergeAdjacentMacroEmTags(content),
+ location: undefined,
+ }));
+ });
+ }
+ }
- const nameHighlight = innerResults.at(0)?.highlight.name ?? null;
+ const nameHighlight = data.results.at(0)?.highlight.name ?? null;
return {
nameHighlight: nameHighlight
? mergeAdjacentMacroEmTags(nameHighlight)
: null,
- contentHighlights: contentHighlights.length > 0 ? contentHighlights : null,
+ contentHitData: contentHitData.length > 0 ? contentHitData : null,
source: 'service' as const,
};
};
@@ -123,13 +132,23 @@ const useMapSearchResponseItem = () => {
if (!result.metadata || result.metadata.deleted_at) return;
const searchFileType =
result.file_type === 'docx' ? 'pdf' : result.file_type;
- const search = ['md', 'pdf'].includes(searchFileType)
- ? getLocationHighlights(
- result.document_search_results,
- searchFileType as FileTypeWithLocation,
- searchQuery
- )
- : getHighlights(result.document_search_results);
+ let search: SearchData;
+ if (searchFileType === 'md') {
+ search = getSearchData({
+ results: result.document_search_results,
+ type: 'md',
+ });
+ } else if (searchFileType === 'pdf') {
+ search = getSearchData({
+ results: result.document_search_results,
+ type: 'pdf',
+ searchQuery,
+ });
+ } else {
+ search = getSearchData({
+ results: result.document_search_results,
+ });
+ }
return {
type: 'document',
id: result.document_id,
@@ -149,7 +168,9 @@ const useMapSearchResponseItem = () => {
console.error('Email result not found', result);
return;
}
- const search = getHighlights(result.email_message_search_results);
+ const search = getSearchData({
+ results: result.email_message_search_results,
+ });
return {
type: 'email',
id: result.thread_id,
@@ -167,7 +188,9 @@ const useMapSearchResponseItem = () => {
}
case 'chat': {
if (!result.metadata || result.metadata.deleted_at) return;
- const search = getHighlights(result.chat_search_results);
+ const search = getSearchData({
+ results: result.chat_search_results,
+ });
let name = result.name;
if (!name || name === 'New Chat') {
const chat = history().find((item) => item.id === result.chat_id);
@@ -191,23 +214,13 @@ const useMapSearchResponseItem = () => {
(c) => c.id === result.channel_id
);
- // TODO: serialize correctly from backend
- const latestMessage = channelWithLatest?.latest_message
- ? {
- content: channelWithLatest.latest_message.content,
- senderId: channelWithLatest.latest_message.sender_id,
- createdAt:
- new Date(
- channelWithLatest.latest_message.created_at
- ).getTime() / 1000,
- }
- : undefined;
-
- const search = getHighlights(result.channel_message_search_results);
+ const search = getSearchData({
+ type: 'channel',
+ results: result.channel_message_search_results,
+ });
return {
type: 'channel',
- // TODO: distinguish channel name match from channel message match
id: result.channel_id,
name: channelWithLatest?.name ?? '',
ownerId: result.owner_id ?? '',
@@ -215,14 +228,16 @@ const useMapSearchResponseItem = () => {
updatedAt: result.metadata?.updated_at,
channelType: result.channel_type as ChannelType,
interactedAt: result.metadata?.interacted_at ?? undefined,
- latestMessage,
search,
};
}
case 'project': {
if (!result.metadata || result.metadata.deleted_at) return;
- const search = getHighlights(result.project_search_results);
+ const search = getSearchData({
+ results: result.project_search_results,
+ });
+
return {
type: 'project',
id: result.id,
diff --git a/js/app/packages/macro-entity/src/types/search.ts b/js/app/packages/macro-entity/src/types/search.ts
index b952e51a54..9c48c16071 100644
--- a/js/app/packages/macro-entity/src/types/search.ts
+++ b/js/app/packages/macro-entity/src/types/search.ts
@@ -1,3 +1,5 @@
+import type { EntityData } from './entity';
+
type MarkdownHighlightLocation = {
type: 'md';
nodeId: string;
@@ -11,19 +13,66 @@ type PdfHighlightLocation = {
searchRawQuery: string;
};
-export type FileTypeWithLocation = 'md' | 'pdf';
+type ChannelMessageHighlightLocation = {
+ type: 'channel';
+ messageId: string;
+};
-export type SearchLocation = MarkdownHighlightLocation | PdfHighlightLocation;
+export type SearchLocation =
+ | MarkdownHighlightLocation
+ | PdfHighlightLocation
+ | ChannelMessageHighlightLocation;
-type ContentHighlight = {
+export type ChannelContentHitData = {
+ type: 'channel';
+ id: string;
content: string;
- location?: SearchLocation;
+ senderId: string;
+ sentAt: number;
+ location: ChannelMessageHighlightLocation;
+};
+
+type MdContentHitData = {
+ type: 'md';
+ content: string;
+ location: MarkdownHighlightLocation;
+};
+
+type PdfContentHitData = {
+ type: 'pdf';
+ content: string;
+ location: PdfHighlightLocation;
+};
+
+type GenericContentHitData = {
+ type?: undefined;
+ content: string;
+ location?: never;
+};
+
+export type DocumentContentHitData =
+ | MdContentHitData
+ | PdfContentHitData
+ | GenericContentHitData;
+
+export type ContentHitData<T extends EntityData = EntityData> = T extends {
+ type: 'channel';
+}
+ ? ChannelContentHitData
+ : T extends { type: 'document'; fileType: 'md' }
+ ? MdContentHitData
+ : T extends { type: 'document'; fileType: 'pdf' | 'docx' }
+ ? PdfContentHitData
+ : T extends { type: 'document' }
+ ? DocumentContentHitData
+ : GenericContentHitData;
+
+export type SearchData<T extends EntityData = EntityData> = {
+ nameHighlight: string | null;
+ contentHitData: ContentHitData<T>[] | null;
+ source: 'local' | 'service';
};
-export type WithSearch<T extends object> = T & {
- search: {
- nameHighlight: string | null;
- contentHighlights: ContentHighlight[] | null;
- source: 'local' | 'service';
- };
+export type WithSearch<T extends EntityData> = T & {
+ search: SearchData<T>;
};