| 1 | 'use client' |
| 2 | |
| 3 | import type { PlatType } from '@/app/config/platConfig' |
| 4 | import { useEffect, useState } from 'react' |
| 5 | import { useShallow } from 'zustand/shallow' |
| 6 | import { useGetClientLng } from '@/hooks/useSystem' |
| 7 | import { usePlatformMetadataStore } from '@/store/platformMetadata' |
| 8 | import { |
| 9 | getChannelPlatformInfos, |
| 10 | getEnabledPlatformInfos, |
| 11 | getPublishPlatformInfos, |
| 12 | getTaskPlatformInfos, |
| 13 | platformInfoListToTuples, |
| 14 | } from '@/store/platformMetadata/utils' |
| 15 | |
| 16 | export function usePlatformMetadata() { |
| 17 | const lng = useGetClientLng() |
| 18 | const { list, map, status, ensureLoaded } = usePlatformMetadataStore( |
| 19 | useShallow(state => ({ |
| 20 | list: state.list, |
| 21 | map: state.map, |
| 22 | status: state.status, |
| 23 | ensureLoaded: state.ensureLoaded, |
| 24 | })), |
| 25 | ) |
| 26 | |
| 27 | useEffect(() => { |
| 28 | ensureLoaded(lng) |
| 29 | }, [ensureLoaded, lng]) |
| 30 | |
| 31 | return { |
| 32 | list, |
| 33 | map, |
| 34 | status, |
| 35 | ready: status === 'success', |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | export function usePlatformMetadataReady() { |
| 40 | const status = usePlatformMetadataStore(state => state.status) |
| 41 | const lng = useGetClientLng() |
| 42 | const ensureLoaded = usePlatformMetadataStore(state => state.ensureLoaded) |
| 43 | |
| 44 | useEffect(() => { |
| 45 | ensureLoaded(lng) |
| 46 | }, [ensureLoaded, lng]) |
| 47 | |
| 48 | return status === 'success' |
| 49 | } |
| 50 | |
| 51 | export function usePlatformInfo(platType?: PlatType | null) { |
| 52 | const map = usePlatformMetadataStore(state => state.map) |
| 53 | const lng = useGetClientLng() |
| 54 | const ensureLoaded = usePlatformMetadataStore(state => state.ensureLoaded) |
| 55 | |
| 56 | useEffect(() => { |
| 57 | ensureLoaded(lng) |
| 58 | }, [ensureLoaded, lng]) |
| 59 | |
| 60 | return platType ? map.get(platType) : undefined |
| 61 | } |
| 62 | |
| 63 | export function usePlatformInfoMap() { |
| 64 | usePlatformMetadata() |
| 65 | return usePlatformMetadataStore(state => state.map) |
| 66 | } |
| 67 | |
| 68 | export function usePlatformInfoList(scene: 'all' | 'enabled' | 'publish' | 'task' = 'all') { |
| 69 | const { list } = usePlatformMetadata() |
| 70 | |
| 71 | if (scene === 'enabled') |
| 72 | return getEnabledPlatformInfos(list) |
| 73 | if (scene === 'publish') |
| 74 | return getPublishPlatformInfos(list) |
| 75 | if (scene === 'task') |
| 76 | return getTaskPlatformInfos(list) |
| 77 | return list |
| 78 | } |
| 79 | |
| 80 | export function useRegionSortedPlatforms() { |
| 81 | const { list } = usePlatformMetadata() |
| 82 | return platformInfoListToTuples(getChannelPlatformInfos(list)) |
| 83 | } |
| 84 | |
| 85 | export function useTaskPlatforms() { |
| 86 | return platformInfoListToTuples(usePlatformInfoList('task')) |
| 87 | } |
| 88 | |
| 89 | export function useFreshTaskPlatforms() { |
| 90 | const lng = useGetClientLng() |
| 91 | const { list, loadedLng, status, ensureLoaded } = usePlatformMetadataStore( |
| 92 | useShallow(state => ({ |
| 93 | list: state.list, |
| 94 | loadedLng: state.loadedLng, |
| 95 | status: state.status, |
| 96 | ensureLoaded: state.ensureLoaded, |
| 97 | })), |
| 98 | ) |
| 99 | const [ready, setReady] = useState(false) |
| 100 | |
| 101 | useEffect(() => { |
| 102 | let cancelled = false |
| 103 | const timeout = setTimeout(() => { |
| 104 | if (!cancelled) |
| 105 | setReady(true) |
| 106 | }, 3000) |
| 107 | |
| 108 | if (loadedLng !== lng && list.length === 0) |
| 109 | setReady(false) |
| 110 | else |
| 111 | setReady(true) |
| 112 | |
| 113 | ensureLoaded(lng, { force: true }).then(() => { |
| 114 | if (!cancelled) |
| 115 | setReady(true) |
| 116 | }).finally(() => { |
| 117 | clearTimeout(timeout) |
| 118 | }) |
| 119 | return () => { |
| 120 | cancelled = true |
| 121 | clearTimeout(timeout) |
| 122 | } |
| 123 | }, [ensureLoaded, list.length, loadedLng, lng]) |
| 124 | |
| 125 | return { |
| 126 | platforms: platformInfoListToTuples(getTaskPlatformInfos(list)), |
| 127 | ready: ready || list.length > 0 || status === 'error', |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | export function usePlatformName(platType?: PlatType | null, fallback?: string) { |
| 132 | const platformInfo = usePlatformInfo(platType) |
| 133 | return platformInfo?.name ?? fallback ?? platType ?? '' |
| 134 | } |
| 135 |