diff --git a/src/renderer/src/components/Settings/PaintScale/usePaintScaleConfig.ts b/src/renderer/src/components/Settings/PaintScale/usePaintScaleConfig.ts index 64462e7..a79bf0d 100644 --- a/src/renderer/src/components/Settings/PaintScale/usePaintScaleConfig.ts +++ b/src/renderer/src/components/Settings/PaintScale/usePaintScaleConfig.ts @@ -1,11 +1,14 @@ import { useState, useEffect } from 'react'; import ipcTypes from '../../../../../util/ipcTypes.json'; -import {PaintScaleConfig, PaintScaleType} from "./types"; +import { PaintScaleConfig, PaintScaleType } from "./types"; +import { message } from "antd"; +import {useTranslation} from "react-i18next"; type ConfigType = 'input' | 'output'; export const usePaintScaleConfig = (configType: ConfigType) => { const [paintScaleConfigs, setPaintScaleConfigs] = useState([]); + const { t } = useTranslation(); // Get the appropriate IPC methods based on config type const getConfigsMethod = configType === 'input' @@ -55,6 +58,20 @@ export const usePaintScaleConfig = (configType: ConfigType) => { }); }; + // New helper to check if a path is unique across input and output configs + const checkPathUnique = async (newPath: string): Promise => { + try { + const inputConfigs: PaintScaleConfig[] = await window.electron.ipcRenderer.invoke(ipcTypes.toMain.settings.paintScale.getInputConfigs); + const outputConfigs: PaintScaleConfig[] = await window.electron.ipcRenderer.invoke(ipcTypes.toMain.settings.paintScale.getOutputConfigs); + const allConfigs = [...inputConfigs, ...outputConfigs]; + // Allow updating the current config even if its current value equals newPath. + return !allConfigs.some(config => config.path === newPath); + } catch (error) { + console.error("Failed to check unique path:", error); + return false; + } + }; + // Handle adding a new paint scale config const handleAddConfig = (type: PaintScaleType) => { const newConfig: PaintScaleConfig = { @@ -75,22 +92,25 @@ export const usePaintScaleConfig = (configType: ConfigType) => { saveConfigs(updatedConfigs); }; - // Handle path selection - const handlePathChange = (id: string) => { - window.electron.ipcRenderer - .invoke(setPathMethod, id) - .then((path: string | null) => { - if (path) { - const updatedConfigs = paintScaleConfigs.map((config) => - config.id === id ? { ...config, path } : config, - ); - setPaintScaleConfigs(updatedConfigs); - saveConfigs(updatedConfigs); - } - }) - .catch((error) => { - console.error(`Failed to set paint scale ${configType} path:`, error); - }); + // Handle path selection (modified to check directory uniqueness) + const handlePathChange = async (id: string) => { + try { + const path: string | null = await window.electron.ipcRenderer.invoke(setPathMethod, id); + if (path) { + const isUnique = await checkPathUnique(path); + if (!isUnique) { + message.error(t("settings.errors.duplicatePath")); + return; + } + const updatedConfigs = paintScaleConfigs.map((config) => + config.id === id ? { ...config, path } : config, + ); + setPaintScaleConfigs(updatedConfigs); + saveConfigs(updatedConfigs); + } + } catch (error) { + console.error(`Failed to set paint scale ${configType} path:`, error); + } }; // Handle polling interval change @@ -109,4 +129,4 @@ export const usePaintScaleConfig = (configType: ConfigType) => { handlePathChange, handlePollingIntervalChange, }; -}; \ No newline at end of file +}; diff --git a/src/util/translations/en-US/renderer.json b/src/util/translations/en-US/renderer.json index f86bf3a..50d631c 100644 --- a/src/util/translations/en-US/renderer.json +++ b/src/util/translations/en-US/renderer.json @@ -25,6 +25,9 @@ "startwatcher": "Start Watcher", "stopwatcher": "Stop Watcher\n" }, + "errors": { + "duplicatePath": "The selected directory is already used in another configuration." + }, "labels": { "emsOutFilePath": "EMS Out File Path (Parts Order, etc.)", "pollinginterval": "Polling Interval (ms)",