Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { SessionBootstrap } from "@/components/session-bootstrap";
import AdminPage from "@/pages/admin";
import AboutPage from "@/pages/about";
import ContactsPage from "@/pages/contacts";
import LetterDetailPage from "@/pages/letter-detail";
import LettersPage from "@/pages/letters";
import LoginPage from "@/pages/login";
import PublicWallPage from "@/pages/public-wall";
Expand Down Expand Up @@ -37,6 +38,8 @@ export default function App() {
<Route path="/register" element={<RegisterPage />} />
<Route element={<RequireAuth />}>
<Route path="/letters" element={<LettersPage />} />
{/* 只读详情:已寄出/失败/发送中的信只能看不能改,入口是工作台卡片标题 */}
<Route path="/letters/:id" element={<LetterDetailPage />} />
<Route path="/compose/:id?" element={<ComposePage />} />
<Route path="/contacts" element={<ContactsPage />} />
<Route path="/settings" element={<SettingsPage />} />
Expand Down
69 changes: 69 additions & 0 deletions web/src/components/letter-preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Clock, Globe, Users } from "lucide-react";

import type { LetterRecipient } from "@/api/letters";
import { LETTER_CONTENT_CLASS } from "@/editor/content-style";
import { formatDate } from "@/lib/utils";

export interface LetterPreviewProps {
title: string;
bodyHtml: string;
recipients: LetterRecipient[];
/** 已格式化的送达时间(YYYY-MM-DD HH:mm);空串表示未设定。 */
sendAtLocal: string;
isPublic: boolean;
/** 写信日期(ISO);缺省为当前时间(写信页预览用「今天」)。 */
writtenAt?: string;
}

/**
* 信件预览:写信页与详情页共用同一份「按邮件实际渲染效果」,
* 不做第二套正文渲染(SPEC §5 + fleet-discipline §3)。
*/
export function LetterPreview({
title,
bodyHtml,
recipients,
sendAtLocal,
isPublic,
writtenAt,
}: LetterPreviewProps) {
return (
<aside className="rounded-[6px] border border-paper-300 bg-paper-50 shadow-[0_1px_3px_rgba(58,53,42,0.08)]">
<div className="border-b border-paper-300 px-5 py-3">
<p className="text-xs text-ink-500">预览 · 按邮件实际渲染效果</p>
</div>
<div className="space-y-4 p-5">
<div>
<p className="text-xs text-ink-500">主题</p>
<p className="mt-1 font-serif text-lg font-semibold text-ink-900">
{title || "(无标题)"}
</p>
</div>
<div className="border-t border-paper-200 pt-4">
{/* 正文为编辑器生成的受限集 HTML,与邮件正文同一套样式渲染 */}
<div
className={LETTER_CONTENT_CLASS}
dangerouslySetInnerHTML={{ __html: bodyHtml }}
/>
<p className="mt-6 border-t border-paper-200 pt-3 text-sm text-ink-500">
这封信写于 {formatDate(writtenAt ?? new Date().toISOString())},由时光邮局送达
</p>
</div>
<div className="flex flex-wrap gap-x-4 gap-y-1 border-t border-paper-200 pt-3 text-xs text-ink-600">
<span className="inline-flex items-center gap-1">
<Users className="size-3.5" />
{recipients.length > 0 ? `收件人 ${recipients.length} 位` : "未添加收件人"}
</span>
<span className="inline-flex items-center gap-1">
<Clock className="size-3.5" />
{sendAtLocal ? `送达 ${sendAtLocal.replace("T", " ")}` : "未设定送达时间"}
</span>
<span className="inline-flex items-center gap-1">
<Globe className="size-3.5" />
{isPublic ? "公开到信墙" : "仅收件人可见"}
</span>
</div>
</div>
</aside>
);
}
26 changes: 26 additions & 0 deletions web/src/lib/letter-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 信件状态的前端判定与样式:工作台卡片(链接目标)与详情页(编辑入口)共用同一份,
// 避免两处各写一遍导致漂移。
import type { LetterStatus } from "@/api/letters";

/**
* 是否允许编辑内容/收件人。与后端 `worker/src/routes/letters.ts` 的 `isEditableStatus` 同义:
* 不可编辑状态(sent / failed / sending)只能只读查看,入口是详情页而非编辑器。
*/
export function isEditableStatus(status: LetterStatus): boolean {
return status === "draft" || status === "scheduled" || status === "cancelled";
}

