Skip to content

Commit

Permalink
feat: add Auto Check Updates Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
keiko233 committed Feb 29, 2024
1 parent 46d33a0 commit a17510a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
5 changes: 5 additions & 0 deletions backend/tauri/src/config/nyanpasu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub struct IVerge {

/// verge mixed port 用于覆盖 clash 的 mixed port
pub verge_mixed_port: Option<u16>,

/// Check update when app launch
pub disbale_auto_check_update: Option<bool>,
}

#[derive(Default, Debug, Clone, Deserialize, Serialize)]
Expand Down Expand Up @@ -216,6 +219,7 @@ impl IVerge {
page_transition_animation: Some("slide".into()),
// auto_log_clean: Some(60 * 24 * 7), // 7 days 自动清理日记
max_log_files: Some(7), // 7 days
disbale_auto_check_update: Some(true),
..Self::default()
}
}
Expand Down Expand Up @@ -243,6 +247,7 @@ impl IVerge {
patch!(traffic_graph);
patch!(enable_memory_usage);
patch!(page_transition_animation);
patch!(disbale_auto_check_update);

patch!(enable_tun_mode);
patch!(enable_service_mode);
Expand Down
19 changes: 14 additions & 5 deletions src/components/layout/update-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button } from "@mui/material";
import { checkUpdate } from "@tauri-apps/api/updater";
import { UpdateViewer } from "../setting/mods/update-viewer";
import { DialogRef } from "../base";
import { useVerge } from "@/hooks/use-verge";

interface Props {
className?: string;
Expand All @@ -14,11 +15,19 @@ export const UpdateButton = (props: Props) => {

const viewerRef = useRef<DialogRef>(null);

const { data: updateInfo } = useSWR("checkUpdate", checkUpdate, {
errorRetryCount: 2,
revalidateIfStale: false,
focusThrottleInterval: 36e5, // 1 hour
});
const { verge } = useVerge();

const { disbale_auto_check_update } = verge ?? {};

const { data: updateInfo } = useSWR(
disbale_auto_check_update ? null : "checkUpdate",
disbale_auto_check_update ? null : checkUpdate,
{
errorRetryCount: 2,
revalidateIfStale: false,
focusThrottleInterval: 36e5, // 1 hour
},
);

if (!updateInfo?.shouldUpdate) return null;

Expand Down
65 changes: 39 additions & 26 deletions src/components/setting/setting-verge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { TasksViewer } from "./mods/tasks-viewer";
import { ThemeModeSwitch } from "./mods/theme-mode-switch";
import { ThemeViewer } from "./mods/theme-viewer";
import { UpdateViewer } from "./mods/update-viewer";
import MDYSwitch from "../common/mdy-switch";

interface Props {
onError?: (err: Error) => void;
Expand All @@ -44,7 +45,7 @@ const SettingVerge = ({ onError }: Props) => {
const { t } = useTranslation();

const { verge, patchVerge } = useVerge();
const { theme_mode, language } = verge ?? {};
const { theme_mode, language, disbale_auto_check_update } = verge ?? {};

const [loading, setLoading] = useState({
theme_mode: false,
Expand Down Expand Up @@ -91,6 +92,8 @@ const SettingVerge = ({ onError }: Props) => {
}
});

const onSwitchFormat = (_e: any, value: boolean) => value;

return (
<SettingList title={t("Nyanpasu Setting")}>
<ThemeViewer ref={themeRef} />
Expand Down Expand Up @@ -246,32 +249,42 @@ const SettingVerge = ({ onError }: Props) => {
</SettingItem>

{!(OS === "windows" && WIN_PORTABLE) && (
<SettingItem
label={t("Check for Updates")}
extra={
tipChips.current.onCheckUpdate && (
<Chip
label={tipChips.current.onCheckUpdate}
size="small"
sx={{ mr: 1 }}
/>
)
}
>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={onCheckUpdate}
disabled={loading["onCheckUpdate"]}
<>
<SettingItem
label={t("Check for Updates")}
extra={
tipChips.current.onCheckUpdate && (
<Chip label={tipChips.current.onCheckUpdate} size="small" />
)
}
>
{loading["onCheckUpdate"] ? (
<CircularProgress color="inherit" size={24} />
) : (
<ArrowForward />
)}
</IconButton>
</SettingItem>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={onCheckUpdate}
disabled={loading["onCheckUpdate"]}
>
{loading["onCheckUpdate"] ? (
<CircularProgress color="inherit" size={24} />
) : (
<ArrowForward />
)}
</IconButton>
</SettingItem>

<SettingItem label={t("Auto Check Updates")}>
<GuardState
value={!disbale_auto_check_update}
valueProps="checked"
onFormat={onSwitchFormat}
onCatch={onError}
onGuard={(e) => patchVerge({ disbale_auto_check_update: !e })}
>
<MDYSwitch edge="end" />
</GuardState>
</SettingItem>
</>
)}

<SettingItem label={t("Nyanpasu Version")}>
Expand Down
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"Open App Dir": "Open App Dir",
"Open Core Dir": "Open Core Dir",
"Open Logs Dir": "Open Logs Dir",
"Auto Check Updates": "Auto Check Updates",
"Check for Updates": "Check for Updates",
"Nyanpasu Version": "Nyanpasu Version",
"theme.light": "Light",
Expand Down
1 change: 1 addition & 0 deletions src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"Open App Dir": "应用目录",
"Open Core Dir": "内核目录",
"Open Logs Dir": "日志目录",
"Auto Check Updates": "自动检查更新",
"Check for Updates": "检查更新",
"Nyanpasu Version": "Nyanpasu 版本",
"theme.light": "浅色",
Expand Down
1 change: 1 addition & 0 deletions src/services/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ interface IVergeConfig {
traffic_graph?: boolean;
enable_memory_usage?: boolean;
page_transition_animation?: keyof typeof import("@/components/layout/page-transition").pageTransitionVariants;
disbale_auto_check_update?: boolean;
enable_tun_mode?: boolean;
enable_auto_launch?: boolean;
enable_service_mode?: boolean;
Expand Down

0 comments on commit a17510a

Please sign in to comment.