返回 AiToEarn
fansRefreshCooldownStore.ts
根目录 / project / aitoearn-web / src / components / ChannelManager / fansRefreshCooldownStore.ts
1 /**
2 * fansRefreshCooldownStore - 频道粉丝数刷新冷却记录
3 * 按平台记录最近一次刷新时间,避免一小时内重复触发。
4 */
5
6 import type { PlatType } from '@/app/config/platConfig'
7 import { createPersistStore } from '@/utils/storage/createPersistStore'
8
9 interface FansRefreshCooldownState {
10 platformRecords: Partial<Record<PlatType, number>>
11 }
12
13 const FANS_REFRESH_COOLDOWN = 60 * 60 * 1000
14
15 function calcPlatformRefreshRemaining(
16 records: Partial<Record<PlatType, number>>,
17 platform: PlatType,
18 ) {
19 const lastRefreshTime = records[platform] || 0
20 return Math.max(0, FANS_REFRESH_COOLDOWN - (Date.now() - lastRefreshTime))
21 }
22
23 export const useFansRefreshCooldownStore = createPersistStore<
24 FansRefreshCooldownState,
25 {
26 markPlatformRefresh: (platform: PlatType) => void
27 canPlatformRefresh: (platform: PlatType) => boolean
28 getPlatformRefreshRemaining: (platform: PlatType) => number
29 }
30 >(
31 { platformRecords: {} },
32 (set, get) => ({
33 markPlatformRefresh(platform) {
34 const state = get()
35 set({
36 platformRecords: {
37 ...state.platformRecords,
38 [platform]: Date.now(),
39 },
40 })
41 },
42
43 canPlatformRefresh(platform) {
44 return calcPlatformRefreshRemaining(get().platformRecords, platform) <= 0
45 },
46
47 getPlatformRefreshRemaining(platform) {
48 return calcPlatformRefreshRemaining(get().platformRecords, platform)
49 },
50 }),
51 { name: 'aitoearn-channel-fans-refresh-cooldown', version: 1 },
52 )
53
53 lines TYPESCRIPT