Files
geo/apps/admin-web/src/lib/kol-platform-options.ts
T

52 lines
1.5 KiB
TypeScript
Raw Normal View History

import type { MediaPlatform } from '@geo/shared-types'
export interface KolPlatformOption {
label: string
value: string
disabled?: boolean
}
export const KOL_PLATFORM_HINT_DEFAULT = '通用'
export function buildKolPlatformOptions(
defaultLabel: string,
platforms: MediaPlatform[] = [],
selectedValues: string[] = [],
): KolPlatformOption[] {
const defaultSelected = selectedValues.includes(KOL_PLATFORM_HINT_DEFAULT)
const options = [
{ label: defaultLabel, value: '通用' },
...platforms.map((item) => ({
label: item.name,
value: item.name,
disabled: defaultSelected,
})),
]
const knownValues = new Set(options.map((item) => item.value))
const customOptions = normalizeKolPlatformHints(selectedValues)
.filter((value) => !knownValues.has(value))
.map((value) => ({ label: value, value }))
return [...customOptions, ...options]
}
export function parseKolPlatformHints(value?: string | null): string[] {
return normalizeKolPlatformHints(String(value ?? '').split(/[、,;\n]+/))
}
export function normalizeKolPlatformHints(values?: Array<string | null | undefined>): string[] {
const next = Array.from(
new Set((values ?? []).map((item) => String(item ?? '').trim()).filter(Boolean)),
)
if (next.includes(KOL_PLATFORM_HINT_DEFAULT)) {
return [KOL_PLATFORM_HINT_DEFAULT]
}
return next
}
export function serializeKolPlatformHints(values?: Array<string | null | undefined>): string {
return normalizeKolPlatformHints(values).join('、')
}