metacubexd/src/signals/rules.ts

44 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-09-02 17:42:13 +08:00
import { createSignal } from 'solid-js'
import { useRequest } from '~/signals'
import type { Rule, RuleProvider } from '~/types'
export const useRules = () => {
2023-09-07 12:54:47 +08:00
const request = useRequest()
2023-09-02 17:42:13 +08:00
const [rules, setRules] = createSignal<Rule[]>([])
const [ruleProviders, setRuleProviders] = createSignal<RuleProvider[]>([])
2023-09-02 17:42:13 +08:00
const updateRules = async () => {
2023-09-05 15:38:42 +08:00
const [{ rules }, { providers }] = await Promise.all([
request.get('rules').json<{ rules: Record<string, Rule> }>(),
request
.get('providers/rules')
.json<{ providers: Record<string, RuleProvider> }>(),
])
2023-09-02 17:42:13 +08:00
setRules(Object.values(rules))
setRuleProviders(Object.values(providers))
2023-09-02 17:42:13 +08:00
}
const updateAllRuleProvider = async () => {
await Promise.all(
ruleProviders().map((provider) => {
2023-09-02 17:42:13 +08:00
return request.put(`providers/rules/${provider.name}`)
}),
)
await updateRules()
}
const updateRuleProviderByName = async (name: string) => {
await request.put(`providers/rules/${name}`)
await updateRules()
}
return {
rules,
ruleProviders,
2023-09-02 17:42:13 +08:00
updateRules,
updateAllRuleProvider,
updateRuleProviderByName,
}
}