Skip to content

Commit 94eb764

Browse files
committed
🤖 refactor: DRY - extract duplicated state reset logic
Extracted duplicated state to constants/helpers: - INITIAL_WORKSPACE_MODAL_STATE in App.tsx (used in useState + onClose) - resetForm() in NewWorkspaceModal.tsx (used in handleCancel + handleSubmit) Reduces duplication and ensures consistent state resets across all paths. Generated with `cmux`
1 parent 8fe752e commit 94eb764

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/App.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function AppInner() {
4646
selectedWorkspace,
4747
setSelectedWorkspace,
4848
} = useApp();
49-
const [workspaceModalState, setWorkspaceModalState] = useState({
49+
50+
const INITIAL_WORKSPACE_MODAL_STATE = {
5051
isOpen: false,
5152
projectPath: null as string | null,
5253
projectName: "",
@@ -55,7 +56,9 @@ function AppInner() {
5556
loadError: null as string | null,
5657
startMessage: undefined as string | undefined,
5758
model: undefined as string | undefined,
58-
});
59+
};
60+
61+
const [workspaceModalState, setWorkspaceModalState] = useState(INITIAL_WORKSPACE_MODAL_STATE);
5962
const workspaceModalProjectRef = useRef<string | null>(null);
6063

6164
// Auto-collapse sidebar on mobile by default
@@ -749,16 +752,7 @@ function AppInner() {
749752
initialModel={workspaceModalState.model}
750753
onClose={() => {
751754
workspaceModalProjectRef.current = null;
752-
setWorkspaceModalState({
753-
isOpen: false,
754-
projectPath: null,
755-
projectName: "",
756-
branches: [],
757-
defaultTrunk: undefined,
758-
loadError: null,
759-
startMessage: undefined,
760-
model: undefined,
761-
});
755+
setWorkspaceModalState(INITIAL_WORKSPACE_MODAL_STATE);
762756
}}
763757
onAdd={handleCreateWorkspace}
764758
/>

src/components/NewWorkspaceModal.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ const NewWorkspaceModal: React.FC<NewWorkspaceModalProps> = ({
5252
const [runtimeOptions, setRuntimeOptions] = useNewWorkspaceOptions(projectPath);
5353
const { runtimeMode, sshHost, getRuntimeString } = runtimeOptions;
5454

55+
// Reset form to initial state
56+
const resetForm = () => {
57+
setBranchName("");
58+
setTrunkBranch(defaultTrunkBranch ?? branches[0] ?? "");
59+
setRuntimeOptions(RUNTIME_MODE.LOCAL, "");
60+
setStartMessage("");
61+
};
62+
5563
useEffect(() => {
5664
setError(loadErrorMessage ?? null);
5765
}, [loadErrorMessage]);
@@ -78,10 +86,7 @@ const NewWorkspaceModal: React.FC<NewWorkspaceModalProps> = ({
7886
}, [branches, defaultTrunkBranch, hasBranches]);
7987

8088
const handleCancel = () => {
81-
setBranchName("");
82-
setTrunkBranch(defaultTrunkBranch ?? branches[0] ?? "");
83-
setRuntimeOptions(RUNTIME_MODE.LOCAL, "");
84-
setStartMessage("");
89+
resetForm();
8590
setError(loadErrorMessage ?? null);
8691
onClose();
8792
};
@@ -129,10 +134,7 @@ const NewWorkspaceModal: React.FC<NewWorkspaceModalProps> = ({
129134
trimmedStartMessage || undefined,
130135
initialModel
131136
);
132-
setBranchName("");
133-
setTrunkBranch(defaultTrunkBranch ?? branches[0] ?? "");
134-
setRuntimeOptions(RUNTIME_MODE.LOCAL, "");
135-
setStartMessage("");
137+
resetForm();
136138
onClose();
137139
} catch (err) {
138140
const message = err instanceof Error ? err.message : "Failed to create workspace";

0 commit comments

Comments
 (0)