Spaces:
Sleeping
Sleeping
Download fairvalue-webapp/src/hooks/useUsageLimiter.ts from Britzzy/fairvalue-api: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/spaces/Britzzy/fairvalue-api/resolve/main/fairvalue-webapp/src/hooks/useUsageLimiter.ts
- Command line
-
hf download hf://spaces/Britzzy/fairvalue-api/fairvalue-webapp/src/hooks/useUsageLimiter.ts
-
curl -L -o useUsageLimiter.ts https://huggingface.co/spaces/Britzzy/fairvalue-api/resolve/main/fairvalue-webapp/src/hooks/useUsageLimiter.ts
1.02 kB
| import { useState, useEffect } from 'react'; | |
| const USAGE_KEY = 'fairvalue_evaluation_count'; | |
| const MAX_USES = 3; | |
| export function useUsageLimiter() { | |
| const [useCount, setUseCount] = useState<number>(0); | |
| const [isLocked, setIsLocked] = useState<boolean>(false); | |
| useEffect(() => { | |
| if (sessionStorage.getItem('fv_access_granted') === 'true') return; | |
| const stored = localStorage.getItem(USAGE_KEY); | |
| const count = stored ? parseInt(stored, 10) : 0; | |
| setUseCount(count); | |
| if (count >= MAX_USES) { | |
| setIsLocked(true); | |
| } | |
| }, []); | |
| const incrementUsage = () => { | |
| if (sessionStorage.getItem('fv_access_granted') === 'true') return true; | |
| const newCount = useCount + 1; | |
| setUseCount(newCount); | |
| localStorage.setItem(USAGE_KEY, newCount.toString()); | |
| if (newCount >= MAX_USES) { | |
| setIsLocked(true); | |
| return false; // Tells the caller it just locked | |
| } | |
| return true; // Still allowed | |
| }; | |
| return { useCount, isLocked, incrementUsage, MAX_USES }; | |
| } | |