Skip to content

Commit

Permalink
chore: Use tanstack query for settings (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
going-confetti authored Dec 18, 2024
1 parent a936fbc commit 7ebb02a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/components/Settings/LogsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export function LogsSettings() {
return (
<SettingsSection>
<Flex mb="4" justify="between" align="center">
<Flex>
<Flex align="center">
<Spinner mr="2" />
<Text size="2">
k6 Studio logs in this screen are updated in real-time.
<Text size="2" color="gray">
Application logs are updated in real-time.
</Text>
</Flex>
<Button onClick={handleOpenLogClick} variant="outline">
Open log location
Open logs folder
</Button>
</Flex>

Expand Down
26 changes: 26 additions & 0 deletions src/components/Settings/Settings.hooks.ts
Original file line number Diff line number Diff line change
@@ -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)
},
})
}
40 changes: 11 additions & 29 deletions src/components/Settings/SettingsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -41,17 +42,12 @@ const tabs = [
]

export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => {
const [settings, setSettings] = useState<AppSettings>()
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<AppSettings>({
resolver: zodResolver(AppSettingsSchema),
shouldFocusError: true,
Expand All @@ -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<string, unknown>) => {
// Sort tabs by the order they appear in the UI
const tabsWithError = sortBy(Object.keys(errors), (key) =>
Expand Down Expand Up @@ -158,10 +137,13 @@ export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => {
</Dialog.Close>
<Dialog.Close>
<ButtonWithTooltip
loading={submitting}
loading={isPending}
disabled={!isDirty}
tooltip={!isDirty ? 'Changes saved' : ''}
onClick={handleSubmit(onSubmit, onInvalid)}
onClick={handleSubmit(
(data) => saveSettings(data),
onInvalid
)}
>
Save changes
</ButtonWithTooltip>
Expand Down

0 comments on commit 7ebb02a

Please sign in to comment.