Skip to content

Commit

Permalink
fix: notification fallback (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Jan 14, 2024
1 parent 2e8e881 commit ce0224d
Show file tree
Hide file tree
Showing 26 changed files with 373 additions and 123 deletions.
8 changes: 6 additions & 2 deletions src/components/profile/editor-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import { useRecoilValue } from "recoil";

import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { monaco } from "@/services/monaco";

interface Props {
Expand Down Expand Up @@ -66,7 +66,11 @@ export const EditorViewer = (props: Props) => {
onChange?.();
onClose();
} catch (err: any) {
useNotification(t("Error"), err.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand Down
54 changes: 32 additions & 22 deletions src/components/profile/profile-item.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import dayjs from "dayjs";
import { mutate } from "swr";
import { useEffect, useState } from "react";
import { useLockFn } from "ahooks";
import { useRecoilState } from "recoil";
import { useTranslation } from "react-i18next";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { deleteProfile, updateProfile, viewProfile } from "@/services/cmds";
import { atomLoadingCache } from "@/services/states";
import parseTraffic from "@/utils/parse-traffic";
import { useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";
import { DragIndicator, RefreshRounded } from "@mui/icons-material";
import {
Box,
Typography,
LinearProgress,
CircularProgress,
IconButton,
keyframes,
MenuItem,
LinearProgress,
Menu,
CircularProgress,
MenuItem,
Typography,
keyframes,
} from "@mui/material";
import { RefreshRounded, DragIndicator } from "@mui/icons-material";
import { atomLoadingCache } from "@/services/states";
import { updateProfile, deleteProfile, viewProfile } from "@/services/cmds";
import { useLockFn } from "ahooks";
import dayjs from "dayjs";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useRecoilState } from "recoil";
import { mutate } from "swr";
import { EditorViewer } from "./editor-viewer";
import { ProfileBox } from "./profile-box";
import parseTraffic from "@/utils/parse-traffic";
import { useNotification } from "@/hooks/use-notification";

const round = keyframes`
from { transform: rotate(0deg); }
Expand Down Expand Up @@ -113,7 +113,11 @@ export const ProfileItem = (props: Props) => {
try {
await viewProfile(itemData.uid);
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand Down Expand Up @@ -146,11 +150,13 @@ export const ProfileItem = (props: Props) => {
mutate("getProfiles");
} catch (err: any) {
const errmsg = err?.message || err.toString();
useNotification(
t("Error"),
err.message ||
useNotification({
title: t("Error"),
body:
err.message ||
errmsg.replace(/error sending request for url (\S+?): /, ""),
);
type: NotificationType.Error,
});
} finally {
setLoadingCache((cache) => ({ ...cache, [itemData.uid]: false }));
}
Expand All @@ -162,7 +168,11 @@ export const ProfileItem = (props: Props) => {
await deleteProfile(itemData.uid);
mutate("getProfiles");
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand Down
30 changes: 17 additions & 13 deletions src/components/profile/profile-more.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import dayjs from "dayjs";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useLockFn } from "ahooks";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { viewProfile } from "@/services/cmds";
import { FeaturedPlayListRounded } from "@mui/icons-material";
import {
Box,
Badge,
Box,
Chip,
Typography,
MenuItem,
Menu,
IconButton,
Menu,
MenuItem,
Typography,
} from "@mui/material";
import { FeaturedPlayListRounded } from "@mui/icons-material";
import { viewProfile } from "@/services/cmds";
import { useLockFn } from "ahooks";
import dayjs from "dayjs";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { EditorViewer } from "./editor-viewer";
import { ProfileBox } from "./profile-box";
import { LogViewer } from "./log-viewer";
import { useNotification } from "@/hooks/use-notification";
import { ProfileBox } from "./profile-box";

interface Props {
selected: boolean;
Expand Down Expand Up @@ -68,7 +68,11 @@ export const ProfileMore = (props: Props) => {
try {
await viewProfile(itemData.uid);
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand Down
8 changes: 6 additions & 2 deletions src/components/profile/profile-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseDialog } from "@/components/base";
import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { createProfile, patchProfile } from "@/services/cmds";
import {
Box,
Expand Down Expand Up @@ -119,7 +119,11 @@ export const ProfileViewer = forwardRef<ProfileViewerRef, Props>(
fileDataRef.current = null;
props.onChange();
} catch (err: any) {
useNotification(t("Error"), err.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
setLoading(false);
}
}),
Expand Down
14 changes: 11 additions & 3 deletions src/components/providers/rules-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { updateRulesProviders, type ProviderRules } from "@/services/api";
import { Refresh } from "@mui/icons-material";
import {
Expand Down Expand Up @@ -30,9 +30,17 @@ export default function RulesProvider(props: RulesProviderProps) {
try {
await updateRulesProviders(provider.name);
onRulesProviderUpdated();
useNotification(t("Success"), t("Update Rules Providers Success"));
useNotification({
title: t("Success"),
body: t("Update Rules Providers Success"),
type: NotificationType.Success,
});
} catch (err: any) {
useNotification(t("Error"), err.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
} finally {
setUpdating(false);
}
Expand Down
80 changes: 66 additions & 14 deletions src/components/setting/mods/clash-core-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseDialog, DialogRef } from "@/components/base";
import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useVerge } from "@/hooks/use-verge";
import { closeAllConnections } from "@/services/api";
import {
Expand Down Expand Up @@ -74,12 +74,24 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
: results[each.core.replace(/-/g, "_") as keyof typeof results],
}));
setValidCores(buf);
useNotification(t("Success"), `Successfully check updates`);
useNotification({
title: t("Success"),
body: t("Successfully check updates"),
type: NotificationType.Success,
});
} catch (e) {
if (e instanceof Error) {
useNotification(t("Error"), e.message);
useNotification({
title: t("Error"),
body: e.message,
type: NotificationType.Error,
});
} else if (typeof e === "string") {
useNotification(t("Error"), e);
useNotification({
title: t("Error"),
body: e,
type: NotificationType.Error,
});
} else {
console.error(e);
}
Expand All @@ -93,9 +105,17 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
try {
setRestartLoading(true);
await restartSidecar();
useNotification(t("Success"), `Successfully restart core`);
useNotification({
title: t("Success"),
body: t("Successfully restart core"),
type: NotificationType.Success,
});
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
} finally {
setRestartLoading(false);
}
Expand All @@ -119,9 +139,17 @@ export const ClashCoreViewer = forwardRef<DialogRef>((props, ref) => {
);
} catch (e) {
if (e instanceof Error) {
useNotification(t("Error"), `Failed to get core version: ${e.message}`);
useNotification({
title: t("Error"),
body: `Failed to get core version: ${e.message}`,
type: NotificationType.Error,
});
} else if (typeof e === "string") {
useNotification(t("Error"), `Failed to get core version: ${e}`);
useNotification({
title: t("Error"),
body: `Failed to get core version: ${e}`,
type: NotificationType.Error,
});
} else {
console.error(e);
}
Expand Down Expand Up @@ -224,9 +252,17 @@ function CoreElement({
mutate("getClashConfig");
mutate("getVersion");
}, 100);
useNotification(t("Success"), `Successfully switch to ${core}`);
useNotification({
title: t("Success"),
body: `Successfully switch to ${core}`,
type: NotificationType.Success,
});
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
} finally {
setLoading(false);
onCoreChanged(core, "finish");
Expand All @@ -238,9 +274,17 @@ function CoreElement({
await grantPermission(core);
// 自动重启
if (selected) await restartSidecar();
useNotification(t("Success"), `Successfully grant permission to ${core}`);
useNotification({
title: t("Success"),
body: `Successfully grant permission to ${core}`,
type: NotificationType.Success,
});
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand All @@ -255,10 +299,18 @@ function CoreElement({
mutate("getClashConfig");
mutate("getVersion");
}, 100);
useNotification(t("Success"), `Successfully updated to ${core}`);
useNotification({
title: t("Success"),
body: `Successfully update core ${core}`,
type: NotificationType.Success,
});
onCoreUpdated?.(core);
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
} finally {
setUpdateCoreLoading(false);
}
Expand Down
8 changes: 6 additions & 2 deletions src/components/setting/mods/clash-field-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseDialog, DialogRef } from "@/components/base";
import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useProfiles } from "@/hooks/use-profiles";
import { getRuntimeExists } from "@/services/cmds";
import {
Expand Down Expand Up @@ -58,7 +58,11 @@ export const ClashFieldViewer = forwardRef<DialogRef>((props, ref) => {
await patchProfiles({ valid: [...curSet] });
// Notice.success("Refresh clash config", 1000);
} catch (err: any) {
useNotification(t("Error"), err?.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
};

Expand Down
14 changes: 11 additions & 3 deletions src/components/setting/mods/clash-port-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseDialog, DialogRef } from "@/components/base";
import { useClashInfo } from "@/hooks/use-clash";
import { useNotification } from "@/hooks/use-notification";
import { NotificationType, useNotification } from "@/hooks/use-notification";
import { useVerge } from "@/hooks/use-verge";
import { List, ListItem, ListItemText, TextField } from "@mui/material";
import { useLockFn } from "ahooks";
Expand Down Expand Up @@ -35,9 +35,17 @@ export const ClashPortViewer = forwardRef<DialogRef>((props, ref) => {
await patchInfo({ "mixed-port": port });
await patchVerge({ verge_mixed_port: port });
setOpen(false);
useNotification(t("Success"), "Change Clash port successfully!");
useNotification({
title: t("Success"),
body: t("Change Clash Port successfully!"),
type: NotificationType.Success,
});
} catch (err: any) {
useNotification(t("Error"), err.message || err.toString());
useNotification({
title: t("Error"),
body: err.message || err.toString(),
type: NotificationType.Error,
});
}
});

Expand Down

0 comments on commit ce0224d

Please sign in to comment.