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.

1.9 KB
Raw Blame
import { fireAndForget } from '@core/util/fireAndForget';
import { throwOnErr } from '@core/util/result';
import { cognitionApiServiceClient } from '@service-cognition/client';
import { createConnectionWebsocketEffect } from '@service-connection/websocket';
import { queryOptions, useQuery } from '@tanstack/solid-query';
import { queryClient } from './client';
import { historyKeys } from './history/keys';
import { invalidatePreview } from './preview';
import { soupKeys } from './soup/keys';

const CHAT_STALE_TIME = 30 * 1000;
const CHAT_RENAMED_MESSAGE_TYPE = 'chat_renamed';

type ChatRenamedMessage = {
  type: typeof CHAT_RENAMED_MESSAGE_TYPE;
  chat_id: string;
  name: string;
};

function chatQueryOptions(id: string | undefined) {
  return queryOptions({
    queryKey: ['chat', id],
    queryFn: () =>
      throwOnErr(() => cognitionApiServiceClient.getChat({ chat_id: id! })),
    staleTime: CHAT_STALE_TIME,
    enabled: !!id,
  });
}

export function useChatQuery(chatId: () => string | undefined) {
  return useQuery(() => chatQueryOptions(chatId()));
}

export function useChatRenameWebsocketSync() {
  createConnectionWebsocketEffect((message) => {
    if (message.type !== CHAT_RENAMED_MESSAGE_TYPE) return;

    let data: ChatRenamedMessage;
    try {
      data = JSON.parse(message.data);
    } catch {
      console.error('unparsable chat rename payload', message);
      return;
    }

    if (data.type !== CHAT_RENAMED_MESSAGE_TYPE || !data.chat_id) return;

    queryClient.invalidateQueries({ queryKey: ['chat', data.chat_id] });
    queryClient.invalidateQueries({ queryKey: ['entity'] });
    queryClient.invalidateQueries({ queryKey: historyKeys.list.queryKey });
    queryClient.invalidateQueries({ queryKey: soupKeys.items._def });
    queryClient.invalidateQueries({ queryKey: soupKeys.astItems._def });
    queryClient.invalidateQueries({ queryKey: soupKeys.search._def });
    fireAndForget(invalidatePreview(data.chat_id), 'chat-rename-sync');
  });
}