import type { Check, FeedRow, Snapshot } from "../shared/ipc";
import type { Scheme } from "../shared/feed";
function escapeHtml(text: string): string {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&") return "&";
if (char === "<") return "<";
if (char === ">") return ">";
return """;
});
}
function time(at: number): string {
return new Date(at).toLocaleTimeString("zh-CN", { hour12: false });
}
function checkRow(check: Check): string {
return `
${check.pass ? "PASS" : "FAIL"}
${escapeHtml(check.label)}
${escapeHtml(check.detail)}
`;
}
function schemeRow(row: Scheme): string {
return `
| ${escapeHtml(row.name)} |
${escapeHtml(row.feed)} |
${escapeHtml(row.updates)} |
${escapeHtml(row.limits)} |
`;
}
function feedButton(row: FeedRow, active: boolean): string {
return ``;
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
本章只验证 feed 元数据、版本比较与提示状态;不下载、不替换安装包。
检查与决策
—
提示状态只影响 UI,不会触发下载或安装。
官方服务门槛
update.electronjs.org
需要公开 GitHub 仓库 + GitHub Releases;仅 macOS/Windows;
macOS 需签名;不支持 Linux。
`;
const score = root.querySelector('[data-testid="score"]');
const schemeCount = root.querySelector(
'[data-testid="scheme-count"]',
);
const schemesEl = root.querySelector('[data-testid="schemes"]');
const feedButtons = root.querySelector(
'[data-testid="feed-buttons"]',
);
const currentVersion = root.querySelector(
'[data-testid="current-version"]',
);
const decisionState = root.querySelector(
'[data-testid="decision-state"]',
);
const decisionEl = root.querySelector(
'[data-testid="decision"]',
);
const promptActions = root.querySelector(
'[data-testid="prompt-actions"]',
);
const promptNote = root.querySelector(
'[data-testid="prompt-note"]',
);
const feedLabel = root.querySelector(
'[data-testid="feed-label"]',
);
const feedRaw = root.querySelector('[data-testid="feed-raw"]');
const eligibilityEl = root.querySelector(
'[data-testid="eligibility"]',
);
const checksEl = root.querySelector('[data-testid="checks"]');
const checkCount = root.querySelector(
'[data-testid="check-count"]',
);
const eventsEl = root.querySelector('[data-testid="events"]');
let state: Snapshot | null = null;
let selectedFeed = "mac";
let prompt = "idle";
function feed(): FeedRow | undefined {
return state?.feeds.find((row) => row.id === selectedFeed);
}
function renderSchemes(): void {
if (!state) return;
if (schemesEl) schemesEl.innerHTML = state.schemes.map(schemeRow).join("");
if (schemeCount) schemeCount.textContent = `${state.schemes.length} 类方案`;
}
function renderFeed(): void {
if (!state) return;
const row = feed();
if (feedButtons) {
feedButtons.innerHTML = state.feeds
.map((item) => feedButton(item, item.id === selectedFeed))
.join("");
for (const item of state.feeds) {
root
.querySelector(`[data-testid="feed-${item.id}"]`)
?.addEventListener("click", () => {
selectedFeed = item.id;
prompt = "idle";
renderFeed();
renderDecision();
});
}
}
if (currentVersion) currentVersion.textContent = state.currentVersion;
if (feedLabel) feedLabel.textContent = row?.label ?? "—";
if (feedRaw) feedRaw.textContent = (row?.raw ?? "").slice(0, 520);
if (decisionState) {
decisionState.textContent = row?.decision.status ?? "—";
decisionState.className = `muted small status-${row?.decision.status ?? "none"}`;
}
}
function renderDecision(): void {
const row = feed();
if (!row) return;
const decision = row.decision;
if (decisionEl) {
const tag =
decision.status === "update"
? '有更新'
: decision.status === "none"
? '已是最新'
: 'feed 错误';
decisionEl.innerHTML = `
${tag}
${escapeHtml(decision.current)} → ${escapeHtml(decision.latest || "—")}
${escapeHtml(decision.reason)}
${escapeHtml(decision.notes || "无 release notes")}
`;
}
if (promptActions) {
promptActions.innerHTML =
decision.status === "update"
? `
`
: ``;
const later = root.querySelector(
'[data-testid="action-later"]',
);
const skip = root.querySelector(
'[data-testid="action-skip"]',
);
const download = root.querySelector(
'[data-testid="action-download"]',
);
const check = root.querySelector(
'[data-testid="action-check"]',
);
later?.addEventListener("click", () => {
prompt = "deferred";
updatePromptNote();
});
skip?.addEventListener("click", () => {
prompt = "skipped";
updatePromptNote();
});
download?.addEventListener("click", () => {
prompt = "downloading";
updatePromptNote();
});
check?.addEventListener("click", () => {
prompt = "idle";
updatePromptNote();
});
}
updatePromptNote();
}
function updatePromptNote(): void {
if (!promptNote) return;
const label =
prompt === "deferred"
? "已推迟:下次启动再提示(仅 UI 状态)"
: prompt === "skipped"
? "已忽略此版本:不再提示该版本(仅 UI 状态)"
: prompt === "downloading"
? "模拟下载中:不会写入磁盘,也不会安装(仅 UI 状态)"
: "提示状态只影响 UI,不会触发下载或安装。";
promptNote.textContent = label;
promptNote.className = `muted small prompt-note ${prompt === "idle" ? "" : "active"}`;
}
function renderEligibility(): void {
if (!eligibilityEl) return;
const rows: Array<[string, boolean, string]> = [
["公开 GitHub 仓库", true, "必需"],
["GitHub Releases", true, "必需"],
["macOS + 签名", true, "可用"],
["Windows", true, "可用"],
["Linux", false, "不支持"],
["私有仓库", false, "不支持"],
];
eligibilityEl.innerHTML = rows
.map(
([label, ok, note]) => `
${ok ? "可" : "否"}
${escapeHtml(label)}
${escapeHtml(note)}
`,
)
.join("");
}
function renderChecks(): void {
if (!state) return;
if (checksEl) checksEl.innerHTML = state.checks.map(checkRow).join("");
if (checkCount) {
const passed = state.checks.filter((check) => check.pass).length;
checkCount.textContent = `${passed}/${state.checks.length} 通过`;
}
if (score) {
score.textContent = `${state.score} / 100`;
score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`;
}
}
function renderEvents(): void {
if (!eventsEl || !state) return;
eventsEl.innerHTML =
state.events.length === 0
? `暂无活动`
: state.events
.slice(0, 8)
.map(
(row) => `
${time(row.at)}
${escapeHtml(row.kind)}
${escapeHtml(row.detail)}
`,
)
.join("");
}
function render(next: Snapshot): void {
state = next;
renderSchemes();
renderFeed();
renderDecision();
renderEligibility();
renderChecks();
renderEvents();
}
void window.api.updates.snapshot().then(render);
window.api.updates.onEvent(() => {
void window.api.updates.snapshot().then(render);
});
root
.querySelector('[data-testid="refresh"]')
?.addEventListener("click", async () => {
render(await window.api.updates.refresh());
});
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
render(await window.api.updates.refresh());
if (stage === "available") {
selectedFeed = "mac";
prompt = "idle";
renderFeed();
renderDecision();
document
.querySelector(".decision-card")
?.scrollIntoView({ block: "start" });
} else if (stage === "limits") {
selectedFeed = "invalid";
prompt = "idle";
renderFeed();
renderDecision();
document
.querySelector(".eligibility-card")
?.scrollIntoView({ block: "start" });
} else {
selectedFeed = "mac";
prompt = "idle";
renderFeed();
renderDecision();
document
.querySelector(".schemes-card")
?.scrollIntoView({ block: "start" });
}
};
window.__demo = async () => {
const next = await window.api.updates.snapshot();
state = next;
const find = (id: string) => next.feeds.find((row) => row.id === id);
return {
score: next.score,
checks: next.checks.length,
passed: next.checks.filter((check) => check.pass).length,
schemes: next.schemes.length,
feeds: next.feeds.length,
macStatus: find("mac")?.decision.status ?? "",
macLatest: find("mac")?.decision.latest ?? "",
winStatus: find("win")?.decision.status ?? "",
winLatest: find("win")?.decision.latest ?? "",
noneStatus: find("none")?.decision.status ?? "",
invalidStatus: find("invalid")?.decision.status ?? "",
noInstall:
next.checks.find((check) => check.id === "no-install")?.pass ?? false,
};
};
}