feature/IO-3205-Paint-Scale-Integrations: Prevent duplicate Paths

This commit is contained in:
Dave Richer
2025-04-28 11:44:20 -04:00
parent fb24eafcc8
commit 9a33993dea
2 changed files with 41 additions and 18 deletions

View File

@@ -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<PaintScaleConfig[]>([]);
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<boolean> => {
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,
};
};
};

View File

@@ -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)",