Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Download utils/usePersistentState.ts from enzostvs/hub-api-playground: direct link, hf CLI and curl.
- Browser
- Download file 798 Bytes
-
https://huggingface.co/spaces/enzostvs/hub-api-playground/resolve/31f728775a02ac838f67e1e46a30b1a36c0dc32e/utils/usePersistentState.ts
- Command line
-
hf download hf://spaces/enzostvs/hub-api-playground@31f728775a02ac838f67e1e46a30b1a36c0dc32e/utils/usePersistentState.ts
-
curl -L -o usePersistentState.ts https://huggingface.co/spaces/enzostvs/hub-api-playground/resolve/31f728775a02ac838f67e1e46a30b1a36c0dc32e/utils/usePersistentState.ts
798 Bytes
| import { useEffect, useState } from "react"; | |
| import axios from "@/utils/axios"; | |
| export const usePersistentState = (key: string, defaultValue: any) => { | |
| const [state, setState] = useState(() => { | |
| if (typeof window !== 'undefined') { | |
| const persistedState = localStorage?.getItem(key); | |
| if (persistedState) { | |
| axios.defaults.headers = {...JSON.parse(persistedState), Authorization: `Bearer ${JSON.parse(persistedState).Authorization}`} | |
| } | |
| return persistedState !== null ? JSON.parse(persistedState) : defaultValue; | |
| } | |
| return defaultValue; | |
| }); | |
| useEffect(() => { | |
| localStorage.setItem(key, JSON.stringify(state)); | |
| axios.defaults.headers = {...state, Authorization: `Bearer ${state.Authorization}`} | |
| }, [key, state]); | |
| return [state, setState]; | |
| } |