|
| 1 | +import { useState, useMemo, useEffect } from "react"; |
| 2 | +import { autosubmitApiV4 } from "../services/autosubmitApiV4"; |
| 3 | +import { cn } from "../services/utils"; |
| 4 | + |
| 5 | +export const RunnerOptionsFormSection = ({ onSelectionChange }) => { |
| 6 | + const { |
| 7 | + data: runnerProfiles, |
| 8 | + error: runnerProfilesError, |
| 9 | + isLoading: runnerProfilesLoading, |
| 10 | + } = autosubmitApiV4.endpoints.getRunnersConfigProfiles.useQuery(); |
| 11 | + |
| 12 | + const activeRunnerProfiles = useMemo( |
| 13 | + () => (runnerProfiles ? Object.keys(runnerProfiles) : []), |
| 14 | + [runnerProfiles] |
| 15 | + ); |
| 16 | + |
| 17 | + const [selectedProfileName, setSelectedProfileName] = useState(""); |
| 18 | + const [runnerConfig, setRunnerConfig] = useState({}); |
| 19 | + |
| 20 | + const selectedProfile = useMemo(() => { |
| 21 | + if (!selectedProfileName || !runnerProfiles) return null; |
| 22 | + return runnerProfiles[selectedProfileName]; |
| 23 | + }, [selectedProfileName, runnerProfiles]); |
| 24 | + |
| 25 | + const isSSHRunner = selectedProfile?.RUNNER_TYPE === "SSH"; |
| 26 | + const hasModules = selectedProfile?.MODULE_LOADER_TYPE !== "NO_MODULE"; |
| 27 | + |
| 28 | + const handleProfileNameChange = (profileName) => { |
| 29 | + const updatedSelection = { profile_name: profileName, profile_params: {} }; |
| 30 | + setSelectedProfileName(updatedSelection.profile_name); |
| 31 | + setRunnerConfig(updatedSelection.profile_params); |
| 32 | + onSelectionChange?.(updatedSelection); |
| 33 | + }; |
| 34 | + |
| 35 | + const handleConfigChange = (field, value) => { |
| 36 | + const updatedConfig = { ...runnerConfig, [field]: value }; |
| 37 | + setRunnerConfig(updatedConfig); |
| 38 | + onSelectionChange?.({ |
| 39 | + profile_name: selectedProfileName, |
| 40 | + profile_params: updatedConfig, |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + const handleSSHChange = (field, value) => { |
| 45 | + const updatedSSH = { ...runnerConfig.SSH, [field]: value }; |
| 46 | + const updatedConfig = { ...runnerConfig, SSH: updatedSSH }; |
| 47 | + setRunnerConfig(updatedConfig); |
| 48 | + onSelectionChange?.({ |
| 49 | + profile_name: selectedProfileName, |
| 50 | + profile_params: updatedConfig, |
| 51 | + }); |
| 52 | + }; |
| 53 | + |
| 54 | + // Render loading/error states |
| 55 | + if (runnerProfilesLoading) { |
| 56 | + return <div className="text-gray-500">Loading runner profiles...</div>; |
| 57 | + } |
| 58 | + |
| 59 | + if (runnerProfilesError) { |
| 60 | + return <div className="text-red-500">Error loading runner profiles</div>; |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="flex flex-col w-full gap-2"> |
| 65 | + <div className="flex gap-2 items-center"> |
| 66 | + <label className="font-semibold text-lg">Runner profile:</label> |
| 67 | + <select |
| 68 | + className="px-2 py-1 border rounded text-sm" |
| 69 | + value={selectedProfileName} |
| 70 | + onChange={(e) => handleProfileNameChange(e.target.value)} |
| 71 | + > |
| 72 | + <option value="" disabled> |
| 73 | + Select Runner profile |
| 74 | + </option> |
| 75 | + {activeRunnerProfiles.map((runner) => ( |
| 76 | + <option key={runner} value={runner}> |
| 77 | + {runner} |
| 78 | + </option> |
| 79 | + ))} |
| 80 | + </select> |
| 81 | + </div> |
| 82 | + |
| 83 | + {selectedProfile && ( |
| 84 | + <div className="flex flex-col border rounded py-4 px-4 gap-3"> |
| 85 | + <div className="flex items-center"> |
| 86 | + <label className="font-semibold min-w-[150px]">Runner type:</label> |
| 87 | + {selectedProfile.RUNNER_TYPE ? ( |
| 88 | + <div className="ml-2">{selectedProfile.RUNNER_TYPE}</div> |
| 89 | + ) : ( |
| 90 | + <input |
| 91 | + type="text" |
| 92 | + className="ml-2 px-2 py-1 border rounded text-sm flex-1" |
| 93 | + placeholder="Enter runner type" |
| 94 | + value={runnerConfig.RUNNER_TYPE || ""} |
| 95 | + onChange={(e) => |
| 96 | + handleConfigChange("RUNNER_TYPE", e.target.value) |
| 97 | + } |
| 98 | + /> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + |
| 102 | + {isSSHRunner && ( |
| 103 | + <div className="flex flex-col border rounded py-3 px-4 gap-3 bg-gray-50 dark:bg-gray-800"> |
| 104 | + <div className="font-semibold text-sm">SSH Configuration:</div> |
| 105 | + |
| 106 | + <div className="flex items-center"> |
| 107 | + <label className="font-medium min-w-[120px] text-sm"> |
| 108 | + Host: |
| 109 | + </label> |
| 110 | + {selectedProfile.SSH?.HOST ? ( |
| 111 | + <div className="ml-2">{selectedProfile.SSH.HOST}</div> |
| 112 | + ) : ( |
| 113 | + <input |
| 114 | + type="text" |
| 115 | + className="ml-2 px-2 py-1 border rounded text-sm flex-1" |
| 116 | + placeholder="Enter SSH host" |
| 117 | + value={runnerConfig.SSH?.HOST || ""} |
| 118 | + onChange={(e) => handleSSHChange("HOST", e.target.value)} |
| 119 | + /> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className="flex items-center"> |
| 124 | + <label className="font-medium min-w-[120px] text-sm"> |
| 125 | + Username: |
| 126 | + </label> |
| 127 | + {selectedProfile.SSH?.USERNAME ? ( |
| 128 | + <div className="ml-2">{selectedProfile.SSH.USERNAME}</div> |
| 129 | + ) : ( |
| 130 | + <input |
| 131 | + type="text" |
| 132 | + className="ml-2 px-2 py-1 border rounded text-sm flex-1" |
| 133 | + placeholder="Enter SSH username" |
| 134 | + value={runnerConfig.SSH?.USERNAME || ""} |
| 135 | + onChange={(e) => |
| 136 | + handleSSHChange("USERNAME", e.target.value) |
| 137 | + } |
| 138 | + /> |
| 139 | + )} |
| 140 | + </div> |
| 141 | + |
| 142 | + <div className="flex items-center"> |
| 143 | + <label className="font-medium min-w-[120px] text-sm"> |
| 144 | + Port: |
| 145 | + </label> |
| 146 | + {selectedProfile.SSH?.PORT ? ( |
| 147 | + <div className="ml-2">{selectedProfile.SSH.PORT}</div> |
| 148 | + ) : ( |
| 149 | + <input |
| 150 | + type="number" |
| 151 | + className="ml-2 px-2 py-1 border rounded text-sm w-24" |
| 152 | + placeholder="22" |
| 153 | + value={runnerConfig.SSH?.PORT || ""} |
| 154 | + onChange={(e) => handleSSHChange("PORT", e.target.value)} |
| 155 | + /> |
| 156 | + )} |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + )} |
| 160 | + |
| 161 | + <div className="flex items-center"> |
| 162 | + <label className="font-semibold min-w-[150px]"> |
| 163 | + Module Loader type: |
| 164 | + </label> |
| 165 | + {selectedProfile.MODULE_LOADER_TYPE ? ( |
| 166 | + <div className="ml-2">{selectedProfile.MODULE_LOADER_TYPE}</div> |
| 167 | + ) : ( |
| 168 | + <input |
| 169 | + type="text" |
| 170 | + className="ml-2 px-2 py-1 border rounded text-sm flex-1" |
| 171 | + placeholder="Enter module loader type" |
| 172 | + value={runnerConfig.MODULE_LOADER_TYPE || ""} |
| 173 | + onChange={(e) => |
| 174 | + handleConfigChange("MODULE_LOADER_TYPE", e.target.value) |
| 175 | + } |
| 176 | + /> |
| 177 | + )} |
| 178 | + </div> |
| 179 | + |
| 180 | + {hasModules && ( |
| 181 | + <div className="flex items-center"> |
| 182 | + <label className="font-semibold min-w-[150px]">Modules:</label> |
| 183 | + {selectedProfile.MODULES ? ( |
| 184 | + <div className="ml-2"> |
| 185 | + {Array.isArray(selectedProfile.MODULES) |
| 186 | + ? selectedProfile.MODULES.join(", ") |
| 187 | + : selectedProfile.MODULES} |
| 188 | + </div> |
| 189 | + ) : ( |
| 190 | + <input |
| 191 | + type="text" |
| 192 | + className="ml-2 px-2 py-1 border rounded text-sm flex-1" |
| 193 | + placeholder="Enter modules to load" |
| 194 | + value={runnerConfig.MODULES || ""} |
| 195 | + onChange={(e) => |
| 196 | + handleConfigChange("MODULES", e.target.value) |
| 197 | + } |
| 198 | + /> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + )} |
| 202 | + </div> |
| 203 | + )} |
| 204 | + </div> |
| 205 | + ); |
| 206 | +}; |
0 commit comments