/** 状态徽标配色(默认 Badge 样式之外的状态覆盖)。 */
export function statusBadgeClass(status: LetterStatus): string | undefined {
switch (status) {
case "scheduled":
case "sending":
return "border-seal-500/30 bg-seal-500/10 text-seal-600";
case "failed":
return "border-[#9a2f22]/30 bg-[#9a2f22]/10 text-[#9a2f22]";
case "sent":
return "border-paper-300 bg-paper-100 text-ink-600";
default:
return undefined;
}
}
61 changes: 3 additions & 58 deletions web/src/pages/compose.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { useCallback, useEffect, useMemo, useState } from "react";
import { Link, useNavigate, useParams } from "react-router-dom";
import { ArrowLeft, CalendarClock, Clock, Globe, Send, Users } from "lucide-react";
import { ArrowLeft, CalendarClock, Send } from "lucide-react";
import type { JSONContent } from "@tiptap/react";

import { contactsApi, lettersApi } from "@/api/client";
import type { Contact } from "@/api/contacts";
import type { Letter, LetterPayload, LetterRecipient, LetterStatus } from "@/api/letters";
import { LetterPreview } from "@/components/letter-preview";
import { RecipientPicker } from "@/components/recipient-picker";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Switch } from "@/components/ui/switch";
import { LETTER_CONTENT_CLASS } from "@/editor/content-style";
import { RichTextEditor } from "@/editor/rich-text-editor";
import { getDefaultSendAt, getUserTimezone, isAfterNow, toLocalInputValue, toUtcIso } from "@/lib/datetime";
import { formatDate } from "@/lib/utils";

const STATUS_LABEL: Record<LetterStatus, string> = {
draft: "草稿",
Expand All @@ -37,60 +36,6 @@ function parseInitialContent(bodyJson: string, bodyHtml: string): string | JSONC
return bodyHtml;
}

function PreviewPanel({
title,
bodyHtml,
recipients,
sendAtLocal,
isPublic,
}: {
title: string;
bodyHtml: string;
recipients: LetterRecipient[];
sendAtLocal: string;
isPublic: boolean;
}) {
return (
<aside className="rounded-[6px] border border-paper-300 bg-paper-50 shadow-[0_1px_3px_rgba(58,53,42,0.08)]">
<div className="border-b border-paper-300 px-5 py-3">
<p className="text-xs text-ink-500">预览 · 按邮件实际渲染效果</p>
</div>
<div className="space-y-4 p-5">
<div>
<p className="text-xs text-ink-500">主题</p>
<p className="mt-1 font-serif text-lg font-semibold text-ink-900">
{title || "(无标题)"}
</p>
</div>
<div className="border-t border-paper-200 pt-4">
{/* 正文为编辑器生成的受限集 HTML,与邮件正文同一套样式渲染 */}
<div
className={LETTER_CONTENT_CLASS}
dangerouslySetInnerHTML={{ __html: bodyHtml }}
/>
<p className="mt-6 border-t border-paper-200 pt-3 text-sm text-ink-500">
这封信写于 {formatDate(new Date().toISOString())},由时光邮局送达
</p>
</div>
<div className="flex flex-wrap gap-x-4 gap-y-1 border-t border-paper-200 pt-3 text-xs text-ink-600">
<span className="inline-flex items-center gap-1">
<Users className="size-3.5" />
{recipients.length > 0 ? `收件人 ${recipients.length} 位` : "未添加收件人"}
</span>
<span className="inline-flex items-center gap-1">
<Clock className="size-3.5" />
{sendAtLocal ? `送达 ${sendAtLocal.replace("T", " ")}` : "未设定送达时间"}
</span>
<span className="inline-flex items-center gap-1">
<Globe className="size-3.5" />
{isPublic ? "公开到信墙" : "仅收件人可见"}
</span>
</div>
</div>
</aside>
);
}

export default function ComposePage() {
const { id } = useParams<{ id?: string }>();
const navigate = useNavigate();
Expand Down Expand Up @@ -355,7 +300,7 @@ export default function ComposePage() {
)}
</section>

<PreviewPanel
<LetterPreview
title={title}
bodyHtml={bodyHtml}
recipients={recipients}
Expand Down
136 changes: 136 additions & 0 deletions web/src/pages/letter-detail.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { render, screen } from "@testing-library/react";
import { MemoryRouter, Route, Routes } from "react-router-dom";
import { beforeEach, describe, expect, it } from "vitest";

