From 7ebb02a520a5bfd0e7c7f7305a7a29631ef79c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uladzimir=20Dzmitra=C4=8Dko=C5=AD?= <3773351+going-confetti@users.noreply.github.com> Date: Wed, 18 Dec 2024 13:25:55 +0100 Subject: [PATCH] chore: Use tanstack query for settings (#384) --- src/components/Settings/LogsSettings.tsx | 8 ++--- src/components/Settings/Settings.hooks.ts | 26 ++++++++++++++ src/components/Settings/SettingsDialog.tsx | 40 ++++++---------------- 3 files changed, 41 insertions(+), 33 deletions(-) create mode 100644 src/components/Settings/Settings.hooks.ts diff --git a/src/components/Settings/LogsSettings.tsx b/src/components/Settings/LogsSettings.tsx index 83ee165f..d5ea034a 100644 --- a/src/components/Settings/LogsSettings.tsx +++ b/src/components/Settings/LogsSettings.tsx @@ -43,14 +43,14 @@ export function LogsSettings() { return ( - + - - k6 Studio logs in this screen are updated in real-time. + + Application logs are updated in real-time. diff --git a/src/components/Settings/Settings.hooks.ts b/src/components/Settings/Settings.hooks.ts new file mode 100644 index 00000000..56523987 --- /dev/null +++ b/src/components/Settings/Settings.hooks.ts @@ -0,0 +1,26 @@ +import { queryClient } from '@/utils/query' +import { useMutation, useQuery } from '@tanstack/react-query' + +export function useSettings() { + return useQuery({ + queryKey: ['settings'], + queryFn: window.studio.settings.getSettings, + }) +} + +export function useSaveSettings(onSuccess?: () => void) { + return useMutation({ + mutationFn: window.studio.settings.saveSettings, + onSuccess: async (isSuccessful) => { + if (!isSuccessful) { + return + } + + await queryClient.invalidateQueries({ queryKey: ['settings'] }) + onSuccess?.() + }, + onError: (error) => { + console.error('Error saving settings', error) + }, + }) +} diff --git a/src/components/Settings/SettingsDialog.tsx b/src/components/Settings/SettingsDialog.tsx index c53b6565..76189eca 100644 --- a/src/components/Settings/SettingsDialog.tsx +++ b/src/components/Settings/SettingsDialog.tsx @@ -3,7 +3,7 @@ import { zodResolver } from '@hookform/resolvers/zod' import { Box, Button, Dialog, Flex, ScrollArea, Tabs } from '@radix-ui/themes' import { ExclamationTriangleIcon } from '@radix-ui/react-icons' import { findIndex, sortBy } from 'lodash-es' -import { useEffect, useState } from 'react' +import { useState } from 'react' import { FormProvider, useForm } from 'react-hook-form' import { ProxySettings } from './ProxySettings' @@ -14,6 +14,7 @@ import { UsageReportSettings } from './UsageReportSettings' import { ButtonWithTooltip } from '../ButtonWithTooltip' import { AppearanceSettings } from './AppearanceSettings' import { LogsSettings } from './LogsSettings' +import { useSaveSettings, useSettings } from './Settings.hooks' type SettingsDialogProps = { open: boolean @@ -41,17 +42,12 @@ const tabs = [ ] export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => { - const [settings, setSettings] = useState() - const [submitting, setSubmitting] = useState(false) + const { data: settings } = useSettings() + const { mutateAsync: saveSettings, isPending } = useSaveSettings(() => { + onOpenChange(false) + }) const [selectedTab, setSelectedTab] = useState('proxy') - useEffect(() => { - ;(async function fetchSettings() { - const data = await window.studio.settings.getSettings() - setSettings(data) - })() - }, []) - const formMethods = useForm({ resolver: zodResolver(AppSettingsSchema), shouldFocusError: true, @@ -64,23 +60,6 @@ export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => { formState: { isDirty, errors }, } = formMethods - const onSubmit = async (data: AppSettings) => { - try { - setSubmitting(true) - const isSuccess = await window.studio.settings.saveSettings(data) - if (isSuccess) { - onOpenChange(false) - reset(data) - setSettings(data) - setSelectedTab('proxy') - } - } catch (error) { - console.error('Error saving settings', error) - } finally { - setSubmitting(false) - } - } - const onInvalid = (errors: Record) => { // Sort tabs by the order they appear in the UI const tabsWithError = sortBy(Object.keys(errors), (key) => @@ -158,10 +137,13 @@ export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => { saveSettings(data), + onInvalid + )} > Save changes