* Wrap desktop IPC calls in shared hooks * Unslop desktop IPC hook callsites * Redesign desktop IPC hooks around domains
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { useCallback, useEffect, useRef } from "react";
|
|
import type { ToastApi } from "@/components/toast-host";
|
|
import { useToast } from "@/contexts/toast-context";
|
|
|
|
interface DesktopIpcErrorReport {
|
|
toast: ToastApi;
|
|
logLabel: string;
|
|
message: string;
|
|
error: unknown;
|
|
}
|
|
|
|
interface DesktopIpcQueryErrorToastOptions {
|
|
error: Error | null;
|
|
logLabel: string;
|
|
message: string;
|
|
}
|
|
|
|
interface DesktopIpcErrorReporterInput {
|
|
logLabel: string;
|
|
message: string;
|
|
error: unknown;
|
|
}
|
|
|
|
export function reportDesktopIpcError(input: DesktopIpcErrorReport): void {
|
|
console.error(input.logLabel, input.error);
|
|
input.toast.error(input.message);
|
|
}
|
|
|
|
export function useDesktopIpcErrorReporter(): (input: DesktopIpcErrorReporterInput) => void {
|
|
const toast = useToast();
|
|
return useCallback(
|
|
(input: DesktopIpcErrorReporterInput) => {
|
|
reportDesktopIpcError({ ...input, toast });
|
|
},
|
|
[toast],
|
|
);
|
|
}
|
|
|
|
export function useDesktopIpcQueryErrorToast(options: DesktopIpcQueryErrorToastOptions): void {
|
|
const toast = useToast();
|
|
const lastReportedErrorRef = useRef<Error | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!options.error || options.error === lastReportedErrorRef.current) {
|
|
return;
|
|
}
|
|
|
|
lastReportedErrorRef.current = options.error;
|
|
reportDesktopIpcError({
|
|
toast,
|
|
logLabel: options.logLabel,
|
|
message: options.message,
|
|
error: options.error,
|
|
});
|
|
}, [options.error, options.logLabel, options.message, toast]);
|
|
}
|