Files
fusion/packages/dashboard/app/components/NativeStructurePreview.tsx
gsxdsm cf3f5e9bf0 FN-8292: embed native structures in mail messages
Embed selected native project structures directly in mailbox messages and drafts.

- Persist native structure embeds in message metadata and expose their shared types.
- Add composer selection, mailbox rendering, previews, and dashboard navigation for linked structures.
- Cover metadata persistence and mailbox embed behavior with core and dashboard tests.

Files changed:
 .changeset/fn-8292-mail-native-structure-embeds.md |  7 ++
 docs/dashboard-guide.md                            |  1 +
 .../message-metadata-native-structures.test.ts     | 25 +++++++
 .../__tests__/postgres/message-store.pg.test.ts    | 21 ++++++
 packages/core/src/index.ts                         |  2 +-
 packages/core/src/types.ts                         | 32 +++++++++
 packages/core/src/types/messages.ts                | 14 ++++
 packages/dashboard/app/components/MailboxModal.css | 29 ++++++++
 packages/dashboard/app/components/MailboxModal.tsx | 13 +++-
 .../components/MailboxNativeStructureEmbeds.tsx    | 37 ++++++++++
 packages/dashboard/app/components/MailboxView.tsx  | 14 +++-
 .../dashboard/app/components/MessageComposer.tsx   | 65 ++++++++++++++++--
 .../app/components/NativeStructurePreview.tsx      |  6 +-
 .../__tests__/MailboxModal.cache.test.tsx          |  8 +--
 .../app/components/__tests__/MailboxModal.test.tsx |  9 +++
 .../MailboxNativeStructureEmbeds.test.tsx          | 42 ++++++++++++
 .../app/components/__tests__/MailboxView.test.tsx  |  9 +++
 .../components/__tests__/MessageComposer.test.tsx  | 25 +++++++
 .../app/components/dashboard/MainContent.tsx       | 78 +++++++++++++++++++++-
 .../MainContent.mailbox-view-task.test.tsx         | 46 ++++++++++++-
 20 files changed, 459 insertions(+), 24 deletions(-)

Fusion-Task-Id: FN-8292

Fusion-Task-Lineage: ae6cfb9c-a764-407c-bd45-cfd879ccdd3e

Co-authored-by: Fusion (runfusion.ai) <noreply@runfusion.ai>
2026-07-18 19:45:08 -07:00

118 lines
5.5 KiB
TypeScript

import { memo, useEffect, useState } from "react";
import { BarChart3, CircleAlert, Flag, Lightbulb, Map, Target } from "lucide-react";
import type { NativeStructurePreviewResult, NativeStructureRef } from "@fusion/core";
import { fetchNativeStructurePreview } from "../api";
import "./NativeStructurePreview.css";
export interface NativeStructurePreviewProps {
ref: NativeStructureRef;
payload?: NativeStructurePreviewResult;
/** Attach-time label used only when a persisted target is no longer available. */
capturedLabel?: string;
onOpen: (ref: NativeStructureRef, payload: NativeStructurePreviewResult) => void;
}
const icons = {
mission: Map,
milestone: Flag,
"research-finding": Lightbulb,
"eval-result": BarChart3,
goal: Target,
} satisfies Record<NativeStructureRef["kind"], typeof Map>;
function isSupportedKind(kind: string): kind is NativeStructureRef["kind"] {
return Object.prototype.hasOwnProperty.call(icons, kind);
}
function unavailableLabel(kind: string): string {
return kind.replace(/-/g, " ");
}
/**
* FNXC:NativeStructureEmbed 2026-07-16-12:00:
* Chat and mail use this one memoized renderer for compact structure cards. Navigation remains
* owned by each consumer through `onOpen` because dashboard views use callback/view state rather
* than URL routes; rendering an anchor here would create dead destinations.
*/
export const NativeStructurePreview = memo(function NativeStructurePreview({ ref, payload, capturedLabel, onOpen }: NativeStructurePreviewProps) {
const supportedKind = isSupportedKind(ref.kind);
const refKey = `${ref.kind}\u0000${ref.id}\u0000${ref.projectId ?? ""}`;
const [fetchedPayload, setFetchedPayload] = useState<{ refKey: string; result: NativeStructurePreviewResult } | undefined>();
const [error, setError] = useState(false);
// FNXC:NativeStructureEmbed 2026-07-16-12:00: A ref update must not briefly render a prior fetch result; consumers can replace cards while messages or drafts rehydrate.
const result = payload ?? (fetchedPayload?.refKey === refKey ? fetchedPayload.result : undefined);
const Icon = supportedKind ? icons[ref.kind] : CircleAlert;
useEffect(() => {
/*
FNXC:NativeStructureEmbed 2026-07-19-18:00:
Refs can arrive from persisted chat/mail content, so reject a future or malformed kind before
fetching. The five-kind route is the sole resolver contract; roadmap-item must not trigger a
plugin read or turn an invalid icon lookup into a render crash.
*/
if (payload || !supportedKind) return;
let active = true;
setFetchedPayload(undefined);
setError(false);
void fetchNativeStructurePreview(ref)
.then((nextPayload) => {
if (active) setFetchedPayload({ refKey, result: nextPayload });
})
.catch(() => {
if (active) setError(true);
});
return () => { active = false; };
}, [payload, ref.kind, ref.id, ref.projectId, refKey]);
if (!supportedKind) {
return (
<section className="native-structure-preview native-structure-preview--unavailable" data-testid="native-structure-preview-unavailable" data-reason="missing">
<Icon aria-hidden="true" />
<div className="native-structure-preview__content"><span className="native-structure-preview__label">Preview unavailable</span><p>This structure is unavailable.</p></div>
</section>
);
}
// FNXC:NativeStructureEmbed 2026-07-16-14:05: A caller-supplied projection is authoritative after a transient fetch failure, so it must replace the error placeholder for the same ref.
if (error && !payload) {
return (
<section className="native-structure-preview native-structure-preview--unavailable" data-testid="native-structure-preview-error">
<Icon aria-hidden="true" />
<div className="native-structure-preview__content"><span className="native-structure-preview__label">Preview unavailable</span><p>Could not load this {unavailableLabel(ref.kind)}.</p></div>
</section>
);
}
if (!result) {
return (
<section className="native-structure-preview" data-testid="native-structure-preview-loading" aria-busy="true">
<Icon aria-hidden="true" />
<div className="native-structure-preview__content"><span className="native-structure-preview__label">Loading {unavailableLabel(ref.kind)}</span></div>
</section>
);
}
if (!result.available) {
return (
<section className="native-structure-preview native-structure-preview--unavailable" data-testid="native-structure-preview-unavailable" data-reason={result.reason}>
<Icon aria-hidden="true" />
<div className="native-structure-preview__content"><span className="native-structure-preview__label">{unavailableLabel(result.kind)}</span><strong className="native-structure-preview__title">{capturedLabel?.trim() || "Preview unavailable"}</strong><p>This structure is unavailable.</p></div>
</section>
);
}
return (
<section className="native-structure-preview" data-testid="native-structure-preview" data-kind={result.kind}>
<Icon aria-hidden="true" />
<div className="native-structure-preview__content">
<span className="native-structure-preview__label">{result.kindLabel}</span>
<strong className="native-structure-preview__title">{result.title}</strong>
<p className="native-structure-preview__excerpt">{result.excerpt}</p>
</div>
<button className="btn native-structure-preview__open" type="button" onClick={() => onOpen(ref, result)} aria-label={`Open ${result.kindLabel}: ${result.title}`}>
Open
</button>
</section>
);
});