import type { Check, Snapshot } from "../shared/ipc";
import type { Channel, Installer } from "../shared/channels";
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 channelRow(row: Channel): string {
return `
| ${escapeHtml(row.name)} |
${escapeHtml(row.artifact)} |
${escapeHtml(row.updateOwner)} |
${escapeHtml(row.review)} |
${row.inAppUpdate ? "可" : "否"} |
`;
}
function installerRow(row: Installer): string {
return `
${escapeHtml(row.id)} · ${escapeHtml(row.platform)} |
${escapeHtml(row.location)} |
${escapeHtml(row.uninstall)} |
${escapeHtml(row.silent)} |
${escapeHtml(row.residue)} |
`;
}
export function mountApp(root: HTMLElement): void {
root.innerHTML = `
渠道决定「谁负责更新」;商店与包管理器渠道不要叠加应用内自更新。
渠道矩阵
—
同一产品可以同时上多个渠道,但每个渠道的更新责任必须唯一。
`;
const score = root.querySelector('[data-testid="score"]');
const channelCount = root.querySelector(
'[data-testid="channel-count"]',
);
const channelsEl = root.querySelector(
'[data-testid="channels"]',
);
const channelNotes = root.querySelector(
'[data-testid="channel-notes"]',
);
const installerCount = root.querySelector(
'[data-testid="installer-count"]',
);
const installersEl = root.querySelector(
'[data-testid="installers"]',
);
const decisionsEl = root.querySelector(
'[data-testid="decisions"]',
);
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;
function renderChannels(): void {
if (!state) return;
if (channelsEl)
channelsEl.innerHTML = state.channels.map(channelRow).join("");
if (channelCount) {
channelCount.textContent = `${state.channels.length} 个渠道 · ${state.channels.filter((row) => row.inAppUpdate).length} 个支持应用内更新`;
}
if (channelNotes) {
channelNotes.innerHTML = state.channels
.map(
(row) => `
${escapeHtml(row.name)}
${escapeHtml(row.signing)}
${escapeHtml(row.note)}
`,
)
.join("");
}
}
function renderInstallers(): void {
if (!state) return;
if (installersEl) {
installersEl.innerHTML = state.installers.map(installerRow).join("");
}
if (installerCount) {
installerCount.textContent = `${state.installers.length} 种安装器`;
}
}
function renderDecisions(): void {
if (!state || !decisionsEl) return;
decisionsEl.innerHTML = state.decisions
.map(
(
row,
) => `
${escapeHtml(row.label)}
${escapeHtml(row.result.channel)}
${escapeHtml(row.result.artifact)}
${escapeHtml(row.result.reason)}
`,
)
.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;
renderChannels();
renderInstallers();
renderDecisions();
renderChecks();
renderEvents();
}
void window.api.channels.snapshot().then(render);
window.api.channels.onEvent(() => {
void window.api.channels.snapshot().then(render);
});
root
.querySelector('[data-testid="refresh"]')
?.addEventListener("click", async () => {
render(await window.api.channels.refresh());
});
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
render(await window.api.channels.refresh());
if (stage === "installers") {
document
.querySelector(".installers-card")
?.scrollIntoView({ block: "start" });
} else if (stage === "decisions") {
document
.querySelector(".decisions-card")
?.scrollIntoView({ block: "start" });
} else {
document
.querySelector(".channels-card")
?.scrollIntoView({ block: "start" });
}
};
window.__demo = async () => {
const next = await window.api.channels.snapshot();
state = next;
const storeIds = ["mas", "msstore", "flathub", "snap"];
const find = (id: string) => next.decisions.find((row) => row.id === id);
return {
score: next.score,
checks: next.checks.length,
passed: next.checks.filter((check) => check.pass).length,
channels: next.channels.length,
installers: next.installers.length,
storeOff: next.channels
.filter((row) => storeIds.includes(row.id))
.every((row) => !row.inAppUpdate),
directOn: next.channels
.filter((row) => ["direct", "github"].includes(row.id))
.every((row) => row.inAppUpdate),
enterpriseArtifact: find("enterprise")?.result.artifact ?? "",
personalChannel: find("personal")?.result.channel ?? "",
storeMac: find("store-mac")?.result.channel ?? "",
storeLinux: find("store-linux")?.result.channel ?? "",
decisions: next.decisions.length,
};
};
}