feature/IO-3205-Paint-Scale-Integrations: Prevent duplicate Paths
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import ipcTypes from '../../../../../util/ipcTypes.json';
|
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';
|
type ConfigType = 'input' | 'output';
|
||||||
|
|
||||||
export const usePaintScaleConfig = (configType: ConfigType) => {
|
export const usePaintScaleConfig = (configType: ConfigType) => {
|
||||||
const [paintScaleConfigs, setPaintScaleConfigs] = useState<PaintScaleConfig[]>([]);
|
const [paintScaleConfigs, setPaintScaleConfigs] = useState<PaintScaleConfig[]>([]);
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
// Get the appropriate IPC methods based on config type
|
// Get the appropriate IPC methods based on config type
|
||||||
const getConfigsMethod = configType === 'input'
|
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
|
// Handle adding a new paint scale config
|
||||||
const handleAddConfig = (type: PaintScaleType) => {
|
const handleAddConfig = (type: PaintScaleType) => {
|
||||||
const newConfig: PaintScaleConfig = {
|
const newConfig: PaintScaleConfig = {
|
||||||
@@ -75,22 +92,25 @@ export const usePaintScaleConfig = (configType: ConfigType) => {
|
|||||||
saveConfigs(updatedConfigs);
|
saveConfigs(updatedConfigs);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle path selection
|
// Handle path selection (modified to check directory uniqueness)
|
||||||
const handlePathChange = (id: string) => {
|
const handlePathChange = async (id: string) => {
|
||||||
window.electron.ipcRenderer
|
try {
|
||||||
.invoke(setPathMethod, id)
|
const path: string | null = await window.electron.ipcRenderer.invoke(setPathMethod, id);
|
||||||
.then((path: string | null) => {
|
if (path) {
|
||||||
if (path) {
|
const isUnique = await checkPathUnique(path);
|
||||||
const updatedConfigs = paintScaleConfigs.map((config) =>
|
if (!isUnique) {
|
||||||
config.id === id ? { ...config, path } : config,
|
message.error(t("settings.errors.duplicatePath"));
|
||||||
);
|
return;
|
||||||
setPaintScaleConfigs(updatedConfigs);
|
}
|
||||||
saveConfigs(updatedConfigs);
|
const updatedConfigs = paintScaleConfigs.map((config) =>
|
||||||
}
|
config.id === id ? { ...config, path } : config,
|
||||||
})
|
);
|
||||||
.catch((error) => {
|
setPaintScaleConfigs(updatedConfigs);
|
||||||
console.error(`Failed to set paint scale ${configType} path:`, error);
|
saveConfigs(updatedConfigs);
|
||||||
});
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to set paint scale ${configType} path:`, error);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handle polling interval change
|
// Handle polling interval change
|
||||||
@@ -109,4 +129,4 @@ export const usePaintScaleConfig = (configType: ConfigType) => {
|
|||||||
handlePathChange,
|
handlePathChange,
|
||||||
handlePollingIntervalChange,
|
handlePollingIntervalChange,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -25,6 +25,9 @@
|
|||||||
"startwatcher": "Start Watcher",
|
"startwatcher": "Start Watcher",
|
||||||
"stopwatcher": "Stop Watcher\n"
|
"stopwatcher": "Stop Watcher\n"
|
||||||
},
|
},
|
||||||
|
"errors": {
|
||||||
|
"duplicatePath": "The selected directory is already used in another configuration."
|
||||||
|
},
|
||||||
"labels": {
|
"labels": {
|
||||||
"emsOutFilePath": "EMS Out File Path (Parts Order, etc.)",
|
"emsOutFilePath": "EMS Out File Path (Parts Order, etc.)",
|
||||||
"pollinginterval": "Polling Interval (ms)",
|
"pollinginterval": "Polling Interval (ms)",
|
||||||
|
|||||||
Reference in New Issue
Block a user