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
feat(task/history): add upsert mutation to history query, use tan-query history in mentions menu (#889)
Seamusc296fd783132
diff --git a/js/app/packages/block-md/component/ComposeTask.tsx b/js/app/packages/block-md/component/ComposeTask.tsxindex 17318ec40f..6fa54806a3 100644--- a/js/app/packages/block-md/component/ComposeTask.tsx+++ b/js/app/packages/block-md/component/ComposeTask.tsx@@ -40,10 +40,11 @@ import {queryKeys,useQueryClient as useEntityQueryClient,} from '@macro-entity';+import { useUpsertToHistoryMutation } from '@queries/history/history';import { useUserId } from '@service-gql/client';import { propertiesServiceClient } from '@service-properties/client';import type { PropertyDefinition } from '@service-properties/generated/schemas/propertyDefinition';-import { refetchHistory } from '@service-storage/history';+import { debounce } from '@solid-primitives/scheduled';import { useQuery } from '@tanstack/solid-query';import { Button } from '@ui/components/Button';@@ -78,7 +79,8 @@ async function createTaskWithProperties(taskTitle: string,taskContent: string,properties: Array<[string, PropertyApiValues]>,- definitions: Map<string, PropertyDefinition>+ definitions: Map<string, PropertyDefinition>,+ upsertToHistory: (params: { itemId: string; itemType: 'document' }) => void) {// Convert properties to API format (filter out null values)const propertyValues = properties.flatMap(([id, value]) => {@@ -108,12 +110,17 @@ async function createTaskWithProperties(});- // Invalidate queries to refresh DSS and history+ // Invalidate queries to refresh DSS and add to historyconst entityQueryClient = useEntityQueryClient();entityQueryClient.invalidateQueries({queryKey: queryKeys.all.dss,});- refetchHistory();++ // Upsert the new task to history+ upsertToHistory({+ itemId: documentId,+ itemType: 'document',+ });return documentId;}@@ -267,6 +274,9 @@ export function ComposeTask(props: ComposeTaskProps) {Record<string, PropertyApiValues>>(initialState.propertyValues);+ // History upsert mutation+ const upsertToHistoryMutation = useUpsertToHistoryMutation();+// draft saving logiclet hasInitializedFromDraft = isDraftLoaded();const debouncedSave = debounce(saveTaskComposerDraft, 300);@@ -378,7 +388,13 @@ export function ComposeTask(props: ComposeTaskProps) {const properties = structuredClone(Object.entries(unwrap(propertyValues)));- createTaskWithProperties(taskTitle, taskContent, properties, definitions());+ createTaskWithProperties(+ taskTitle,+ taskContent,+ properties,+ definitions(),+ (params) => upsertToHistoryMutation.mutate(params)+ );// Clear draft and reset formclearTaskComposerDraft();diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu.tsxindex 8176c6847a..85936f5aa9 100644--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu.tsx+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/MentionsMenu.tsx@@ -26,9 +26,9 @@ import {type EmailEntity,useEmails,} from '@macro-entity';+import { useHistoryQuery } from '@queries/history/history';import type { PaginatedSearchArgs } from '@service-search/client';import type { Item } from '@service-storage/generated/schemas/item';-import { useHistory } from '@service-storage/history';import { debounce } from '@solid-primitives/scheduled';import { globalSplitManager } from 'app/signal/splitLayout';import type { LexicalEditor } from 'lexical';@@ -409,9 +409,12 @@ export function MentionsMenu(props: {const [searchTerm, setSearchTerm] = createSignal<string>(props.menu.searchTerm());- const historyAccessor = props.history ?? useHistory();+ const historyQuery = useHistoryQuery();const history = createMemo(() => {- return historyAccessor().map(entityMapper('item'));+ if (props.history) {+ return props.history().map(entityMapper('item'));+ }+ return historyQuery.data?.map(entityMapper('item')) ?? [];});let emails: Accessor<Entity<'email'>[]>;diff --git a/js/app/packages/queries/history/history.ts b/js/app/packages/queries/history/history.tsindex aeed47c84f..500b589fbf 100644--- a/js/app/packages/queries/history/history.ts+++ b/js/app/packages/queries/history/history.ts@@ -164,3 +164,91 @@ export function useTrackViewedMutation(),}));}++type UpsertToHistoryParams = {+ itemId: string;+ itemType: CloudStorageItemType;+};++type UpsertToHistoryContext = {+ previousData: HistoryQueryResponse | undefined;+};++export function useUpsertToHistoryMutation(+ callbacks?: MutationCallbacks<+ void,+ Error,+ UpsertToHistoryParams,+ UpsertToHistoryContext+ >+) {+ return useMutation(() => ({+ mutationFn: async (params: UpsertToHistoryParams) => {+ await throwOnErr(+ async () =>+ await storageServiceClient.upsertItemToUserHistory({+ itemId: params.itemId,+ itemType: params.itemType,+ })+ );+ },+ ...withCallbacks<+ void,+ Error,+ UpsertToHistoryParams,+ UpsertToHistoryContext+ >(+ {+ onMutate: async (params) => {+ await queryClient.cancelQueries({+ queryKey: historyKeys.list.queryKey,+ });++ const previousData = queryClient.getQueryData<HistoryQueryResponse>(+ historyKeys.list.queryKey+ );++ queryClient.setQueryData<HistoryQueryResponse>(+ historyKeys.list.queryKey,+ (old) => {+ if (!old) return old;+ const existsIndex = old.data.findIndex(+ (item) => item.id === params.itemId+ );+ if (existsIndex >= 0) {+ const updatedData = updateItemViewedAt(+ old.data,+ params.itemId,+ Date.now()+ );+ const [updatedItem] = updatedData.splice(existsIndex, 1);+ return {+ ...old,+ data: [updatedItem, ...updatedData],+ };+ } else {+ return old;+ }+ }+ );++ return { previousData };+ },+ onError: (_err, _params, context) => {+ if (context?.previousData) {+ queryClient.setQueryData(+ historyKeys.list.queryKey,+ context.previousData+ );+ }+ },+ onSettled: () => {+ queryClient.invalidateQueries({+ queryKey: historyKeys.list.queryKey,+ });+ },+ },+ callbacks+ ),+ }));+}