import React, { useRef } from 'react'; import { useDispatch } from 'react-redux'; import StyledWrapper from './StyledWrapper'; import { IconTrash, IconFile, IconFileImport, IconAlertCircle, IconFolder } from '@tabler/icons'; import { getBasename } from 'utils/common/path'; import { Tooltip } from 'react-tooltip'; import useProtoFileManagement from '../../../hooks/useProtoFileManagement'; import { saveCollectionSettings } from 'providers/ReduxStore/slices/collections/actions'; import Button from 'ui/Button'; const ProtobufSettings = ({ collection }) => { const dispatch = useDispatch(); const { protoFiles, importPaths, addProtoFileToCollection, addImportPathToCollection, toggleImportPath, browseForProtoFile, browseForImportDirectory, removeProtoFileFromCollection, removeImportPathFromCollection, replaceImportPathInCollection, replaceProtoFileInCollection } = useProtoFileManagement(collection); const fileInputRef = useRef(null); const handleSave = () => dispatch(saveCollectionSettings(collection.uid)); // Get file path using the ipcRenderer const getProtoFile = async (event) => { const files = event?.files; if (files && files.length > 0) { for (let i = 0; i < files.length; i++) { const filePath = window?.ipcRenderer?.getFilePath(files[i]); if (filePath) { await addProtoFileToCollection(filePath); } } // Reset the file input if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; const handleRemoveProtoFile = async (index) => { await removeProtoFileFromCollection(index); }; const handleBrowseClick = () => { if (fileInputRef.current) { fileInputRef.current.click(); } }; const handleReplaceProtoFile = async (index) => { const result = await browseForProtoFile(); if (result.success) { await replaceProtoFileInCollection(index, result.filePath); } }; const handleReplaceImportPath = async (index) => { const result = await browseForImportDirectory(); if (result.success) { await replaceImportPathInCollection(index, result.directoryPath); } }; const handleFileInputChange = (e) => { getProtoFile(e.target); }; const getImportPath = async () => { const result = await browseForImportDirectory(); if (result.success) { await addImportPathToCollection(result.directoryPath); } }; const handleRemoveImportPath = async (index) => { await removeImportPathFromCollection(index); }; const handleToggleImportPath = async (index) => { await toggleImportPath(index); }; const handleBrowseImportPathClick = () => { getImportPath(); }; return ( {/* Hidden file input for file selection */} {/* Proto Files Section */}
{protoFiles.some((file) => !file.exists) && (
Some proto files cannot be found. Use the replace option to update their locations.
)} {protoFiles.length === 0 ? ( ) : ( protoFiles.map((file, index) => { const isValid = file.exists; return ( ); }) )}
File Path Actions
No proto files added
{getBasename(collection.pathname, file.path)} {!isValid && }
{file.path}
{!isValid && ( )}
{/* Import Paths Section */}
{importPaths.some((path) => !path.exists) && (
Some import paths cannot be found at their specified locations.
)} {importPaths.length === 0 ? ( ) : ( importPaths.map((importPath, index) => { const isValid = importPath.exists; return ( ); }) )}
Directory Path Actions
No import paths added
handleToggleImportPath(index)} className="h-4 w-4" title={importPath.enabled ? 'Disable this import path' : 'Enable this import path'} data-testid="protobuf-import-path-checkbox" />
{getBasename(collection.pathname, importPath.path)} {!isValid && }
{importPath.path}
{!isValid && ( )}
); }; export default ProtobufSettings;