Component: Form
Relevant source files
The following files were used as context for generating this wiki page:The Form component is a key user interface element for adding new PowerShell scripts to the Scripty application. It provides a modal-like form where users can specify a name for the script, assign one or more tags, and select a .ps1 or .txt file from their local system. Upon submission, the component interacts with the Tauri backend to read the selected file's content and then uses a Zustand store to persist the new script data within the application's state.
The component's visibility is controlled by the parent App component. It uses motion/react for entry and exit animations, providing a smooth user experience. It is closely related to the FormTag component, which handles the creation of new tags that can then be used within this form.
Sources: src/components/Form/Form.jsx, src/App.jsx:4, 10, 80, src/components/Form/FormTag.jsx
Architecture and Data Flow
The Form component is part of the React frontend and integrates with both the application's state management (Zustand) and the Rust-based Tauri backend.
Component Rendering
The Form component is conditionally rendered within App.jsx. Its visibility is managed by the popOpen state variable. When popOpen is true, the Form is rendered, and when false, it is removed from the DOM. The AnimatePresence component from motion/react wraps the Form to handle its mounting and unmounting animations.
Sources: src/App.jsx:4, 48, 80
The following diagram illustrates the component's visibility toggle logic within the App component.
Sources: src/App.jsx:48-51, 80
State Management
The component relies on a global Zustand store, useListStore, for state interactions.
addItem: A function used to add a new script object to the globalitemsarray.tags: An array of available strings used to populate the "Select Tags" dropdown.
The Form component itself maintains local state for selectedTags using the useState hook to track which tags are currently selected in the multi-select input.
Sources: src/components/Form/Form.jsx:5, 40, src/Hooks/useListStore.js:33, 36
Form Submission Flow
The core logic resides in the handleSubmit function, which orchestrates the process of reading a file and adding it to the application's state.
The overall data flow on form submission is as follows:
Key Functions and Logic
handleSubmit(evt)
This asynchronous function is the onSubmit handler for the form. It prevents the default form submission, orchestrates file loading, creates a new script item object, adds it to the global store, and finally resets and hides the form.
// src/components/Form/Form.jsx:46-63
const handleSubmit = async (evt) => {
evt.preventDefault();
const ps1Content = await handleLoadPS1();
if (ps1Content === null) {
return;
}
addItem({
title: evt.target.sp.value,
content: ps1Content,
tags: selectedTags,
date: new Date().toLocaleDateString(),
id: crypto.randomUUID(),
});
evt.target.reset();
isVisible(false);
};
handleLoadPS1()
This helper function uses the @tauri-apps/plugin-dialog to open a native file selection dialog, filtered for .ps1 and .txt files. It then invokes the read_ps1 command in the Rust backend to get the file's content. If the file cannot be read, it displays an error message dialog.
Sources: src/components/Form/Form.jsx:7-35
read_ps1 (Tauri Command)
This Rust function is exposed to the frontend as a Tauri command. It takes a file path as a string, attempts to read the file into a string, and returns a tuple of (path, content) on success or an error string on failure.
// src-tauri/src/lib.rs:67-72
#[tauri::command]
fn read_ps1(path: String) -> Result<(String, String), String> {
match fs::read_to_string(&path) {
Ok(content) => Ok((path, content)),
Err(_e) => Err(format!("Failed to resolve the .ps1 file")),
}
}
Form Structure and Item Schema
Form Inputs
The form contains three main interactive elements.
| Field | HTML Element | Name | Purpose | Validation/Properties |
|---|---|---|---|---|
| Script Name | <input> | sp | To provide a user-friendly name for the script. | type="text", required, maxLength={12} |
| Select Tags | <select> | tags | To assign one or more existing tags. | multiple |
| Add File | <button> | N/A | To submit the form. | type="submit" |
Sources: src/components/Form/Form.jsx:71-105
Script Item Schema
When the form is submitted successfully, a new item object is created with the following structure and added to the items array in useListStore.
| Field | Type | Description | Source |
|---|---|---|---|
id | string | A unique identifier generated by crypto.randomUUID(). | crypto.randomUUID() |
title | string | The name of the script from the "Script Name" input. | evt.target.sp.value |
content | string | The content of the selected .ps1 file. | Result of handleLoadPS1() |
tags | string[] | An array of selected tag strings. | selectedTags state |
date | string | The date the script was added, in local string format. | new Date().toLocaleDateString() |
Sources: src/components/Form/Form.jsx:52-58, src/Hooks/useListStore.js:33-35