-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
||
|
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) | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<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, | ||
|
@@ -64,23 +60,6 @@ export const SettingsDialog = ({ open, onOpenChange }: SettingsDialogProps) => { | |
formState: { isDirty, errors }, | ||
} = formMethods | ||
|
||
const onSubmit = async (data: AppSettings) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => | ||
|
@@ -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> | ||
|
There was a problem hiding this comment.
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