metacubexd/src/signals/config.ts

70 lines
2.2 KiB
TypeScript
Raw Normal View History

import { makePersisted } from '@solid-primitives/storage'
import { createSignal } from 'solid-js'
2023-09-03 03:56:04 +08:00
import {
LATENCY_QUALITY_MAP_HTTP,
LATENCY_QUALITY_MAP_HTTPS,
PROXIES_ORDERING_TYPE,
2023-09-03 03:56:04 +08:00
PROXIES_PREVIEW_TYPE,
} from '~/constants'
2023-09-02 19:06:02 +08:00
import { setCurTheme } from '~/signals'
export const [proxiesPreviewType, setProxiesPreviewType] = makePersisted(
createSignal(PROXIES_PREVIEW_TYPE.Auto),
{ name: 'proxiesPreviewType', storage: localStorage },
)
export const [proxiesOrderingType, setProxiesOrderingType] = makePersisted(
createSignal(PROXIES_ORDERING_TYPE.NATURAL),
{ name: 'proxiesOrderingType', storage: localStorage },
)
2023-09-03 05:40:39 +08:00
export const [urlForLatencyTest, setUrlForLatencyTest] = makePersisted(
createSignal('https://www.gstatic.com/generate_204'),
2023-09-03 05:40:39 +08:00
{ name: 'urlForLatencyTest', storage: localStorage },
)
export const [autoCloseConns, setAutoCloseConns] = makePersisted(
createSignal(false),
{ name: 'autoCloseConns', storage: localStorage },
)
2023-09-02 19:06:02 +08:00
export const [autoSwitchTheme, setAutoSwitchTheme] = makePersisted(
createSignal(false),
{ name: 'autoSwitchTheme', storage: localStorage },
)
export const [favDayTheme, setFavDayTheme] = makePersisted(
createSignal('light'),
{ name: 'favDayTheme', storage: localStorage },
)
export const [favNightTheme, setFavNightTheme] = makePersisted(
createSignal('night'),
{ name: 'favNightTheme', storage: localStorage },
)
2023-09-02 21:00:20 +08:00
export const [renderInTwoColumn, setRenderInTwoColumn] = makePersisted(
createSignal(true),
{ name: 'renderInTwoColumn', storage: localStorage },
)
2023-09-02 19:06:02 +08:00
2023-09-03 05:40:39 +08:00
export const isLatencyTestByHttps = () =>
urlForLatencyTest().startsWith('https')
2023-09-03 03:56:04 +08:00
export const latencyQualityMap = () =>
isLatencyTestByHttps() ? LATENCY_QUALITY_MAP_HTTPS : LATENCY_QUALITY_MAP_HTTP
2023-09-02 19:06:02 +08:00
const setTheme = (isDark: boolean) => {
if (autoSwitchTheme()) {
if (isDark) {
setCurTheme(favNightTheme())
} else {
setCurTheme(favDayTheme())
}
}
}
export const applyThemeByMode = () =>
2023-09-02 19:06:02 +08:00
setTheme(window.matchMedia('(prefers-color-scheme: dark)').matches)
export const useAutoSwitchTheme = () => {
2023-09-02 19:06:02 +08:00
applyThemeByMode()
2023-09-02 19:06:02 +08:00
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', (event) => setTheme(event.matches))
2023-09-02 19:06:02 +08:00
}