Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification service #414

Merged
merged 4 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
"mousetrap": "^1.6.5",
"node-stream-zip": "^1.15.0",
"nodejs-traceroute": "2.0.0",
"notistack": "^3.0.0-alpha.2",
"notistack": "^3.0.1",
"obs-websocket-js": "^5.0.1",
"observable-fns": "^0.6.1",
"os-name": "^5.1.0",
Expand Down
27 changes: 3 additions & 24 deletions src/renderer/lib/hooks/use_toasts.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
import { useSnackbar } from "notistack";
import React from "react";
import { useServices } from "@/services";

export const useToasts = () => {
const { enqueueSnackbar } = useSnackbar();
const toastHandlers = React.useMemo(
() => ({
showError: (error: Error | string | unknown) => {
let message: string;
if (error instanceof Error) {
message = error.message;
} else if (typeof error === "string") {
message = error;
} else {
message = JSON.stringify(error);
}
// Let's not automatically hide error messages
enqueueSnackbar(message, { variant: "error", persist: true });
},
showSuccess: (message: string) => enqueueSnackbar(message, { variant: "success", autoHideDuration: 2500 }),
showWarning: (message: string) => enqueueSnackbar(message, { variant: "warning" }),
showInfo: (message: string) => enqueueSnackbar(message, { variant: "info" }),
}),
[enqueueSnackbar],
);
return toastHandlers;
const { notificationService } = useServices();
return notificationService;
};
3 changes: 3 additions & 0 deletions src/renderer/services/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { appVersion } from "@common/constants";

import createAuthClient from "./auth/auth.service";
import createDolphinClient from "./dolphin/dolphin.service";
import createNotificationClient from "./notification/notification.service";
import createReplayClient from "./replay/replay.service";
import createSlippiClient from "./slippi/slippi.service";
import type { Services } from "./types";
Expand All @@ -18,6 +19,7 @@ export async function installServices(): Promise<Services> {
dolphinService,
`${appVersion}${isDevelopment ? "-dev" : ""}`,
);
const notificationService = createNotificationClient();

const broadcastService = window.electron.broadcast;
const consoleService = window.electron.console;
Expand All @@ -29,5 +31,6 @@ export async function installServices(): Promise<Services> {
broadcastService,
consoleService,
replayService,
notificationService,
};
}
23 changes: 23 additions & 0 deletions src/renderer/services/notification/notification.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { enqueueSnackbar } from "notistack";

import type { NotificationService } from "./types";

export default function createNotificationClient(): NotificationService {
return {
showError: (error: Error | string | unknown) => {
let message: string;
if (error instanceof Error) {
message = error.message;
} else if (typeof error === "string") {
message = error;
} else {
message = JSON.stringify(error);
}
// Let's not automatically hide error messages
enqueueSnackbar(message, { variant: "error", persist: true });
},
showSuccess: (message: string) => enqueueSnackbar(message, { variant: "success", autoHideDuration: 2500 }),
showWarning: (message: string) => enqueueSnackbar(message, { variant: "warning" }),
showInfo: (message: string) => enqueueSnackbar(message, { variant: "info" }),
};
}
6 changes: 6 additions & 0 deletions src/renderer/services/notification/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface NotificationService {
showError: (error: Error | string | unknown) => void;
showSuccess: (message: string) => void;
showWarning: (message: string) => void;
showInfo: (message: string) => void;
}
2 changes: 2 additions & 0 deletions src/renderer/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DolphinService } from "@dolphin/types";
import type { ReplayService } from "@replays/types";

import type { AuthService } from "./auth/types";
import type { NotificationService } from "./notification/types";
import type { SlippiBackendService } from "./slippi/types";

export type Services = {
Expand All @@ -13,4 +14,5 @@ export type Services = {
broadcastService: BroadcastService;
consoleService: ConsoleService;
replayService: ReplayService;
notificationService: NotificationService;
};
11 changes: 5 additions & 6 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading