Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
1.4 KB
Raw Blame
"use client";

/**
 * Shared React Query for fetching attachment text bodies via the
 * `/api/attachments/{id}/content` proxy.
 *
 * Same retry / staleTime / gcTime policy as AttachmentPreviewModal's local
 * TextBackedPreview, lifted out so the modal and the inline `AttachmentCard`
 * (file-card NodeView / readonly file-card / standalone AttachmentList) hit
 * the same cache key — opening the modal after the inline preview already
 * loaded a body does not refetch.
 */

import { useQuery } from "@tanstack/react-query";
import { api } from "@multica/core/api";

/**
 * `workspaceId` is the workspace that owns the attachment, when it may differ
 * from the current route's (a file linked from another workspace).
 */
export function useAttachmentHtmlText(
  attachmentId: string | null | undefined,
  workspaceId?: string | null,
) {
  return useQuery({
    queryKey: ["attachment-content", attachmentId ?? ""] as const,
    queryFn: () =>
      workspaceId
        ? api.getAttachmentTextContent(attachmentId as string, { workspaceId })
        : api.getAttachmentTextContent(attachmentId as string),
    enabled: !!attachmentId,
    // 413 / 415 won't become 200 on retry; a transport error is easier to
    // recover from by re-opening than waiting on background retries with
    // no UI affordance.
    retry: false,
    staleTime: 5 * 60_000,
    gcTime: 30 * 60_000,
  });
}