Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed Mar 20, 2024
1 parent 1ddee47 commit 3739197
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import React from 'react'
import { OperationResult } from 'urql'

import { SKELETON_DELAY } from '@/lib/constants'
import { graphql } from '@/lib/gql/generates'
import { EmailSettingQuery } from '@/lib/gql/generates/graphql'
import { useDebounceValue } from '@/lib/hooks/use-debounce'
import { client } from '@/lib/tabby/gql'
import { ListSkeleton } from '@/components/skeleton'

Expand Down Expand Up @@ -34,9 +32,6 @@ export const Mail = () => {
const [queryResult, setQueryResult] =
React.useState<OperationResult<EmailSettingQuery, any>>()
const [initialized, setInitialized] = React.useState(false)
const [debouncedInitialzed] = useDebounceValue(initialized, SKELETON_DELAY, {
leading: true
})
const mailFormRef = React.useRef<MailFormRef>(null)

const queryEmailSettings = () => {
Expand Down Expand Up @@ -85,7 +80,7 @@ export const Mail = () => {
return (
<>
<MailDeliveryHeader />
{debouncedInitialzed ? (
{initialized ? (
<div>
<div className="mb-8 border-b pb-4">
<MailForm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import * as z from 'zod'

import { SKELETON_DELAY } from '@/lib/constants'
import { graphql } from '@/lib/gql/generates'
import { useDebounceValue } from '@/lib/hooks/use-debounce'
import { useNetworkSetting } from '@/lib/hooks/use-network-setting'
import { useMutation } from '@/lib/tabby/gql'
import { Button } from '@/components/ui/button'
Expand Down Expand Up @@ -44,14 +42,8 @@ interface NetworkFormProps {

const NetworkForm: React.FC<NetworkFormProps> = ({
onSuccess,
defaultValues: propsDefaultValues
defaultValues
}) => {
const defaultValues = React.useMemo(() => {
return {
...(propsDefaultValues || {})
}
}, [propsDefaultValues])

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues
Expand Down Expand Up @@ -115,25 +107,16 @@ const NetworkForm: React.FC<NetworkFormProps> = ({
}

export const GeneralNetworkForm = () => {
const [queryState, reexecuteQuery] = useNetworkSetting({
const [{ data, stale }, reexecuteQuery] = useNetworkSetting({
requestPolicy: 'network-only'
})

const { data, fetching, stale } = queryState

// Use `useDebounceValue` to avoid flicker
const [shouldInitializeForm] = useDebounceValue(
// Combine three variables to determine and ensure not to use cache.
data && !stale && !fetching,
SKELETON_DELAY
)

const onSuccess = () => {
toast.success('Network configuration is updated')
reexecuteQuery()
}

return shouldInitializeForm && data ? (
return data && !stale ? (
<NetworkForm defaultValues={data.networkSetting} onSuccess={onSuccess} />
) : (
<FormSkeleton />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { toast } from 'sonner'
import { useQuery } from 'urql'
import * as z from 'zod'

import { SKELETON_DELAY } from '@/lib/constants'
import { graphql } from '@/lib/gql/generates'
import { LicenseType } from '@/lib/gql/generates/graphql'
import { useDebounceValue } from '@/lib/hooks/use-debounce'
import { useMutation } from '@/lib/tabby/gql'
import { cn } from '@/lib/utils'
import { Button } from '@/components/ui/button'
Expand Down Expand Up @@ -244,9 +242,7 @@ export const GeneralSecurityForm = () => {
)
}

const [shouldInitializeForm] = useDebounceValue(!!data, SKELETON_DELAY)

return shouldInitializeForm ? (
return data ? (
<SecurityForm defaultValues={defaultValues} onSuccess={onSuccess} />
) : (
<FormSkeleton />
Expand Down
2 changes: 0 additions & 2 deletions ee/tabby-ui/lib/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const PLACEHOLDER_EMAIL_FORM = '[email protected]'

export const DEFAULT_PAGE_SIZE = 20

export const SKELETON_DELAY = 300

0 comments on commit 3739197

Please sign in to comment.