Skip to content

Commit

Permalink
feat: enhance DNS form initialization and UI components
Browse files Browse the repository at this point in the history
Refactor DNS form to initialize fields from URL search params dynamically, improving user experience by retaining form state across page reloads. Simplify DNS panel by removing unnecessary Suspense wrapper. Update UI components to use client-side rendering directives and refine input styles for better clarity.
  • Loading branch information
ccbikai committed Oct 11, 2024
1 parent 3e6f7e9 commit c2277e1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
21 changes: 14 additions & 7 deletions components/dns/dns-form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import { zodResolver } from '@hookform/resolvers/zod'

import isValidDomain from 'is-valid-domain'

import { useSearchParams } from 'next/navigation'

import { useState } from 'react'
import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form'
import * as z from 'zod'
import { DNSTypes } from './dns-type'
Expand All @@ -50,17 +48,26 @@ const formSchema = z.object({
})

export function DNSForm({ onSearch }) {
const searchParams = useSearchParams()
const form = useForm({
resolver: zodResolver(formSchema),
defaultValues: {
name: searchParams.get('name') || '',
type: searchParams.get('type') || 'A',
resolver: searchParams.get('resolver') || 'cloudflare',
name: '',
type: 'A',
resolver: 'cloudflare',
},
})
const [loading, setLoading] = useState(false)

useEffect(() => {
const values = form.getValues()

for (const key in values) {
const searchParams = new URL(document.location).searchParams
const queryValue = searchParams.get(key)
form.setValue(key, queryValue)
}
})

function changeName(e) {
const urlParser = z.string().url()
const result = urlParser.safeParse(e.target.value)
Expand Down
6 changes: 2 additions & 4 deletions components/dns/dns-panel.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import { Suspense, useState } from 'react'
import { useState } from 'react'
import { DNSFeature } from './dns-feature'
import { DNSForm } from './dns-form'
import { DNSTable } from './dns-table'
Expand All @@ -23,9 +23,7 @@ export default function DNSPanel() {
return (
<>
{/* <DNSHero/> */}
<Suspense>
<DNSForm onSearch={onSearch} />
</Suspense>
<DNSForm onSearch={onSearch} />
{
formData.name ? <DNSTable formData={formData} /> : <DNSFeature />
}
Expand Down
2 changes: 2 additions & 0 deletions components/ui/form.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { Slot } from "@radix-ui/react-slot"
Expand Down
2 changes: 1 addition & 1 deletion components/ui/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
Expand Down
2 changes: 2 additions & 0 deletions components/ui/label.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import * as React from "react"
import * as LabelPrimitive from "@radix-ui/react-label"
import { cva, type VariantProps } from "class-variance-authority"
Expand Down
2 changes: 2 additions & 0 deletions components/ui/select.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client"

import * as React from "react"
import * as SelectPrimitive from "@radix-ui/react-select"
import { Check, ChevronDown, ChevronUp } from "lucide-react"
Expand Down

0 comments on commit c2277e1

Please sign in to comment.