MCPcopy Create free account
hub / github.com/Botloader/botloader / ScriptSettings

Function ScriptSettings

frontend/src/components/GuildScriptPage.tsx:176–323  ·  view source on GitHub ↗
({ script }: { script: Script })

Source from the content-addressed store, hash-verified

174}
175
176function ScriptSettings({ script }: { script: Script }) {
177 const session = useSession()
178 const currentGuildId = useCurrentGuildId()
179 const [newValues, setNewValues] = useState<SettingsOptionValue[]>(() => {
180 return stripUnknownFields(script)
181 })
182
183 const [isSubmitting, setIsSubmitting] = useState(false)
184 const [isValidating, setIsValidating] = useState(false)
185 const [apiErrorResponse, setApiErrorResponse] = useState<ApiError | null>(null)
186 const notifications = UseNotifications()
187
188 const isDirty = useMemo(() => {
189 return JSON.stringify(newValues) !== JSON.stringify(script.settings_values)
190 }, [newValues, script])
191
192 const settingsHook: SettingsValuesHook = {
193 getFieldValue(name) {
194 return newValues.find(v => v.name === name)
195 },
196 setFieldValue(name, value) {
197 setNewValues(current => {
198 const copy = [...current]
199 const currentIndex = copy.findIndex(v => name === v.name)
200 if (currentIndex !== -1) {
201 if (value === null) {
202 copy.splice(currentIndex, 1)
203 } else {
204 copy[currentIndex] = { name, value }
205 }
206 } else if (value !== null) {
207 copy.push({ name, value })
208 }
209
210 return copy
211 })
212 },
213 getFieldError(name) {
214 const prefix = "settings_values."
215 if (apiErrorResponse) {
216 const error = apiErrorResponse.getFieldError(prefix + name)
217 return error
218 }
219 },
220 }
221
222 async function save() {
223 const response = await session.apiClient.updateScript(currentGuildId, script.id, {
224 settings_values: newValues
225 })
226
227 if (isErrorResponse(response)) {
228 notifications.push({
229 class: "error",
230 message: "Validation errors occurred"
231 })
232
233 setApiErrorResponse(response)

Callers

nothing calls this directly

Calls 6

useSessionFunction · 0.90
useCurrentGuildIdFunction · 0.90
UseNotificationsFunction · 0.90
stripUnknownFieldsFunction · 0.85
validateScriptFunction · 0.85
saveFunction · 0.70

Tested by

no test coverage detected