diff --git a/electron/main/Files/Filesystem.ts b/electron/main/Files/Filesystem.ts index f0591882..c52979ed 100644 --- a/electron/main/Files/Filesystem.ts +++ b/electron/main/Files/Filesystem.ts @@ -177,6 +177,22 @@ export function startWatchingDirectory( } } +export function appendExtensionIfMissing( + filename: string, + extensions: string[] +): string { + // Check if the filename ends with any of the provided extensions + const hasExtension = extensions.some((ext) => filename.endsWith(ext)); + + // If the filename already has one of the extensions, return it as is + if (hasExtension) { + return filename; + } + + // If not, append the first extension from the list to the filename + return filename + extensions[0]; +} + export function updateFileListForRenderer( win: BrowserWindow, directory: string, diff --git a/src/components/File/NewNote.tsx b/src/components/File/NewNote.tsx index 775ea5d4..1f2a310e 100644 --- a/src/components/File/NewNote.tsx +++ b/src/components/File/NewNote.tsx @@ -15,14 +15,35 @@ const NewNoteComponent: React.FC = ({ onFileSelect, }) => { const [fileName, setFileName] = useState(""); + const [errorMessage, setErrorMessage] = useState(""); + + const [isValidName, setIsValidName] = useState(true); + + const validNamePattern = /^[a-zA-Z0-9_-]+$/; + + const handleNameChange = (e: React.ChangeEvent) => { + const newName = e.target.value; + if (newName === "") { + setFileName(newName); + setErrorMessage(""); // Clear error message when input is empty + } else if (validNamePattern.test(newName)) { + setFileName(newName); + setErrorMessage(""); // Clear error message for valid input + } else { + // Set an error message for invalid input + setErrorMessage( + "Note name can only contain letters, numbers, underscores, and hyphens." + ); + } + }; const sendNewNoteMsg = async () => { - if (!fileName) { + if (!fileName || errorMessage) { return; } const notePath = await window.files.joinPath( window.electronStore.getUserDirectory(), - fileName + fileName + ".md" ); console.log("NEW NOTE PATH: ", notePath); window.files.createFile(notePath, ""); @@ -45,17 +66,20 @@ const NewNoteComponent: React.FC = ({ type="text" className="block w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:shadow-outline-blue focus:border-blue-300 transition duration-150 ease-in-out" value={fileName} - onChange={(e) => setFileName(e.target.value)} + // onChange={(e) => setFileName(e.target.value)} + onChange={handleNameChange} onKeyDown={handleKeyPress} placeholder="Note Name" /> + + {errorMessage &&

{errorMessage}

} );