You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.4 KiB
57 lines
1.4 KiB
import {useState, useEffect} from 'react'
|
|
import {cleanToken, grabToken, grabTokenData, grabTokens, selectToken, storeToken} from "../functions/token";
|
|
|
|
export class Token {
|
|
id!: string
|
|
host!: string
|
|
permissions!: string[]
|
|
}
|
|
|
|
export interface UseTokenInterface {
|
|
token: Token|null
|
|
tokens: Token[]
|
|
switchToken(tokenId: string): void
|
|
deleteToken(tokenId: string): void
|
|
addToken(tokenId: string): void
|
|
}
|
|
|
|
export function useToken(): UseTokenInterface {
|
|
const [token, setToken] = useState<Token|null>(null);
|
|
const [tokens, setTokens] = useState<Token[]>([]);
|
|
useEffect(() => {
|
|
setToken(grabToken()?.payload)
|
|
|
|
let tokens = grabTokens().map((tokenData) => tokenData.payload)
|
|
setTokens(tokens)
|
|
|
|
return () => {
|
|
|
|
}
|
|
}, [])
|
|
|
|
const switchToken = (tokenId: string) => {
|
|
selectToken(tokenId)
|
|
setToken(grabToken()?.payload)
|
|
}
|
|
|
|
const deleteToken = (tokenId: string) => {
|
|
cleanToken(tokenId)
|
|
setToken(grabToken()?.payload)
|
|
}
|
|
|
|
const addToken = (token: string): boolean => {
|
|
if (!grabTokenData(token)) {
|
|
return false;
|
|
}
|
|
|
|
storeToken(token)
|
|
setToken(grabToken()?.payload)
|
|
setTokens([
|
|
...tokens,
|
|
grabToken()?.payload
|
|
])
|
|
return true
|
|
}
|
|
|
|
return {token, tokens, switchToken, deleteToken, addToken}
|
|
} |