mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-08 19:22:44 +08:00
280 lines
8.7 KiB
TypeScript
280 lines
8.7 KiB
TypeScript
import { RefreshRounded, StorageOutlined } from '@mui/icons-material'
|
|
import {
|
|
Box,
|
|
Button,
|
|
Dialog,
|
|
DialogActions,
|
|
DialogContent,
|
|
DialogTitle,
|
|
Divider,
|
|
IconButton,
|
|
List,
|
|
ListItem,
|
|
ListItemText,
|
|
Typography,
|
|
alpha,
|
|
styled,
|
|
} from '@mui/material'
|
|
import { useLockFn } from 'ahooks'
|
|
import dayjs from 'dayjs'
|
|
import { useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { updateRuleProvider } from 'tauri-plugin-mihomo-api'
|
|
|
|
import { useAppRefreshers, useRulesData } from '@/providers/app-data-context'
|
|
import { showNotice } from '@/services/notice-service'
|
|
|
|
const TypeBox = styled(Box)<{ component?: React.ElementType }>(({ theme }) => ({
|
|
display: 'inline-block',
|
|
border: '1px solid #ccc',
|
|
borderColor: alpha(theme.palette.secondary.main, 0.5),
|
|
color: alpha(theme.palette.secondary.main, 0.8),
|
|
borderRadius: 4,
|
|
fontSize: 10,
|
|
marginRight: '4px',
|
|
padding: '0 2px',
|
|
lineHeight: 1.25,
|
|
}))
|
|
|
|
export const ProviderButton = () => {
|
|
const { t } = useTranslation()
|
|
const [open, setOpen] = useState(false)
|
|
const { ruleProviders } = useRulesData()
|
|
const { refreshRules, refreshRuleProviders } = useAppRefreshers()
|
|
const [updating, setUpdating] = useState<Record<string, boolean>>({})
|
|
|
|
const hasProviders = Object.keys(ruleProviders || {}).length > 0
|
|
|
|
const updateProvider = useLockFn(async (name: string) => {
|
|
try {
|
|
setUpdating((prev) => ({ ...prev, [name]: true }))
|
|
|
|
await updateRuleProvider(name)
|
|
|
|
await refreshRules()
|
|
await refreshRuleProviders()
|
|
|
|
showNotice.success(
|
|
'rules.feedback.notifications.provider.updateSuccess',
|
|
{
|
|
name,
|
|
},
|
|
)
|
|
} catch (err) {
|
|
showNotice.error('rules.feedback.notifications.provider.updateFailed', {
|
|
name,
|
|
message: String(err),
|
|
})
|
|
} finally {
|
|
setUpdating((prev) => ({ ...prev, [name]: false }))
|
|
}
|
|
})
|
|
|
|
const updateAllProviders = useLockFn(async () => {
|
|
try {
|
|
const allProviders = Object.keys(ruleProviders || {})
|
|
if (allProviders.length === 0) {
|
|
showNotice.info('rules.feedback.notifications.provider.none')
|
|
return
|
|
}
|
|
|
|
const newUpdating = allProviders.reduce(
|
|
(acc, key) => {
|
|
acc[key] = true
|
|
return acc
|
|
},
|
|
{} as Record<string, boolean>,
|
|
)
|
|
setUpdating(newUpdating)
|
|
|
|
for (const name of allProviders) {
|
|
try {
|
|
await updateRuleProvider(name)
|
|
setUpdating((prev) => ({ ...prev, [name]: false }))
|
|
} catch (err) {
|
|
console.error(`更新 ${name} 失败`, err)
|
|
}
|
|
}
|
|
|
|
await refreshRules()
|
|
await refreshRuleProviders()
|
|
|
|
showNotice.success('rules.feedback.notifications.provider.allUpdated')
|
|
} catch (err) {
|
|
showNotice.error('rules.feedback.notifications.provider.genericError', {
|
|
message: String(err),
|
|
})
|
|
} finally {
|
|
setUpdating({})
|
|
}
|
|
})
|
|
|
|
const handleClose = () => {
|
|
setOpen(false)
|
|
}
|
|
|
|
if (!hasProviders) return null
|
|
|
|
return (
|
|
<>
|
|
<Button
|
|
variant="outlined"
|
|
size="small"
|
|
startIcon={<StorageOutlined />}
|
|
onClick={() => setOpen(true)}
|
|
>
|
|
{t('rules.page.provider.trigger')}
|
|
</Button>
|
|
|
|
<Dialog open={open} onClose={handleClose} maxWidth="sm" fullWidth>
|
|
<DialogTitle>
|
|
<Box
|
|
sx={{
|
|
display: 'flex',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<Typography variant="h6">
|
|
{t('rules.page.provider.dialogTitle')}
|
|
</Typography>
|
|
<Button
|
|
variant="contained"
|
|
size="small"
|
|
onClick={updateAllProviders}
|
|
>
|
|
{t('rules.page.provider.actions.updateAll')}
|
|
</Button>
|
|
</Box>
|
|
</DialogTitle>
|
|
|
|
<DialogContent>
|
|
<List sx={{ py: 0, minHeight: 250 }}>
|
|
{Object.entries(ruleProviders || {})
|
|
.sort()
|
|
.map(([key, item]) => {
|
|
const provider = item
|
|
const time = dayjs(provider.updatedAt)
|
|
const isUpdating = updating[key]
|
|
|
|
return (
|
|
<ListItem
|
|
key={key}
|
|
sx={[
|
|
{
|
|
p: 0,
|
|
mb: '8px',
|
|
borderRadius: 2,
|
|
overflow: 'hidden',
|
|
transition: 'all 0.2s',
|
|
},
|
|
({ palette: { mode, primary } }) => {
|
|
const bgcolor = mode === 'light' ? '#ffffff' : '#24252f'
|
|
const hoverColor =
|
|
mode === 'light'
|
|
? alpha(primary.main, 0.1)
|
|
: alpha(primary.main, 0.2)
|
|
|
|
return {
|
|
backgroundColor: bgcolor,
|
|
'&:hover': {
|
|
backgroundColor: hoverColor,
|
|
borderColor: alpha(primary.main, 0.3),
|
|
},
|
|
}
|
|
},
|
|
]}
|
|
>
|
|
<ListItemText
|
|
sx={{ px: 2, py: 1 }}
|
|
primary={
|
|
<Box
|
|
sx={{
|
|
display: 'flex',
|
|
justifyContent: 'space-between',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<Typography
|
|
variant="subtitle1"
|
|
component="div"
|
|
noWrap
|
|
title={key}
|
|
sx={{ display: 'flex', alignItems: 'center' }}
|
|
>
|
|
<span style={{ marginRight: '8px' }}>{key}</span>
|
|
<TypeBox component="span">
|
|
{provider.ruleCount}
|
|
</TypeBox>
|
|
</Typography>
|
|
|
|
<Typography
|
|
variant="body2"
|
|
color="text.secondary"
|
|
noWrap
|
|
>
|
|
<small>{t('shared.labels.updateAt')}: </small>
|
|
{time.fromNow()}
|
|
</Typography>
|
|
</Box>
|
|
}
|
|
secondary={
|
|
<Box sx={{ display: 'flex' }}>
|
|
<TypeBox component="span">
|
|
{typeof provider.vehicleType === 'string'
|
|
? provider.vehicleType
|
|
: provider.vehicleType.Unknown}
|
|
</TypeBox>
|
|
<TypeBox component="span">
|
|
{typeof provider.behavior === 'string'
|
|
? provider.behavior
|
|
: provider.behavior.Unknown}
|
|
</TypeBox>
|
|
</Box>
|
|
}
|
|
/>
|
|
<Divider orientation="vertical" flexItem />
|
|
<Box
|
|
sx={{
|
|
width: 40,
|
|
display: 'flex',
|
|
justifyContent: 'center',
|
|
alignItems: 'center',
|
|
}}
|
|
>
|
|
<IconButton
|
|
size="small"
|
|
color="primary"
|
|
onClick={() => updateProvider(key)}
|
|
disabled={isUpdating}
|
|
aria-label={t('rules.page.provider.actions.update')}
|
|
sx={{
|
|
animation: isUpdating
|
|
? 'spin 1s linear infinite'
|
|
: 'none',
|
|
'@keyframes spin': {
|
|
'0%': { transform: 'rotate(0deg)' },
|
|
'100%': { transform: 'rotate(360deg)' },
|
|
},
|
|
}}
|
|
title={t('rules.page.provider.actions.update')}
|
|
>
|
|
<RefreshRounded />
|
|
</IconButton>
|
|
</Box>
|
|
</ListItem>
|
|
)
|
|
})}
|
|
</List>
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
|
<Button onClick={handleClose} variant="outlined">
|
|
{t('shared.actions.close')}
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
</>
|
|
)
|
|
}
|