import React from "react";
import { observable, action, computed, makeObservable } from "mobx";
import { observer } from "mobx-react";
import {
getFileName,
getValidFileNameFromFileName,
getShortFileName,
fileExists,
isValidFileName,
isValidPath
} from "eez-studio-shared/util-electron";
import { makeValidator, validators } from "eez-studio-shared/validation";
import { Dialog, showDialog } from "eez-studio-ui/dialog";
import {
PropertyList,
TextInputProperty,
NumberInputProperty,
BooleanProperty
} from "eez-studio-ui/properties";
import { FileInputProperty } from "eez-studio-ui/properties-electron";
import { IFileUploadInstructions } from "instrument/connection/file-upload";
const FileUploadSettingsDialog = observer(
class FileUploadSettingsDialog extends React.Component<{
instructions: IFileUploadInstructions;
callback: (instructions: IFileUploadInstructions) => void;
}> {
constructor(props: any) {
super(props);
makeObservable(this, {
instructions: observable
});
this.instructions = { ...this.props.instructions };
}
instructions: IFileUploadInstructions;
validator = makeValidator({
startCommandTemplate: validators.required,
sendChunkCommandTemplate: validators.required,
chunkSize: validators.rangeInclusive(1, 4096)
});
handleSubmit = async () => {
if (!(await this.validator.checkValidity(this.instructions))) {
return false;
}
this.props.callback(this.instructions);
return true;
};
render() {
return (
);
}
}
);
function showAdvancedSettingsDialog(
instructions: IFileUploadInstructions,
callback: (instructions: IFileUploadInstructions) => void
) {
showDialog(
);
}
const FileUploadDialog = observer(
class FileUploadDialog extends React.Component<{
instructions: IFileUploadInstructions;
callback: (instructions: IFileUploadInstructions) => void;
}> {
constructor(props: any) {
super(props);
makeObservable(this, {
instructions: observable,
destinationFileNameChanged: observable,
destinationFolderPathChanged: observable,
destinationFoldePathSuggestions: computed
});
this.instructions = { ...this.props.instructions };
}
instructions: IFileUploadInstructions;
destinationFileNameChanged = false;
destinationFolderPathChanged = false;
validator = makeValidator({
sourceFilePath: [
validators.required,
async () => {
if (
this.instructions.sourceFilePath &&
!(await fileExists(this.instructions.sourceFilePath))
) {
return "File not found.";
}
return null;
}
],
destinationFileName: [
validators.required,
() => {
if (
this.instructions.destinationFileName &&
!isValidFileName(
this.instructions.destinationFileName,
this.instructions.shortFileName
)
) {
return "Invalid file name.";
}
return null;
}
],
destinationFolderPath: [
() => {
if (
this.instructions.destinationFolderPath &&
!isValidPath(
this.instructions.destinationFolderPath,
this.instructions.shortFileName
)
) {
return "Invalid path.";
}
return null;
}
]
});
handleSubmit = async () => {
if (!(await this.validator.checkValidity(this.instructions))) {
return false;
}
this.props.callback(this.instructions);
return true;
};
deriveDestinationFileNameFromSourceFilePath() {
if (this.instructions.shortFileName) {
return getShortFileName(this.instructions.sourceFilePath!);
} else {
return getValidFileNameFromFileName(
getFileName(this.instructions.sourceFilePath!)
);
}
}
deriveDestinationFolderPathFromSourceFilePath() {
const sourceFilePath = this.instructions.sourceFilePath;
const favoriteDestinationPaths =
this.instructions.favoriteDestinationPaths;
if (sourceFilePath && favoriteDestinationPaths) {
const favoriteDestinationPath = favoriteDestinationPaths.find(
favoriteDestinationPath =>
favoriteDestinationPath.ext &&
sourceFilePath!.endsWith(favoriteDestinationPath.ext)
);
if (favoriteDestinationPath) {
return favoriteDestinationPath.path;
}
}
return this.instructions.destinationFolderPath;
}
get destinationFoldePathSuggestions() {
if (this.instructions.favoriteDestinationPaths) {
return this.instructions.favoriteDestinationPaths
.map(
favoriteDestinationPath => favoriteDestinationPath.path
)
.filter(
(favoriteDestinationPath, index, self) =>
self.indexOf(favoriteDestinationPath) == index
);
}
return undefined;
}
render() {
return (
);
}
}
);
export function showFileUploadDialog(
instructions: IFileUploadInstructions,
callback: (instructions: IFileUploadInstructions) => void
) {
showDialog(
);
}