CS lib
This commit is contained in:
parent
cec664c8d0
commit
ff2320f08c
13 changed files with 297 additions and 297 deletions
|
@ -1,31 +1,31 @@
|
|||
import {createContext, PropsWithChildren, useContext} from "react";
|
||||
import {useStorageState} from '@/context/UseStorageState';
|
||||
import { createContext, PropsWithChildren, useContext } from "react";
|
||||
import { useStorageState } from '@/context/UseStorageState';
|
||||
|
||||
type TokenType = {
|
||||
token: string | null;
|
||||
setToken: (token: string | null) => void;
|
||||
isLoading: boolean;
|
||||
token: string | null;
|
||||
setToken: (token: string | null) => void;
|
||||
isLoading: boolean;
|
||||
}
|
||||
|
||||
const TokenContext = createContext<TokenType>({
|
||||
setToken: () => {
|
||||
},
|
||||
token: null,
|
||||
isLoading: true,
|
||||
});
|
||||
const TokenContext = createContext<TokenType>( {
|
||||
setToken: () => {
|
||||
},
|
||||
token: null,
|
||||
isLoading: true,
|
||||
} );
|
||||
|
||||
export const useToken = () => useContext(TokenContext);
|
||||
export const useToken = () => useContext( TokenContext );
|
||||
|
||||
export function AppProvider({ children }: PropsWithChildren) {
|
||||
const [[isLoading, token], setSession] = useStorageState('appToken');
|
||||
const [ [ isLoading, token ], setSession ] = useStorageState( 'appToken' );
|
||||
|
||||
const tokenContext: TokenType = {
|
||||
token,
|
||||
setToken: (token) => {
|
||||
setSession(token);
|
||||
},
|
||||
isLoading,
|
||||
};
|
||||
const tokenContext: TokenType = {
|
||||
token,
|
||||
setToken: (token) => {
|
||||
setSession( token );
|
||||
},
|
||||
isLoading,
|
||||
};
|
||||
|
||||
return <TokenContext.Provider value={tokenContext}>{children}</TokenContext.Provider>;
|
||||
return <TokenContext.Provider value={tokenContext}>{children}</TokenContext.Provider>;
|
||||
}
|
|
@ -2,66 +2,66 @@ import * as SecureStore from 'expo-secure-store';
|
|||
import * as React from 'react';
|
||||
import { Platform } from 'react-native';
|
||||
|
||||
type UseStateHook<T> = [[boolean, T | null], (value: T | null) => void];
|
||||
type UseStateHook<T> = [ [ boolean, T | null ], (value: T | null) => void ];
|
||||
|
||||
function useAsyncState<T>(
|
||||
initialValue: [boolean, T | null] = [true, null],
|
||||
initialValue: [ boolean, T | null ] = [ true, null ],
|
||||
): UseStateHook<T> {
|
||||
return React.useReducer(
|
||||
(state: [boolean, T | null], action: T | null = null): [boolean, T | null] => [false, action],
|
||||
initialValue
|
||||
) as UseStateHook<T>;
|
||||
return React.useReducer(
|
||||
(state: [ boolean, T | null ], action: T | null = null): [ boolean, T | null ] => [ false, action ],
|
||||
initialValue
|
||||
) as UseStateHook<T>;
|
||||
}
|
||||
|
||||
export async function setStorageItemAsync(key: string, value: string | null) {
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (value === null) {
|
||||
localStorage.removeItem(key);
|
||||
} else {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Local storage is unavailable:', e);
|
||||
}
|
||||
} else {
|
||||
if (value == null) {
|
||||
await SecureStore.deleteItemAsync(key);
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (value === null) {
|
||||
localStorage.removeItem( key );
|
||||
} else {
|
||||
localStorage.setItem( key, value );
|
||||
}
|
||||
} catch (e) {
|
||||
console.error( 'Local storage is unavailable:', e );
|
||||
}
|
||||
} else {
|
||||
await SecureStore.setItemAsync(key, value);
|
||||
if (value == null) {
|
||||
await SecureStore.deleteItemAsync( key );
|
||||
} else {
|
||||
await SecureStore.setItemAsync( key, value );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function useStorageState(key: string): UseStateHook<string> {
|
||||
// Public
|
||||
const [state, setState] = useAsyncState<string>();
|
||||
// Public
|
||||
const [ state, setState ] = useAsyncState<string>();
|
||||
|
||||
// Get
|
||||
React.useEffect(() => {
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
setState(localStorage.getItem(key));
|
||||
// Get
|
||||
React.useEffect( () => {
|
||||
if (Platform.OS === 'web') {
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
setState( localStorage.getItem( key ) );
|
||||
}
|
||||
} catch (e) {
|
||||
console.error( 'Local storage is unavailable:', e );
|
||||
}
|
||||
} else {
|
||||
SecureStore.getItemAsync( key ).then( (value: string | null) => {
|
||||
setState( value );
|
||||
} );
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Local storage is unavailable:', e);
|
||||
}
|
||||
} else {
|
||||
SecureStore.getItemAsync(key).then((value: string | null) => {
|
||||
setState(value);
|
||||
});
|
||||
}
|
||||
}, [key]);
|
||||
}, [ key ] );
|
||||
|
||||
// Set
|
||||
const setValue = React.useCallback(
|
||||
(value: string | null) => {
|
||||
setState(value);
|
||||
setStorageItemAsync(key, value);
|
||||
},
|
||||
[key]
|
||||
);
|
||||
// Set
|
||||
const setValue = React.useCallback(
|
||||
(value: string | null) => {
|
||||
setState( value );
|
||||
setStorageItemAsync( key, value );
|
||||
},
|
||||
[ key ]
|
||||
);
|
||||
|
||||
return [state, setValue];
|
||||
return [ state, setValue ];
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue