import type { MediaPlatform, PlatformAccount } from "@geo/shared-types"; export interface PublishPlatformOption { id: string; name: string; shortName: string; accent: string; accountLabel?: string; bound?: boolean; } const platformCatalog: Record> = { toutiaohao: { id: "toutiaohao", name: "头条号", shortName: "头", accent: "#f5222d" }, baijiahao: { id: "baijiahao", name: "百家号", shortName: "百", accent: "#31445a" }, sohuhao: { id: "sohuhao", name: "搜狐号", shortName: "搜", accent: "#fa8c16" }, qiehao: { id: "qiehao", name: "企鹅号", shortName: "Q", accent: "#111827" }, zhihu: { id: "zhihu", name: "知乎", shortName: "知", accent: "#1677ff" }, wangyihao: { id: "wangyihao", name: "网易号", shortName: "网", accent: "#ff4d4f" }, jianshu: { id: "jianshu", name: "简书", shortName: "简", accent: "#f56a5e" }, bilibili: { id: "bilibili", name: "bilibili", shortName: "B", accent: "#eb2f96" }, juejin: { id: "juejin", name: "稀土掘金", shortName: "掘", accent: "#1677ff" }, smzdm: { id: "smzdm", name: "什么值得买", shortName: "值", accent: "#f5222d" }, weixin_gzh: { id: "weixin_gzh", name: "微信公众号", shortName: "微", accent: "#13c26b" }, zol: { id: "zol", name: "中关村在线", shortName: "Z", accent: "#ff4d4f" }, dongchedi: { id: "dongchedi", name: "懂车帝", shortName: "懂", accent: "#fadb14" }, }; export function getPublishPlatformMeta(platformId: string): Omit { const normalized = normalizePublishPlatformId(platformId); const fallbackShortName = normalized.slice(0, 1).toUpperCase() || "?"; return platformCatalog[normalized] ?? { id: normalized, name: normalized || "--", shortName: fallbackShortName, accent: "#667085", }; } export function normalizePublishPlatformId(platformId?: string | null): string { return String(platformId ?? "").trim(); } export function normalizePublishPlatformIds(platformIds?: Array): string[] { return Array.from( new Set( (platformIds ?? []) .map((item) => normalizePublishPlatformId(item)) .filter(Boolean), ), ); } export function buildPublishPlatformOptions( platforms: MediaPlatform[], accounts?: PlatformAccount[] | null, ): PublishPlatformOption[] { const boundPlatformIds = new Set( (accounts ?? []).map((account) => normalizePublishPlatformId(account.platform_id)).filter(Boolean), ); return platforms.map((platform) => { const id = normalizePublishPlatformId(platform.platform_id); return { id, name: platform.name, shortName: platform.short_name, accent: platform.accent_color, bound: boundPlatformIds.has(id), }; }); } export function serializeTargetPlatforms(platformIds: string[]): string | undefined { const next = normalizePublishPlatformIds(platformIds); return next.length ? next.join(",") : undefined; } export function parseTargetPlatforms(value?: string | null): string[] { if (!value) { return []; } return normalizePublishPlatformIds(value.split(",")); } export function getPublishPlatformLabel(platformId: string): string { return getPublishPlatformMeta(platformId).name; } export function formatPublishPlatformSummary(value?: string | null): string { const labels = parseTargetPlatforms(value).map(getPublishPlatformLabel); return labels.length ? labels.join("、") : "--"; } export function formatPublishPlatformList(platformIds?: string[] | null): string { const labels = normalizePublishPlatformIds(platformIds ?? []).map(getPublishPlatformLabel); return labels.length ? labels.join("、") : "--"; }