Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Use tanstack query for settings #384

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/components/Settings/LogsSettings.tsx
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but I think it'd be really nice to add basic syntax highlighting to logs
image

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>
Comment on lines +46 to 51
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it a bit more subtle, but I'm not quite happy with the spinner here: IMO it gives an indication that something is constantly fetching, even though it's not the case.

<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)
})
allansson marked this conversation as resolved.
Show resolved Hide resolved
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) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using tanstack query is more ergonomic and allows us to reused cached settings across different components

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
Loading