feat: enhance platform management and synchronization in ArticleEditor and PublishArticle components
This commit is contained in:
@@ -1,44 +1,66 @@
|
||||
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<string, Omit<PublishPlatformOption, "accountLabel">> = {
|
||||
zhihu: { id: "zhihu", name: "知乎", shortName: "知", accent: "#1677ff" },
|
||||
const platformCatalog: Record<string, Omit<PublishPlatformOption, "accountLabel" | "bound">> = {
|
||||
toutiaohao: { id: "toutiaohao", name: "头条号", shortName: "头", accent: "#f5222d" },
|
||||
baijiahao: { id: "baijiahao", name: "百家号", shortName: "百", accent: "#31445a" },
|
||||
toutiao: { id: "toutiao", name: "头条号", shortName: "头", accent: "#f5222d" },
|
||||
souhu: { id: "souhu", name: "搜狐号", shortName: "搜", accent: "#fa8c16" },
|
||||
qq: { id: "qq", name: "企鹅号", shortName: "Q", accent: "#111827" },
|
||||
wechat: { id: "wechat", name: "公众号", shortName: "微", accent: "#13c26b" },
|
||||
bilibili: { id: "bilibili", name: "bilibili", shortName: "B", accent: "#eb2f96" },
|
||||
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" },
|
||||
};
|
||||
|
||||
const mockBoundPlatformIds = ["zhihu", "wechat", "juejin", "bilibili", "jianshu", "baijiahao"];
|
||||
|
||||
export function getBoundPublishPlatforms(): PublishPlatformOption[] {
|
||||
return mockBoundPlatformIds
|
||||
.map((id) => platformCatalog[id])
|
||||
.filter((item): item is Omit<PublishPlatformOption, "accountLabel"> => Boolean(item));
|
||||
export function normalizePublishPlatformId(platformId?: string | null): string {
|
||||
return String(platformId ?? "").trim();
|
||||
}
|
||||
|
||||
export function getAllPublishPlatforms(): PublishPlatformOption[] {
|
||||
return Object.values(platformCatalog);
|
||||
export function normalizePublishPlatformIds(platformIds?: Array<string | null | undefined>): string[] {
|
||||
return Array.from(
|
||||
new Set(
|
||||
(platformIds ?? [])
|
||||
.map((item) => normalizePublishPlatformId(item))
|
||||
.filter(Boolean),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
export function isPlatformBound(platformId: string): boolean {
|
||||
return mockBoundPlatformIds.includes(platformId);
|
||||
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 = Array.from(new Set(platformIds.map((item) => item.trim()).filter(Boolean)));
|
||||
const next = normalizePublishPlatformIds(platformIds);
|
||||
return next.length ? next.join(",") : undefined;
|
||||
}
|
||||
|
||||
@@ -47,18 +69,12 @@ export function parseTargetPlatforms(value?: string | null): string[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Array.from(
|
||||
new Set(
|
||||
value
|
||||
.split(",")
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean),
|
||||
),
|
||||
);
|
||||
return normalizePublishPlatformIds(value.split(","));
|
||||
}
|
||||
|
||||
export function getPublishPlatformLabel(platformId: string): string {
|
||||
return platformCatalog[platformId]?.name ?? platformId;
|
||||
const normalized = normalizePublishPlatformId(platformId);
|
||||
return platformCatalog[normalized]?.name ?? normalized;
|
||||
}
|
||||
|
||||
export function formatPublishPlatformSummary(value?: string | null): string {
|
||||
@@ -67,6 +83,6 @@ export function formatPublishPlatformSummary(value?: string | null): string {
|
||||
}
|
||||
|
||||
export function formatPublishPlatformList(platformIds?: string[] | null): string {
|
||||
const labels = (platformIds ?? []).map(getPublishPlatformLabel).filter(Boolean);
|
||||
const labels = normalizePublishPlatformIds(platformIds ?? []).map(getPublishPlatformLabel);
|
||||
return labels.length ? labels.join("、") : "--";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user