mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-03 03:10:50 +08:00
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
/**
|
|
* Quota cache that survives route switches.
|
|
*/
|
|
|
|
import { create } from 'zustand';
|
|
import type { AntigravityQuotaState, CodexQuotaState, GeminiCliQuotaState } from '@/types';
|
|
|
|
type QuotaUpdater<T> = T | ((prev: T) => T);
|
|
|
|
interface QuotaStoreState {
|
|
antigravityQuota: Record<string, AntigravityQuotaState>;
|
|
codexQuota: Record<string, CodexQuotaState>;
|
|
geminiCliQuota: Record<string, GeminiCliQuotaState>;
|
|
setAntigravityQuota: (updater: QuotaUpdater<Record<string, AntigravityQuotaState>>) => void;
|
|
setCodexQuota: (updater: QuotaUpdater<Record<string, CodexQuotaState>>) => void;
|
|
setGeminiCliQuota: (updater: QuotaUpdater<Record<string, GeminiCliQuotaState>>) => void;
|
|
clearQuotaCache: () => void;
|
|
}
|
|
|
|
const resolveUpdater = <T,>(updater: QuotaUpdater<T>, prev: T): T => {
|
|
if (typeof updater === 'function') {
|
|
return (updater as (value: T) => T)(prev);
|
|
}
|
|
return updater;
|
|
};
|
|
|
|
export const useQuotaStore = create<QuotaStoreState>((set) => ({
|
|
antigravityQuota: {},
|
|
codexQuota: {},
|
|
geminiCliQuota: {},
|
|
setAntigravityQuota: (updater) =>
|
|
set((state) => ({
|
|
antigravityQuota: resolveUpdater(updater, state.antigravityQuota)
|
|
})),
|
|
setCodexQuota: (updater) =>
|
|
set((state) => ({
|
|
codexQuota: resolveUpdater(updater, state.codexQuota)
|
|
})),
|
|
setGeminiCliQuota: (updater) =>
|
|
set((state) => ({
|
|
geminiCliQuota: resolveUpdater(updater, state.geminiCliQuota)
|
|
})),
|
|
clearQuotaCache: () =>
|
|
set({
|
|
antigravityQuota: {},
|
|
codexQuota: {},
|
|
geminiCliQuota: {}
|
|
})
|
|
}));
|