-
Notifications
You must be signed in to change notification settings - Fork 620
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): new design ProvidersPage
- Loading branch information
Showing
6 changed files
with
180 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 55 additions & 87 deletions
142
frontend/nyanpasu/src/components/providers/rules-provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,80 @@ | ||
import { NotificationType, useNotification } from "@/hooks/use-notification"; | ||
import { updateRulesProviders, type ProviderRules } from "@/services/api"; | ||
import { useMessage } from "@/hooks/use-notification"; | ||
import { Refresh } from "@mui/icons-material"; | ||
import { | ||
Box, | ||
Card, | ||
CardContent, | ||
CircularProgress, | ||
IconButton, | ||
Stack, | ||
Typography, | ||
} from "@mui/material"; | ||
import LoadingButton from "@mui/lab/LoadingButton/LoadingButton"; | ||
import { Chip, Paper } from "@mui/material"; | ||
import { ProviderRules, useClashCore } from "@nyanpasu/interface"; | ||
import { useLockFn } from "ahooks"; | ||
import dayjs from "dayjs"; | ||
import { useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
export interface RulesProviderProps { | ||
provider: ProviderRules; | ||
onRulesProviderUpdated: () => void; | ||
} | ||
|
||
export default function RulesProvider(props: RulesProviderProps) { | ||
const { provider, onRulesProviderUpdated } = props; | ||
export default function RulesProvider({ provider }: RulesProviderProps) { | ||
const { t } = useTranslation(); | ||
|
||
const [updating, setUpdating] = useState(false); | ||
const onUpdate = useLockFn(async () => { | ||
setUpdating(true); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const { updateRulesProviders } = useClashCore(); | ||
|
||
const handleClick = useLockFn(async () => { | ||
try { | ||
setLoading(true); | ||
|
||
await updateRulesProviders(provider.name); | ||
onRulesProviderUpdated(); | ||
useNotification({ | ||
title: t("Success"), | ||
body: t("Update Rules Providers Success"), | ||
type: NotificationType.Success, | ||
}); | ||
} catch (err: any) { | ||
useNotification({ | ||
} catch (e) { | ||
useMessage(`Update ${provider.name} failed.\n${String(e)}`, { | ||
type: "error", | ||
title: t("Error"), | ||
body: err.message || err.toString(), | ||
type: NotificationType.Error, | ||
}); | ||
} finally { | ||
setUpdating(false); | ||
setLoading(false); | ||
} | ||
}); | ||
|
||
return ( | ||
<Card variant="outlined"> | ||
<CardContent | ||
style={{ | ||
position: "relative", | ||
}} | ||
> | ||
<IconButton | ||
onClick={onUpdate} | ||
disabled={updating} | ||
sx={{ | ||
position: "absolute", | ||
top: "50%", | ||
right: "0.5em", | ||
transform: "translateY(-50%)", | ||
}} | ||
size="medium" | ||
> | ||
{updating ? ( | ||
<CircularProgress size="1.11em" /> | ||
) : ( | ||
<Refresh | ||
style={{ | ||
fontSize: "1.11em", | ||
}} | ||
/> | ||
)} | ||
</IconButton> | ||
<Paper | ||
className="p-5 flex flex-col gap-2" | ||
sx={{ | ||
borderRadius: 6, | ||
}} | ||
> | ||
<div className="flex items-start justify-between gap-2"> | ||
<div className="ml-1"> | ||
<p className="text-lg font-bold truncate">{provider.name}</p> | ||
|
||
<p className="truncate text-sm"> | ||
{provider.vehicleType}/{provider.behavior} | ||
</p> | ||
</div> | ||
|
||
<Stack gap={1}> | ||
<Box display="flex" gap={1} alignItems="end"> | ||
<Typography | ||
variant="h6" | ||
component="div" | ||
sx={{ | ||
padding: 0, | ||
margin: 0, | ||
lineHeight: "inherit", | ||
}} | ||
> | ||
<b>{provider.name}</b> | ||
</Typography> | ||
<Typography variant="caption" color="GrayText"> | ||
{provider.vehicleType}/{provider.behavior} | ||
</Typography> | ||
</Box> | ||
<div className="text-sm text-right"> | ||
{t("Last Update", { | ||
fromNow: dayjs(provider.updatedAt).fromNow(), | ||
})} | ||
</div> | ||
</div> | ||
|
||
<Stack> | ||
<Typography variant="body1"> | ||
{t("Rule Set rules", { | ||
rule: provider.ruleCount, | ||
})} | ||
</Typography> | ||
<Typography variant="body2"> | ||
{t("Last Update", { | ||
fromNow: dayjs(provider.updatedAt).fromNow(), | ||
})} | ||
</Typography> | ||
</Stack> | ||
</Stack> | ||
</CardContent> | ||
</Card> | ||
<div className="flex items-center justify-between"> | ||
<Chip | ||
className="font-bold truncate" | ||
label={t("Rule Set rules", { | ||
rule: provider.ruleCount, | ||
})} | ||
/> | ||
|
||
<LoadingButton | ||
loading={loading} | ||
size="small" | ||
variant="contained" | ||
className="!size-8 !min-w-0" | ||
onClick={handleClick} | ||
> | ||
<Refresh /> | ||
</LoadingButton> | ||
</div> | ||
</Paper> | ||
); | ||
} |
58 changes: 58 additions & 0 deletions
58
frontend/nyanpasu/src/components/providers/update-providers.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useMessage } from "@/hooks/use-notification"; | ||
import LoadingButton from "@mui/lab/LoadingButton"; | ||
import { useClashCore } from "@nyanpasu/interface"; | ||
import { useLockFn } from "ahooks"; | ||
import { useState } from "react"; | ||
import { Refresh } from "@mui/icons-material"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
export const UpdateProviders = () => { | ||
const { t } = useTranslation(); | ||
|
||
const [loading, setLoading] = useState(false); | ||
|
||
const { getRulesProviders, updateRulesProviders } = useClashCore(); | ||
|
||
const handleProviderUpdate = useLockFn(async () => { | ||
if (!getRulesProviders.data) { | ||
useMessage(`No Providers.`, { | ||
type: "info", | ||
title: t("Info"), | ||
}); | ||
|
||
return; | ||
} | ||
|
||
try { | ||
setLoading(true); | ||
|
||
const providers = Object.entries(getRulesProviders.data).map( | ||
([name]) => name, | ||
); | ||
|
||
await Promise.all( | ||
providers.map((provider) => updateRulesProviders(provider)), | ||
); | ||
} catch (e) { | ||
useMessage(`Update all failed.\n${String(e)}`, { | ||
type: "error", | ||
title: t("Error"), | ||
}); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}); | ||
|
||
return ( | ||
<LoadingButton | ||
variant="contained" | ||
loading={loading} | ||
startIcon={<Refresh />} | ||
onClick={handleProviderUpdate} | ||
> | ||
{t("Update Rules Providers All")} | ||
</LoadingButton> | ||
); | ||
}; | ||
|
||
export default UpdateProviders; |
Oops, something went wrong.