Skip to content

Commit

Permalink
Fix: エラーハンドリングを改善し、Tauriダイアログを使用してエラーメッセージを表示
Browse files Browse the repository at this point in the history
  • Loading branch information
shm11C3 committed Nov 23, 2024
1 parent 93836f7 commit 0d47efc
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/atom/useHardwareInfoAtom.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useTauriDialog } from "@/hooks/useTauriDialog";
import { type SysInfo, commands } from "@/rspc/bindings";
import { isError } from "@/types/result";
import { atom, useAtom } from "jotai";
Expand All @@ -10,11 +11,12 @@ const hardInfoAtom = atom<SysInfo>({

export const useHardwareInfoAtom = () => {
const [hardwareInfo, setHardInfo] = useAtom(hardInfoAtom);
const { error } = useTauriDialog();

const init = async () => {
const fetchedHardwareInfo = await commands.getHardwareInfo();

if (isError(fetchedHardwareInfo)) {
error(fetchedHardwareInfo.error);
console.error("Failed to fetch hardware info:", fetchedHardwareInfo);
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/atom/useSettingsAtom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { defaultColorRGB } from "@/consts/chart";
import { useTauriDialog } from "@/hooks/useTauriDialog";
import { type Result, commands } from "@/rspc/bindings";
import type { ChartDataType } from "@/types/hardwareDataType";
import { isError } from "@/types/result";
Expand Down Expand Up @@ -26,6 +27,7 @@ const settingsAtom = atom<Settings>({
});

export const useSettingsAtom = () => {
const { error } = useTauriDialog();
const mapSettingUpdater: {
[K in keyof Omit<Settings, "state" | "lineGraphColor">]: (
value: Settings[K],
Expand All @@ -46,10 +48,12 @@ export const useSettingsAtom = () => {

const [settings, setSettings] = useAtom(settingsAtom);

// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
const loadSettings = useCallback(async () => {
const setting = await commands.getSettings();

if (isError(setting)) {
error(setting.error);
console.error("Failed to fetch settings:", setting.error);
return;
}
Expand All @@ -69,6 +73,7 @@ export const useSettingsAtom = () => {
const result = await mapSettingUpdater[key](value);

if (isError(result)) {
error(result.error);
console.error(result.error);
setSettings((prev) => ({ ...prev, [key]: previousValue }));
}
Expand All @@ -82,6 +87,7 @@ export const useSettingsAtom = () => {
const result = await commands.setDisplayTargets(newTargets);

if (isError(result)) {
error(result.error);
console.error(result.error);
return;
}
Expand All @@ -102,6 +108,7 @@ export const useSettingsAtom = () => {
const result = await commands.setLineGraphColor(key, value);

if (isError(result)) {
error(result.error);
console.error(result.error);
return;
}
Expand Down
9 changes: 6 additions & 3 deletions src/components/charts/ProcessTable.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useTauriDialog } from "@/hooks/useTauriDialog";
import { type ProcessInfo, commands } from "@/rspc/bindings";
import { CaretDown } from "@phosphor-icons/react";
import { atom, useAtom, useSetAtom } from "jotai";
Expand All @@ -10,6 +11,7 @@ const ProcessesTable = ({
defaultItemLength,
}: { defaultItemLength: number }) => {
const { t } = useTranslation();
const { error } = useTauriDialog();
const [processes] = useAtom(processesAtom);
const setAtom = useSetAtom(processesAtom);
const [showAllItem, setShowAllItem] = useState<boolean>(false);
Expand All @@ -23,8 +25,9 @@ const ProcessesTable = ({
try {
const processesData = await commands.getProcessList();
setAtom(processesData);
} catch (error) {
console.error("Failed to fetch processes:", error);
} catch (err) {
error(err as string);
console.error("Failed to fetch processes:", err);
}
};

Expand All @@ -33,7 +36,7 @@ const ProcessesTable = ({
const interval = setInterval(fetchProcesses, 3000);

return () => clearInterval(interval);
}, [setAtom]);
}, [setAtom, error]);

const sortedProcesses = [...processes];
if (sortConfig !== null) {
Expand Down
7 changes: 5 additions & 2 deletions src/components/forms/UploadImage/useUploadImageForm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useBackgroundImage } from "@/hooks/useBgImage";
import { useTauriDialog } from "@/hooks/useTauriDialog";
import { zodResolver } from "@hookform/resolvers/zod";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
Expand All @@ -8,6 +9,7 @@ import { z } from "zod";
// 画像アップロードカスタムフック
export const useUploadImage = () => {
const { t } = useTranslation();
const { error } = useTauriDialog();
const [displayUrl, setDisplayUrl] = useState<string | null>(null);
const [fileName, setFilename] = useState<string>("");
const [isSubmitting, setIsSubmitting] = useState(false);
Expand Down Expand Up @@ -37,8 +39,9 @@ export const useUploadImage = () => {
try {
await saveBackgroundImage(values.picture);
form.reset();
} catch (error) {
console.error("Error saveBackgroundImage:", error);
} catch (err) {
error(err as string);
console.error("Error saveBackgroundImage:", err);
}

setIsSubmitting(false);
Expand Down
12 changes: 10 additions & 2 deletions src/hooks/useBgImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import { isError, isOk } from "@/types/result";
import type { BackgroundImage } from "@/types/settingsType";
import { atom, useAtom } from "jotai";
import { useCallback, useEffect } from "react";
import { useTauriDialog } from "./useTauriDialog";

const backgroundImageAtom = atom<string | null>(null);
const uploadedBackgroundImagesAtom = atom<Array<BackgroundImage>>([]);

export const useBackgroundImage = () => {
const { error } = useTauriDialog();
const [backgroundImage, setBackgroundImage] = useAtom(backgroundImageAtom);
const { initBackgroundImages, backgroundImageList, setBackgroundImageList } =
useBackgroundImageList();

const { settings, updateSettingAtom } = useSettingsAtom();

// biome-ignore lint/correctness/useExhaustiveDependencies: <explanation>
const initBackgroundImage = useCallback(async () => {
if (settings.selectedBackgroundImg) {
const base64Image = await commands.getBackgroundImage(
Expand All @@ -25,6 +28,7 @@ export const useBackgroundImage = () => {
if (isOk(base64Image)) {
setBackgroundImage(`data:image/png;base64,${base64Image.data}`);
} else {
error(base64Image.error);
console.error("Failed to load background image:", base64Image.error);
}
} else {
Expand All @@ -43,6 +47,7 @@ export const useBackgroundImage = () => {
const fileId = await commands.saveBackgroundImage(base64Image);

if (isError(fileId)) {
error(fileId.error);
console.error("Failed to save background image:", fileId.error);
return;
}
Expand All @@ -63,8 +68,9 @@ export const useBackgroundImage = () => {
(v) => v.fileId !== fileId,
);
setBackgroundImageList(newBackgroundImages);
} catch (error) {
console.error("Failed to delete background image:", error);
} catch (err) {
error(err as string);
console.error("Failed to delete background image:", err);
}
};

Expand All @@ -77,6 +83,7 @@ export const useBackgroundImage = () => {
};

export const useBackgroundImageList = () => {
const { error } = useTauriDialog();
const [backgroundImageList, setBackgroundImageList] = useAtom(
uploadedBackgroundImagesAtom,
);
Expand All @@ -85,6 +92,7 @@ export const useBackgroundImageList = () => {
const uploadedBackgroundImages = await commands.getBackgroundImages();

if (isError(uploadedBackgroundImages)) {
error(uploadedBackgroundImages.error);
console.error(
"Failed to load background images:",
uploadedBackgroundImages.error,
Expand Down
5 changes: 4 additions & 1 deletion src/hooks/useInputListener.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { commands } from "@/rspc/bindings";
import { useEffect } from "react";
import { useTauriDialog } from "./useTauriDialog";
import { useTauriStore } from "./useTauriStore";

export const useKeydown = () => {
const { error } = useTauriDialog();
const [isDecorated, setDecorated] = useTauriStore("window_decorated", false);

useEffect(() => {
Expand All @@ -11,6 +13,7 @@ export const useKeydown = () => {
await commands.setDecoration(!isDecorated);
await setDecorated(!isDecorated);
} catch (e) {
error(e as string);
console.error("Failed to toggle window decoration:", e);
}
};
Expand All @@ -23,5 +26,5 @@ export const useKeydown = () => {
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
}, [isDecorated, setDecorated]);
}, [isDecorated, setDecorated, error]);
};
62 changes: 62 additions & 0 deletions src/hooks/useTauriDialog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
ask as showAsk,
confirm as showConfirm,
message as showMessage,
} from "@tauri-apps/plugin-dialog";
import { useTranslation } from "react-i18next";

type Kind = "info" | "warning" | "error";
type TitleType =
| "info"
| "confirm"
| "success"
| "warning"
| "error"
| "unexpected";

export const useTauriDialog = () => {
const { t } = useTranslation();

const ask = async ({
title,
message,
kind,
}: { title?: TitleType; message: string; kind?: Kind }): Promise<boolean> => {
return await showAsk(message, {
title: title ? t(`error.title.${title}`) : undefined,
kind,
});
};

const confirm = async ({
title,
message,
kind,
}: { title?: TitleType; message: string; kind?: Kind }): Promise<boolean> => {
return await showConfirm(message, {
title: title ? t(`error.title.${title}`) : undefined,
kind,
});
};

const message = async ({
title,
message,
kind,
}: { title?: TitleType; message: string; kind?: Kind }): Promise<void> => {
return await showMessage(message, {
title: title ? t(`error.title.${title}`) : undefined,
kind,
});
};

const error = async (errorMessage: string) => {
return await message({
title: "error",
message: errorMessage,
kind: "error",
});
};

return { ask, confirm, message, error };
};
11 changes: 11 additions & 0 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,16 @@
"opacity": "Opacity"
}
}
},

"error": {
"title": {
"info": "Info",
"confirm": "Confirm",
"warning": "Warning",
"error": "Error",
"success": "Success",
"unexpected": "Unexpected Error"
}
}
}
11 changes: 11 additions & 0 deletions src/i18n/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,16 @@
"opacity": "透過率"
}
}
},

"error": {
"title": {
"info": "情報",
"confirm": "確認",
"warning": "警告",
"error": "エラー",
"success": "成功しました",
"unexpected": "予期せぬエラーが発生しました"
}
}
}

0 comments on commit 0d47efc

Please sign in to comment.