import type { Letter } from "@/api/letters";
import { resetMockStore, seedMockLetters } from "@/api/mock";
import LetterDetailPage from "./letter-detail";

const EMAIL_SELF = "a@b.com";

function letter(overrides: Partial<Letter> = {}): Letter {
return {
id: "l1",
title: "写给未来的信",
body_html: "<p>多年以后,你还记得这个下午吗?</p>",
body_json: "{}",
send_at: "2030-01-01T01:00:00.000Z",
timezone: "Asia/Shanghai",
is_public: false,
recipients: [{ contact_id: null, name: "自己", email: EMAIL_SELF }],
status: "draft",
attempts: 0,
last_error: null,
sent_at: null,
created_at: "2026-10-24T01:00:00.000Z",
updated_at: "2026-10-24T01:00:00.000Z",
...overrides,
};
}

function renderDetail(id: string) {
return render(
<MemoryRouter initialEntries={[`/letters/${id}`]}>
<Routes>
<Route path="/letters/:id" element={<LetterDetailPage />} />
</Routes>
</MemoryRouter>,
);
}

beforeEach(() => {
resetMockStore();
});

describe("信件详情 · 已寄出", () => {
it("只读展示主题/正文/收件人与寄出信息,没有编辑入口", async () => {
seedMockLetters([
letter({
id: "l-sent",
title: "已寄出的信",
status: "sent",
sent_at: "2026-10-25T02:30:00.000Z",
}),
]);
renderDetail("l-sent");

// 主题与正文(正文按邮件样式渲染 body_html)
expect(await screen.findByText("已寄出的信")).toBeInTheDocument();
expect(screen.getByText("多年以后,你还记得这个下午吗?")).toBeInTheDocument();

// 状态徽标 + 返回入口
expect(screen.getByText("已寄出")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "返回我的信件" })).toHaveAttribute(
"href",
"/letters",
);

// 收件人快照可见
expect(screen.getByText(`<${EMAIL_SELF}>`)).toBeInTheDocument();

// 已寄出不可编辑:没有任何「编辑」按钮,也没有保存/寄出动作
expect(screen.queryByRole("link", { name: "编辑" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /存草稿|寄往未来/ })).not.toBeInTheDocument();
});
});

describe("信件详情 · 失败", () => {
it("展示失败原因与尝试次数,同样只读", async () => {
seedMockLetters([
letter({
id: "l-failed",
title: "失败的信",
status: "failed",
attempts: 3,
last_error: "SMTP 认证失败",
}),
]);
renderDetail("l-failed");

expect(await screen.findByText(/失败原因:SMTP 认证失败/)).toBeInTheDocument();
expect(screen.getByText(/已尝试 3 次/)).toBeInTheDocument();
expect(screen.getByText("失败")).toBeInTheDocument();
expect(screen.queryByRole("link", { name: "编辑" })).not.toBeInTheDocument();
});
});

describe("信件详情 · 发送中", () => {
it("发送中的信同样只能看,不给编辑入口", async () => {
seedMockLetters([
letter({ id: "l-sending", title: "正在寄出的信", status: "sending" }),
]);
renderDetail("l-sending");

expect(await screen.findByText("正在寄出的信")).toBeInTheDocument();
expect(screen.getByText("发送中")).toBeInTheDocument();
expect(screen.queryByRole("link", { name: "编辑" })).not.toBeInTheDocument();
});
});

describe("信件详情 · 直达可编辑信件", () => {
it("草稿直链打开给「编辑」入口,指向编辑器", async () => {
seedMockLetters([letter({ id: "l-draft", title: "草稿信", status: "draft" })]);
renderDetail("l-draft");

expect(await screen.findByText("草稿信")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "编辑" })).toHaveAttribute(
"href",
"/compose/l-draft",
);
});
});

describe("信件详情 · 不存在或无权查看", () => {
it("给出错误提示与返回入口,不回显内容", async () => {
renderDetail("nope");

expect(await screen.findByRole("alert")).toHaveTextContent(
"无法打开这封信:信件不存在或无权查看",
);
expect(screen.getByRole("link", { name: /返回我的信件/ })).toHaveAttribute(
"href",
"/letters",
);
expect(screen.queryByText("写给未来的信")).not.toBeInTheDocument();
});
});
Loading
Loading