Skip to content

Commit

Permalink
feat(rules): tab style for rules page #163 (#187)
Browse files Browse the repository at this point in the history
Co-authored-by: nb5p <[email protected]>
Co-authored-by: kunish <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2023
1 parent 8fa51e5 commit a9aa8e5
Show file tree
Hide file tree
Showing 8 changed files with 3,903 additions and 1,726 deletions.
573 changes: 207 additions & 366 deletions CHANGELOG.md

Large diffs are not rendered by default.

4,868 changes: 3,593 additions & 1,275 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
autoSwitchTheme: 'Automatically switch theme',
favDayTheme: 'Favorite light theme',
favNightTheme: 'Favorite dark theme',
renderInTwoColumns: 'Render proxies in two columns',
renderInTwoColumns: 'Render in two columns',
updateGEODatabases: 'Update GEO Databases',
restartCore: 'Restart Core',
upgradeCore: 'Upgrade Core',
Expand All @@ -60,8 +60,6 @@ export default {
ms: 'ms',
updated: 'Updated',
renderProxiesInSamePage: 'Render proxies and proxy provider in same page',
renderRulesAndProviderInTwoColumns:
'Render rules and rule provider in two columns',
tableSize: 'Table size',
xs: 'Extra small size',
sm: 'Small size',
Expand Down
3 changes: 1 addition & 2 deletions src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
autoSwitchTheme: '自动切换主题',
favDayTheme: '浅色主题偏好',
favNightTheme: '深色主题偏好',
renderInTwoColumns: '节点双列渲染',
renderInTwoColumns: '双列渲染',
updateGEODatabases: '更新 GEO 数据库文件',
restartCore: '重启核心',
upgradeCore: '更新核心',
Expand All @@ -60,7 +60,6 @@ export default {
ms: '毫秒',
updated: '更新于',
renderProxiesInSamePage: '将代理和代理提供者显示在同一页',
renderRulesAndProviderInTwoColumns: '规则和规则提供者双列渲染',
tableSize: '表格大小',
xs: '超小尺寸',
sm: '小尺寸',
Expand Down
7 changes: 0 additions & 7 deletions src/pages/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
proxiesOrderingType,
proxiesPreviewType,
renderInTwoColumns,
renderRulesAndProviderInTwoColumns,
setAutoCloseConns,
setAutoSwitchTheme,
setFavDayTheme,
Expand All @@ -39,7 +38,6 @@ import {
setProxiesOrderingType,
setProxiesPreviewType,
setRenderInTwoColumns,
setRenderRulesAndProviderInTwoColumns,
setSelectedEndpoint,
setTableSize,
setTwemoji,
Expand Down Expand Up @@ -319,11 +317,6 @@ const ConfigForXd = () => {
value: renderInTwoColumns,
onChange: setRenderInTwoColumns,
},
{
label: 'renderRulesAndProviderInTwoColumns',
value: renderRulesAndProviderInTwoColumns,
onChange: setRenderRulesAndProviderInTwoColumns,
},
{
label: 'autoSwitchTheme',
value: autoSwitchTheme,
Expand Down
159 changes: 97 additions & 62 deletions src/pages/Rules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ import { IconReload } from '@tabler/icons-solidjs'
import InfiniteScroll from 'solid-infinite-scroll'
import { For, Show, createMemo, createSignal, onMount } from 'solid-js'
import { twMerge } from 'tailwind-merge'
import { Button } from '~/components'
import { Button, ForTwoColumns } from '~/components'
import { formatTimeFromNow, useStringBooleanMap } from '~/helpers'
import { renderRulesAndProviderInTwoColumns, useRules } from '~/signals'
import { useRules } from '~/signals'

enum ActiveTab {
ruleProviders = 'ruleProviders',
rules = 'rules',
}

export default () => {
const [t] = useI18n()
const {
rules,
rulesProviders,
ruleProviders,
updateRules,
updateAllRuleProvider,
updateRuleProviderByName,
Expand Down Expand Up @@ -39,43 +44,45 @@ export default () => {
setAllProviderIsUpdating(false)
}

return (
<div
class={twMerge(
'flex w-full flex-col gap-4',
renderRulesAndProviderInTwoColumns() && 'flex-row',
)}
>
<div class="flex-1">
<h1 class="pb-4 text-lg font-semibold">{t('rules')}</h1>
const [activeTab, setActiveTab] = createSignal(ActiveTab.rules)

<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="flex items-center gap-2">
<span class="break-all">{rule.payload}</span>
<Show when={typeof rule.size === 'number' && rule.size !== -1}>
<div class="badge badge-sm">{rule.size}</div>
</Show>
</div>
<div class="text-xs text-slate-500">
{rule.type} :: {rule.proxy}
</div>
</div>
)}
</InfiniteScroll>
</div>
const tabs = () => [
{
type: ActiveTab.rules,
name: t('rules'),
count: rules().length,
},
{
type: ActiveTab.ruleProviders,
name: t('ruleProviders'),
count: ruleProviders().length,
},
]

<Show when={rulesProviders().length > 0}>
<div class="flex-1">
<h1 class="flex items-center gap-2 pb-4 text-lg font-semibold">
{t('ruleProviders')}
return (
<div class="flex h-full flex-col gap-2">
<Show when={ruleProviders().length > 0}>
<div class="flex items-center gap-2">
<div class="tabs-boxed tabs gap-2">
<For each={tabs()}>
{(tab) => (
<button
class={twMerge(
activeTab() === tab.type && 'tab-active',
'tab gap-2 px-2',
)}
onClick={() => setActiveTab(tab.type)}
>
<span>{tab.name}</span>
<div class="badge badge-sm">{tab.count}</div>
</button>
)}
</For>
</div>

<Show when={activeTab() === ActiveTab.ruleProviders}>
<Button
class="btn-circle btn-sm"
class="btn btn-circle btn-sm"
disabled={allProviderIsUpdating()}
onClick={(e) => onUpdateAllProviderClick(e)}
>
Expand All @@ -85,38 +92,66 @@ export default () => {
)}
/>
</Button>
</h1>
</Show>
</div>
</Show>

<For each={rulesProviders()}>
{(rulesProvider) => (
<div class="card card-bordered card-compact mb-2 bg-base-200 p-4">
<div class="flex items-center gap-2 pr-8">
<span class="break-all">{rulesProvider.name}</span>
<div class="badge badge-sm">{rulesProvider.ruleCount}</div>
<div class="flex-1 overflow-y-auto">
<Show when={activeTab() === ActiveTab.ruleProviders}>
<ForTwoColumns
subChild={ruleProviders().map((ruleProvider) => {
return (
<div class="card card-bordered card-compact mb-2 bg-base-200 p-4">
<div class="flex items-center gap-2 pr-8">
<span class="break-all">{ruleProvider.name}</span>
<div class="badge badge-sm">{ruleProvider.ruleCount}</div>
</div>
<div class="text-xs text-slate-500">
{ruleProvider.vehicleType} / {ruleProvider.behavior} /
{t('updated')} {formatTimeFromNow(ruleProvider.updatedAt)}
</div>
<Button
class="btn-circle btn-sm absolute right-2 top-2 mr-2 h-4"
disabled={updatingMap()[ruleProvider.name]}
onClick={(e) => onUpdateProviderClick(e, ruleProvider.name)}
>
<IconReload
class={twMerge(
updatingMap()[ruleProvider.name] &&
'animate-spin text-success',
)}
/>
</Button>
</div>
)
})}
/>
</Show>

<Show when={activeTab() === ActiveTab.rules}>
<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="flex items-center gap-2">
<span class="break-all">{rule.payload}</span>
<Show
when={typeof rule.size === 'number' && rule.size !== -1}
>
<div class="badge badge-sm">{rule.size}</div>
</Show>
</div>
<div class="text-xs text-slate-500">
{rulesProvider.vehicleType} / {rulesProvider.behavior} /
{t('updated')} {formatTimeFromNow(rulesProvider.updatedAt)}
{rule.type} :: {rule.proxy}
</div>

<Button
class="btn-circle btn-sm absolute right-2 top-2 mr-2 h-4"
disabled={updatingMap()[rulesProvider.name]}
onClick={(e) => onUpdateProviderClick(e, rulesProvider.name)}
>
<IconReload
class={twMerge(
updatingMap()[rulesProvider.name] &&
'animate-spin text-success',
)}
/>
</Button>
</div>
)}
</For>
</div>
</Show>
</InfiniteScroll>
</Show>
</div>
</div>
)
}
7 changes: 0 additions & 7 deletions src/signals/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ export const [renderInTwoColumns, setRenderInTwoColumns] = makePersisted(
createSignal(true),
{ name: 'renderInTwoColumn', storage: localStorage },
)
export const [
renderRulesAndProviderInTwoColumns,
setRenderRulesAndProviderInTwoColumns,
] = makePersisted(createSignal(true), {
name: 'renderRulesAndProviderInTwoColumns',
storage: localStorage,
})
export const [tableSize, setTableSize] = makePersisted(
createSignal<TAILWINDCSS_SIZE>(TAILWINDCSS_SIZE.XS),
{ name: 'tableSize', storage: localStorage },
Expand Down
8 changes: 4 additions & 4 deletions src/signals/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Rule, RuleProvider } from '~/types'
export const useRules = () => {
const request = useRequest()
const [rules, setRules] = createSignal<Rule[]>([])
const [rulesProviders, setRulesProviders] = createSignal<RuleProvider[]>([])
const [ruleProviders, setRuleProviders] = createSignal<RuleProvider[]>([])

const updateRules = async () => {
const [{ rules }, { providers }] = await Promise.all([
Expand All @@ -16,12 +16,12 @@ export const useRules = () => {
])

setRules(Object.values(rules))
setRulesProviders(Object.values(providers))
setRuleProviders(Object.values(providers))
}

const updateAllRuleProvider = async () => {
await Promise.all(
rulesProviders().map((provider) => {
ruleProviders().map((provider) => {
return request.put(`providers/rules/${provider.name}`)
}),
)
Expand All @@ -35,7 +35,7 @@ export const useRules = () => {

return {
rules,
rulesProviders,
ruleProviders,
updateRules,
updateAllRuleProvider,
updateRuleProviderByName,
Expand Down

0 comments on commit a9aa8e5

Please sign in to comment.