metacubexd/src/signals/index.ts

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-24 04:20:53 +08:00
import { makePersisted } from '@solid-primitives/storage'
import ky from 'ky'
import { createSignal } from 'solid-js'
import { themes } from '~/constants'
export const useRequest = () => {
const e = endpoint()
return ky.create({
prefixUrl: e?.url,
headers: {
Authorization: e?.secret ? `Bearer ${e.secret}` : '',
},
})
}
2023-08-24 04:20:53 +08:00
export const [selectedEndpoint, setSelectedEndpoint] = makePersisted(
createSignal(''),
{
name: 'selectedEndpoint',
storage: localStorage,
},
)
export const [endpointList, setEndpointList] = makePersisted(
createSignal<
{
id: string
url: string
secret: string
}[]
>([]),
{ name: 'endpointList', storage: localStorage },
)
export const [curTheme, setCurTheme] = makePersisted(
createSignal<(typeof themes)[number]>('halloween'),
2023-08-24 04:20:53 +08:00
{ name: 'theme', storage: localStorage },
)
export const endpoint = () =>
endpointList().find(({ id }) => id === selectedEndpoint())
export const secret = () => endpoint()?.secret
2023-08-24 04:20:53 +08:00
export const wsEndpointURL = () => endpoint()?.url.replace('http', 'ws')