Skip to content

Commit

Permalink
feat: rules provider pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Zephyruso authored and Zephyruso committed Sep 2, 2023
1 parent d7fb0b4 commit 65346ca
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/pages/Proxies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export default () => {

return (
<div class="flex flex-col gap-2">
<h1 class="pb-2 text-lg font-semibold">{t('proxies')}</h1>
<h1 class="flex h-8 items-center pb-2 text-lg font-semibold">
{t('proxies')}
</h1>
<ForTwoLine
subChild={proxies().map((proxy) => {
const title = (
Expand Down
4 changes: 2 additions & 2 deletions src/pages/ProxyProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default () => {

return (
<div class="flex flex-col gap-2">
<h1 class="pm-4 flex items-center text-lg font-semibold">
<h1 class="flex h-8 items-center pb-2 text-lg font-semibold">
{t('proxyProviders')}
<button
class="btn btn-circle btn-ghost btn-sm ml-2"
Expand All @@ -64,7 +64,7 @@ export default () => {
subChild={proxyProviders().map((proxyProvider) => {
const title = (
<>
<div class="mr-10 flex items-center justify-between">
<div class="mr-8 flex items-center justify-between">
<span>{proxyProvider.name}</span>
<div>
<button
Expand Down
106 changes: 68 additions & 38 deletions src/pages/Rules.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,92 @@
import { useI18n } from '@solid-primitives/i18n'
import { IconReload } from '@tabler/icons-solidjs'
import InfiniteScroll from 'solid-infinite-scroll'
import { For, createMemo, createSignal, onMount } from 'solid-js'
import { useRequest } from '~/signals'
import type { Rule, RuleProvider } from '~/types'
import { For, Show, createMemo, createSignal, onMount } from 'solid-js'
import { useRules } from '~/signals/rules'

export default () => {
const [t] = useI18n()
const request = useRequest()
const {
rules,
rulesProviders,
updateRules,
updateAllRuleProvider,
updateRuleProviderByName,
} = useRules()
const [maxRuleRender, setMaxRuleRender] = createSignal(100)
const [rules, setRules] = createSignal<Rule[]>([])
const [rulesProviders, setRulesProviders] = createSignal<RuleProvider[]>([])
const renderRules = createMemo(() => rules().slice(0, maxRuleRender()))

onMount(async () => {
const { rules } = await request
.get('rules')
.json<{ rules: Record<string, Rule> }>()
updateRules()
})

setRules(Object.values(rules))
const onUpdateProviderClick = async (e: MouseEvent, name: string) => {
const el = e.target as HTMLElement

const { providers } = await request
.get('providers/rules')
.json<{ providers: Record<string, RuleProvider> }>()
el.classList.add('animate-spin')
e.stopPropagation()
await updateRuleProviderByName(name)
el.classList.remove('animate-spin')
}

setRulesProviders(Object.values(providers))
})
const onUpdateAllProviderClick = async (e: MouseEvent) => {
const el = e.target as HTMLElement

el.classList.add('animate-spin')
e.stopPropagation()
await updateAllRuleProvider()
el.classList.remove('animate-spin')
}

return (
<div class="flex flex-col gap-4">
<div>
<div class="flex w-full flex-row gap-4">
<div class="flex-1">
<h1 class="pb-4 text-lg font-semibold">{t('rules')}</h1>

<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
<InfiniteScroll
each={renderRules()}
hasMore={renderRules().length < rules().length}
next={() => setMaxRuleRender(maxRuleRender() + 100)}
>
{(rule) => (
<div class="card card-bordered card-compact bg-base-200 p-4">
<div class="break-all">{rule.payload}</div>
<div class="text-xs text-slate-500">
{rule.type} :: {rule.proxy}
</div>
<InfiniteScroll
each={renderRules()}
hasMore={renderRules().length < rules().length}
next={() => setMaxRuleRender(maxRuleRender() + 100)}
>
{(rule) => (
<div class="card card-bordered card-compact mb-2 bg-base-200 p-4">
<div class="break-all">{rule.payload}</div>
<div class="text-xs text-slate-500">
{rule.type} :: {rule.proxy}
</div>
)}
</InfiniteScroll>
</div>
</div>
)}
</InfiniteScroll>
</div>

<div>
<h1 class="pb-4 text-lg font-semibold">{t('ruleProviders')}</h1>
<Show when={rulesProviders().length > 0}>
<div class="flex-1">
<h1 class="flex h-11 items-center pb-4 text-lg font-semibold">
{t('ruleProviders')}
<button
class="btn btn-circle btn-ghost btn-sm ml-2"
onClick={(e) => onUpdateAllProviderClick(e)}
>
<IconReload />
</button>
</h1>

<div class="grid grid-cols-1 gap-2 sm:grid-cols-1">
<For each={rulesProviders()}>
{(rulesProvider) => (
<div class="card card-bordered card-compact bg-base-200 p-4">
<div>{rulesProvider.name}</div>
<div class="card card-bordered card-compact mb-2 bg-base-200 p-4">
<div class="flex h-6 items-center justify-between">
<span>{rulesProvider.name}</span>
<div>
<button
class="btn btn-circle btn-sm mr-2"
onClick={(e) =>
onUpdateProviderClick(e, rulesProvider.name)
}
>
<IconReload />
</button>
</div>
</div>
<div class="text-xs text-slate-500">
{rulesProvider.vehicleType} :: {rulesProvider.behavior} (
{rulesProvider.ruleCount})
Expand All @@ -65,7 +95,7 @@ export default () => {
)}
</For>
</div>
</div>
</Show>
</div>
)
}
2 changes: 1 addition & 1 deletion src/signals/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function useProxies() {
proxyGroup.now = proxyName

setProxies(proxyGroupList)
queueMicrotask(updateProxy)
// queueMicrotask(updateProxy)
}

const delayTestByProxyGroupName = async (proxyGroupName: string) => {
Expand Down
46 changes: 46 additions & 0 deletions src/signals/rules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createSignal } from 'solid-js'
import { useRequest } from '~/signals'
import type { Rule, RuleProvider } from '~/types'

const request = useRequest()

export const useRules = () => {
const [rules, setRules] = createSignal<Rule[]>([])
const [rulesProviders, setRulesProviders] = createSignal<RuleProvider[]>([])

const updateRules = async () => {
const { rules } = await request
.get('rules')
.json<{ rules: Record<string, Rule> }>()

setRules(Object.values(rules))

const { providers } = await request
.get('providers/rules')
.json<{ providers: Record<string, RuleProvider> }>()

setRulesProviders(Object.values(providers))
}

const updateAllRuleProvider = async () => {
await Promise.all(
rulesProviders().map((provider) => {
return request.put(`providers/rules/${provider.name}`)
}),
)
await updateRules()
}

const updateRuleProviderByName = async (name: string) => {
await request.put(`providers/rules/${name}`)
await updateRules()
}

return {
rules,
rulesProviders,
updateRules,
updateAllRuleProvider,
updateRuleProviderByName,
}
}

0 comments on commit 65346ca

Please sign in to comment.