| 1 | import type { PlatformInfo, PlatformMetadataVo } from '@/api/channels/channel.types' |
| 2 | import type { PlatType } from '@/app/config/platConfig' |
| 3 | import { cache } from 'react' |
| 4 | import { serverFetch } from '@/api/_server/server-fetch' |
| 5 | import { |
| 6 | getEnabledPlatformInfos, |
| 7 | getPublishPlatformInfos, |
| 8 | getTaskPlatformInfos, |
| 9 | normalizePlatformMetadataList, |
| 10 | platformInfoListToTuples, |
| 11 | } from '@/store/platformMetadata/utils' |
| 12 | |
| 13 | const PLATFORM_METADATA_REVALIDATE_SECONDS = 300 |
| 14 | |
| 15 | export const getPlatformMetadataInitialData = cache(async () => { |
| 16 | const res = await serverFetch<PlatformMetadataVo[]>( |
| 17 | 'v2/channels/platforms', |
| 18 | undefined, |
| 19 | { |
| 20 | revalidate: PLATFORM_METADATA_REVALIDATE_SECONDS, |
| 21 | tags: ['platform-metadata'], |
| 22 | }, |
| 23 | ) |
| 24 | |
| 25 | if (Number(res?.code) !== 0 || !res?.data) |
| 26 | return undefined |
| 27 | |
| 28 | return res.data |
| 29 | }) |
| 30 | |
| 31 | export const getPlatformMetadataSSR = cache(async (lng: string) => { |
| 32 | const rawList = await getPlatformMetadataInitialData() ?? [] |
| 33 | return normalizePlatformMetadataList(rawList, lng) |
| 34 | }) |
| 35 | |
| 36 | export async function getPlatformInfoMapSSR(lng: string) { |
| 37 | const { map } = await getPlatformMetadataSSR(lng) |
| 38 | return map |
| 39 | } |
| 40 | |
| 41 | export async function getPlatformInfoSSR(lng: string, platType?: PlatType | null) { |
| 42 | if (!platType) |
| 43 | return undefined |
| 44 | const map = await getPlatformInfoMapSSR(lng) |
| 45 | return map.get(platType) |
| 46 | } |
| 47 | |
| 48 | export async function getEnabledPlatformsSSR(lng: string) { |
| 49 | const { list } = await getPlatformMetadataSSR(lng) |
| 50 | return platformInfoListToTuples(getEnabledPlatformInfos(list)) |
| 51 | } |
| 52 | |
| 53 | export async function getPublishPlatformsSSR(lng: string) { |
| 54 | const { list } = await getPlatformMetadataSSR(lng) |
| 55 | return platformInfoListToTuples(getPublishPlatformInfos(list)) |
| 56 | } |
| 57 | |
| 58 | export async function getTaskPlatformsSSR(lng: string) { |
| 59 | const { list } = await getPlatformMetadataSSR(lng) |
| 60 | return platformInfoListToTuples(getTaskPlatformInfos(list)) |
| 61 | } |
| 62 | |
| 63 | export function getPlatformNameFromMap(map: Map<PlatType, PlatformInfo>, platType?: PlatType | null, fallback = '') { |
| 64 | if (!platType) |
| 65 | return fallback |
| 66 | return map.get(platType)?.name ?? (fallback || platType) |
| 67 | } |
| 68 |