mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-21 11:42:09 +08:00
* feat(contract): export wire contract from backend and generate frontend types Adds a zero-dependency `frontend-contract` bin that serializes the real NoticeStatus variants and FrontendEvent payloads, scripts/gen-contract.mjs which emits the committed src/services/contract.ts, and a CI job that regenerates it and fails on a non-empty diff. events.ts now derives its event map from the generated contract instead of hand-typed shapes. * feat(events): route all backend events through a single event bus and store Adds src/services/bus (the only listen/emit site: one handler per contract event plus the frontend-internal test-all event) and the React context/reducer app store. Migrates the five scattered subscriptions (layout events, pending failures, proxy refresh, profile update/timer, test-all), deletes the old glue (events.ts, use-listen, use-layout-events, LoadingCacheProvider), moves run state into the store, and adds an architecture gate script wired into CI. Behavior is unchanged; the run-state query's poll/focus safety net is preserved by the store provider. * refactor(notice): make the notice table exhaustive and drop dead wire entries D5 cleanup, frontend and backend in one commit: removes the six config_validate::* statuses that never receive a backend send and the set_config::ok send (enum variants, golden list 41→34, send point in restart_clash_core), their frontend handlers, and the five i18n keys only those handlers used. The notice table is now typed as a Record over the generated NoticeStatus union, so a status without a frontend entry fails to compile. The info status (sent by the silent updater for breaking change notices, previously dropped with a console warning) now shows an info toast — the one deliberate behavior delta. * feat(mutate): single funnel for user-intent commands and invoke gate Adds src/services/mutate.ts: one path for optimistic apply, services-layer invoke, Busy handling, failure rollback, and the error toast, with busy bookkeeping mirrored into the app store. The patch hooks (verge, clash, profiles) now route through it. Moves the remaining direct invokes (unlock ×3, DNS ×4) into cmds.ts and turns on the invoke-only-in-services architecture gate in CI. * refactor(settings): route settings-domain commands through the mutate funnel Ports port saving, webdav/local backup operations, core change/restart/ upgrade, service install/uninstall/repair flows, the system-proxy toggle queue, the DNS override toggle, and app lifecycle commands onto the single funnel. Bespoke flows keep their own error handling via errorNotice:false; simple flows gain the funnel's default error toast and revalidation. * refactor(profiles): route profile operations through the mutate funnel Ports profile create/import/reorder/delete/update/patch/save-file and enhance calls onto the funnel; retry and editor flows keep their own error handling with errorNotice:false. Per-uid ids keep the update worker pool concurrent under the funnel's in-flight dedupe. * refactor(proxies): route proxy/home/misc commands through the mutate funnel Ports mode switching (page and home card), chain runtime updates, node selection with tray sync, selection recording, provider updates and syncs, and lightweight-mode entry onto the funnel; every site keeps its existing optimistic/revert and error-handling behavior. * docs: add frontend architecture guardrails for src Layering rules for the contract flow: generated contract as the only wire-typing source, event bus as the only listen/emit site, pure-reducer store for event-derived state, the mutate funnel for user intent, and invoke only in the services layer — each backed by the CI gates. * fix(events): close run-state race and restore single error toasts Self-review findings: the bus now re-reads the run-state snapshot in onSubscribed so an event racing the store's initial read cannot leave a stale snapshot (the ordering the old layout listener guaranteed), and the patch hooks pass errorNotice:false so GuardState/page handlers remain the only error toast, matching the pre-migration behavior. * fix(frontend): preserve queued intents and fresh runtime snapshots * refactor(frontend): trim unused mutation and busy state APIs * docs(frontend): focus guardrails on architecture ownership
405 lines
11 KiB
TypeScript
405 lines
11 KiB
TypeScript
import {
|
|
DnsOutlined,
|
|
HelpOutlineRounded,
|
|
HistoryEduOutlined,
|
|
RouterOutlined,
|
|
SettingsOutlined,
|
|
SpeedOutlined,
|
|
} from '@mui/icons-material'
|
|
import {
|
|
Box,
|
|
Button,
|
|
Checkbox,
|
|
Dialog,
|
|
DialogActions,
|
|
DialogContent,
|
|
DialogTitle,
|
|
FormControlLabel,
|
|
FormGroup,
|
|
Grid,
|
|
IconButton,
|
|
Skeleton,
|
|
Tooltip,
|
|
} from '@mui/material'
|
|
import { useLockFn } from 'ahooks'
|
|
import { Suspense, lazy, useCallback, useMemo, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { BasePage } from '@/components/base'
|
|
import { ClashModeCard } from '@/components/home/clash-mode-card'
|
|
import { CurrentProxyCard } from '@/components/home/current-proxy-card'
|
|
import { EnhancedCard } from '@/components/home/enhanced-card'
|
|
import { EnhancedTrafficStats } from '@/components/home/enhanced-traffic-stats'
|
|
import { HomeProfileCard } from '@/components/home/home-profile-card'
|
|
import { ProxyTunCard } from '@/components/home/proxy-tun-card'
|
|
import { useProfiles } from '@/hooks/use-profiles'
|
|
import { useVerge } from '@/hooks/use-verge'
|
|
import { entry_lightweight_mode } from '@/services/cmds'
|
|
import { mutate } from '@/services/mutate'
|
|
import { showNotice } from '@/services/notice-service'
|
|
import { openExternalUrl } from '@/utils/open-external-url'
|
|
|
|
const preloadTestCard = () =>
|
|
import('@/components/home/test-card').then((module) => ({
|
|
default: module.TestCard,
|
|
}))
|
|
const preloadIpInfoCard = () =>
|
|
import('@/components/home/ip-info-card').then((module) => ({
|
|
default: module.IpInfoCard,
|
|
}))
|
|
const preloadClashInfoCard = () =>
|
|
import('@/components/home/clash-info-card').then((module) => ({
|
|
default: module.ClashInfoCard,
|
|
}))
|
|
const preloadSystemInfoCard = () =>
|
|
import('@/components/home/system-info-card').then((module) => ({
|
|
default: module.SystemInfoCard,
|
|
}))
|
|
|
|
const LazyTestCard = lazy(preloadTestCard)
|
|
const LazyIpInfoCard = lazy(preloadIpInfoCard)
|
|
const LazyClashInfoCard = lazy(preloadClashInfoCard)
|
|
const LazySystemInfoCard = lazy(preloadSystemInfoCard)
|
|
|
|
// Used by bootstrap to initiate optional card imports without blocking render.
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
|
export const preloadHomePageCards = () =>
|
|
Promise.all([
|
|
preloadTestCard().catch(() => {}),
|
|
preloadIpInfoCard().catch(() => {}),
|
|
preloadClashInfoCard().catch(() => {}),
|
|
preloadSystemInfoCard().catch(() => {}),
|
|
])
|
|
|
|
// 定义首页卡片设置接口
|
|
interface HomeCardsSettings {
|
|
profile: boolean
|
|
proxy: boolean
|
|
network: boolean
|
|
mode: boolean
|
|
traffic: boolean
|
|
info: boolean
|
|
clashinfo: boolean
|
|
systeminfo: boolean
|
|
test: boolean
|
|
ip: boolean
|
|
[key: string]: boolean
|
|
}
|
|
|
|
const DEFAULT_HOME_CARDS: HomeCardsSettings = {
|
|
info: false,
|
|
profile: true,
|
|
proxy: true,
|
|
network: true,
|
|
mode: true,
|
|
traffic: true,
|
|
clashinfo: true,
|
|
systeminfo: true,
|
|
test: true,
|
|
ip: true,
|
|
}
|
|
|
|
const serializeCardFlags = (cards: HomeCardsSettings) =>
|
|
Object.keys(cards)
|
|
.sort()
|
|
.map((key) => `${key}:${cards[key] ? 1 : 0}`)
|
|
.join('|')
|
|
|
|
// 首页设置对话框组件接口
|
|
interface HomeSettingsDialogProps {
|
|
onClose: () => void
|
|
homeCards: HomeCardsSettings
|
|
}
|
|
|
|
// 首页设置对话框组件
|
|
const HomeSettingsDialog = ({
|
|
onClose,
|
|
homeCards,
|
|
}: HomeSettingsDialogProps) => {
|
|
const { t } = useTranslation()
|
|
const [cards, setCards] = useState<HomeCardsSettings>(homeCards)
|
|
const { patchVerge } = useVerge()
|
|
|
|
const handleToggle = (key: string) => {
|
|
setCards((prev: HomeCardsSettings) => ({
|
|
...prev,
|
|
[key]: !prev[key],
|
|
}))
|
|
}
|
|
|
|
const handleSave = async () => {
|
|
await patchVerge({ home_cards: cards })
|
|
onClose()
|
|
}
|
|
|
|
return (
|
|
<Dialog open onClose={onClose} maxWidth="xs" fullWidth>
|
|
<DialogTitle>{t('home.page.settings.title')}</DialogTitle>
|
|
<DialogContent>
|
|
<FormGroup>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.profile || false}
|
|
onChange={() => handleToggle('profile')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.profile')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.proxy || false}
|
|
onChange={() => handleToggle('proxy')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.currentProxy')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.network || false}
|
|
onChange={() => handleToggle('network')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.network')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.mode || false}
|
|
onChange={() => handleToggle('mode')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.proxyMode')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.traffic || false}
|
|
onChange={() => handleToggle('traffic')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.traffic')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.test || false}
|
|
onChange={() => handleToggle('test')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.tests')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.ip || false}
|
|
onChange={() => handleToggle('ip')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.ip')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.clashinfo || false}
|
|
onChange={() => handleToggle('clashinfo')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.clashInfo')}
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Checkbox
|
|
checked={cards.systeminfo || false}
|
|
onChange={() => handleToggle('systeminfo')}
|
|
/>
|
|
}
|
|
label={t('home.page.settings.cards.systemInfo')}
|
|
/>
|
|
</FormGroup>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={onClose}>{t('shared.actions.cancel')}</Button>
|
|
<Button onClick={handleSave} color="primary">
|
|
{t('shared.actions.save')}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
const HomePage = () => {
|
|
const { t } = useTranslation()
|
|
const { verge } = useVerge()
|
|
const { current, mutateProfiles } = useProfiles()
|
|
|
|
// 设置弹窗的状态
|
|
const [settingsOpen, setSettingsOpen] = useState(false)
|
|
|
|
// 卡片显示状态
|
|
const homeCards =
|
|
(verge?.home_cards as HomeCardsSettings | undefined) ?? DEFAULT_HOME_CARDS
|
|
|
|
// 文档链接函数
|
|
const toGithubDoc = useLockFn(() =>
|
|
openExternalUrl('https://clash-verge-rev.github.io/index.html').catch(
|
|
showNotice.error,
|
|
),
|
|
)
|
|
|
|
// 新增:打开设置弹窗
|
|
const openSettings = useCallback(() => {
|
|
setSettingsOpen(true)
|
|
}, [])
|
|
|
|
const renderCard = useCallback(
|
|
(cardKey: string, component: React.ReactNode, size: number = 6) => {
|
|
if (!homeCards[cardKey]) return null
|
|
|
|
return (
|
|
<Grid size={size} key={cardKey}>
|
|
{component}
|
|
</Grid>
|
|
)
|
|
},
|
|
[homeCards],
|
|
)
|
|
|
|
const criticalCards = useMemo(
|
|
() => [
|
|
renderCard(
|
|
'profile',
|
|
<HomeProfileCard current={current} onProfileUpdated={mutateProfiles} />,
|
|
),
|
|
renderCard('proxy', <CurrentProxyCard />),
|
|
renderCard('network', <NetworkSettingsCard />),
|
|
renderCard('mode', <ClashModeEnhancedCard />),
|
|
],
|
|
[current, mutateProfiles, renderCard],
|
|
)
|
|
|
|
const nonCriticalCards = useMemo(
|
|
() => [
|
|
renderCard(
|
|
'traffic',
|
|
<EnhancedCard
|
|
title={t('home.page.cards.trafficStats')}
|
|
icon={<SpeedOutlined />}
|
|
iconColor="secondary"
|
|
>
|
|
<EnhancedTrafficStats />
|
|
</EnhancedCard>,
|
|
12,
|
|
),
|
|
renderCard(
|
|
'test',
|
|
<Suspense fallback={<Skeleton variant="rectangular" height={200} />}>
|
|
<LazyTestCard />
|
|
</Suspense>,
|
|
),
|
|
renderCard(
|
|
'ip',
|
|
<Suspense fallback={<Skeleton variant="rectangular" height={200} />}>
|
|
<LazyIpInfoCard />
|
|
</Suspense>,
|
|
),
|
|
renderCard(
|
|
'clashinfo',
|
|
<Suspense fallback={<Skeleton variant="rectangular" height={200} />}>
|
|
<LazyClashInfoCard />
|
|
</Suspense>,
|
|
),
|
|
renderCard(
|
|
'systeminfo',
|
|
<Suspense fallback={<Skeleton variant="rectangular" height={200} />}>
|
|
<LazySystemInfoCard />
|
|
</Suspense>,
|
|
),
|
|
],
|
|
[t, renderCard],
|
|
)
|
|
return (
|
|
<BasePage
|
|
title={t('home.page.title')}
|
|
contentStyle={{ padding: 2 }}
|
|
header={
|
|
<Box sx={{ display: 'flex', alignItems: 'center' }}>
|
|
<Tooltip title={t('home.page.tooltips.lightweightMode')} arrow>
|
|
<IconButton
|
|
onClick={async () =>
|
|
await mutate(() => entry_lightweight_mode(), {
|
|
id: 'entry-lightweight-mode',
|
|
errorNotice: false,
|
|
})
|
|
}
|
|
size="small"
|
|
color="inherit"
|
|
>
|
|
<HistoryEduOutlined />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title={t('home.page.tooltips.manual')} arrow>
|
|
<IconButton onClick={toGithubDoc} size="small" color="inherit">
|
|
<HelpOutlineRounded />
|
|
</IconButton>
|
|
</Tooltip>
|
|
<Tooltip title={t('home.page.tooltips.settings')} arrow>
|
|
<IconButton onClick={openSettings} size="small" color="inherit">
|
|
<SettingsOutlined />
|
|
</IconButton>
|
|
</Tooltip>
|
|
</Box>
|
|
}
|
|
>
|
|
<Grid container spacing={1.5} columns={{ xs: 6, sm: 6, md: 12 }}>
|
|
{criticalCards}
|
|
|
|
{nonCriticalCards}
|
|
</Grid>
|
|
|
|
{/* 首页设置弹窗 */}
|
|
{settingsOpen && (
|
|
<HomeSettingsDialog
|
|
key={serializeCardFlags(homeCards)}
|
|
onClose={() => setSettingsOpen(false)}
|
|
homeCards={homeCards}
|
|
/>
|
|
)}
|
|
</BasePage>
|
|
)
|
|
}
|
|
|
|
// 增强版网络设置卡片组件
|
|
const NetworkSettingsCard = () => {
|
|
const { t } = useTranslation()
|
|
return (
|
|
<EnhancedCard
|
|
title={t('home.page.cards.networkSettings')}
|
|
icon={<DnsOutlined />}
|
|
iconColor="primary"
|
|
action={null}
|
|
>
|
|
<ProxyTunCard />
|
|
</EnhancedCard>
|
|
)
|
|
}
|
|
|
|
// 增强版 Clash 模式卡片组件
|
|
const ClashModeEnhancedCard = () => {
|
|
const { t } = useTranslation()
|
|
return (
|
|
<EnhancedCard
|
|
title={t('home.page.cards.proxyMode')}
|
|
icon={<RouterOutlined />}
|
|
iconColor="info"
|
|
action={null}
|
|
>
|
|
<ClashModeCard />
|
|
</EnhancedCard>
|
|
)
|
|
}
|
|
|
|
export default HomePage
|