2023-09-02 17:42:13 +08:00
|
|
|
import { createSignal } from 'solid-js'
|
2023-09-12 21:33:16 +08:00
|
|
|
import {
|
|
|
|
fetchRuleProvidersAPI,
|
|
|
|
fetchRulesAPI,
|
|
|
|
updateRuleProviderAPI,
|
|
|
|
} from '~/apis'
|
2023-09-02 17:42:13 +08:00
|
|
|
import type { Rule, RuleProvider } from '~/types'
|
|
|
|
|
|
|
|
export const useRules = () => {
|
|
|
|
const [rules, setRules] = createSignal<Rule[]>([])
|
2023-09-09 17:16:18 +08:00
|
|
|
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([
|
2023-09-12 21:33:16 +08:00
|
|
|
fetchRulesAPI(),
|
|
|
|
fetchRuleProvidersAPI(),
|
2023-09-05 15:38:42 +08:00
|
|
|
])
|
2023-09-02 17:42:13 +08:00
|
|
|
|
|
|
|
setRules(Object.values(rules))
|
2023-09-09 17:16:18 +08:00
|
|
|
setRuleProviders(Object.values(providers))
|
2023-09-02 17:42:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateAllRuleProvider = async () => {
|
|
|
|
await Promise.all(
|
2023-09-12 21:33:16 +08:00
|
|
|
ruleProviders().map((provider) => updateRuleProviderAPI(provider.name)),
|
2023-09-02 17:42:13 +08:00
|
|
|
)
|
|
|
|
await updateRules()
|
|
|
|
}
|
|
|
|
|
2023-09-12 21:33:16 +08:00
|
|
|
const updateRuleProviderByName = async (providerName: string) => {
|
|
|
|
await updateRuleProviderAPI(providerName)
|
2023-09-02 17:42:13 +08:00
|
|
|
await updateRules()
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
rules,
|
2023-09-09 17:16:18 +08:00
|
|
|
ruleProviders,
|
2023-09-02 17:42:13 +08:00
|
|
|
updateRules,
|
|
|
|
updateAllRuleProvider,
|
|
|
|
updateRuleProviderByName,
|
|
|
|
}
|
|
|
|
}
|