Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmegood committed Feb 1, 2024
1 parent 0758042 commit 1d76aa3
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 73 deletions.
2 changes: 1 addition & 1 deletion app/components/error-boundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function GeneralErrorBoundary({
? (statusHandlers?.[error.status] ?? defaultStatusHandler)({
error,
params,
})
})
: unexpectedErrorHandler(error)}
</div>
)
Expand Down
39 changes: 21 additions & 18 deletions app/components/forms.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useInputEvent } from '@conform-to/react'
import React, { useId, useRef } from 'react'
import { useInputControl } from '@conform-to/react'
import React, { useId } from 'react'
import { Checkbox, type CheckboxProps } from './ui/checkbox.tsx'
import { Input } from './ui/input.tsx'
import { Label } from './ui/label.tsx'
Expand Down Expand Up @@ -94,42 +94,45 @@ export function CheckboxField({
className,
}: {
labelProps: JSX.IntrinsicElements['label']
buttonProps: CheckboxProps
buttonProps: CheckboxProps & {
name: string
form: string
value?: string
}
errors?: ListOfErrors
className?: string
}) {
const { key, defaultChecked, ...checkboxProps } = buttonProps
const fallbackId = useId()
const buttonRef = useRef<HTMLButtonElement>(null)
// To emulate native events that Conform listen to:
// See https://conform.guide/integrations
const control = useInputEvent({
// Retrieve the checkbox element by name instead as Radix does not expose the internal checkbox element
// See https://github.com/radix-ui/primitives/discussions/874
ref: () =>
buttonRef.current?.form?.elements.namedItem(buttonProps.name ?? ''),
onFocus: () => buttonRef.current?.focus(),
const checkedValue = buttonProps.value ?? 'on'
const input = useInputControl({
key,
name: buttonProps.name,
formId: buttonProps.form,
initialValue: defaultChecked ? checkedValue : undefined,
})
const id = buttonProps.id ?? buttonProps.name ?? fallbackId
const id = buttonProps.id ?? fallbackId
const errorId = errors?.length ? `${id}-error` : undefined

return (
<div className={className}>
<div className="flex gap-2">
<Checkbox
{...checkboxProps}
id={id}
ref={buttonRef}
aria-invalid={errorId ? true : undefined}
aria-describedby={errorId}
{...buttonProps}
checked={input.value === checkedValue}
onCheckedChange={state => {
control.change(Boolean(state.valueOf()))
input.change(state.valueOf() ? checkedValue : '')
buttonProps.onCheckedChange?.(state)
}}
onFocus={event => {
control.focus()
input.focus()
buttonProps.onFocus?.(event)
}}
onBlur={event => {
control.blur()
input.blur()
buttonProps.onBlur?.(event)
}}
type="button"
Expand Down
31 changes: 15 additions & 16 deletions app/root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useForm } from '@conform-to/react'
import { parse } from '@conform-to/zod'
import { getFormProps, useForm } from '@conform-to/react'
import { invariantResponse } from '@epic-web/invariant'
import { cssBundleHref } from '@remix-run/css-bundle'
import {
json,
Expand All @@ -25,7 +25,6 @@ import { withSentry } from '@sentry/remix'
import { HoneypotProvider } from 'remix-utils/honeypot/react'
import { z } from 'zod'
import { GeneralErrorBoundary } from './components/error-boundary.tsx'
import { ErrorList } from './components/forms.tsx'
import { EpicProgress } from './components/progress-bar.tsx'
import { useToast } from './components/toaster.tsx'
import { Icon, href as iconsHref } from './components/ui/icon.tsx'
Expand All @@ -42,6 +41,7 @@ import { useRequestInfo } from './utils/request-info.ts'
import { type Theme, setTheme, getTheme } from './utils/theme.server.ts'
import { makeTimings, time } from './utils/timing.server.ts'
import { getToast } from './utils/toast.server.ts'
import { parseWithZod } from '@conform-to/zod'

export const links: LinksFunction = () => {
return [
Expand Down Expand Up @@ -153,21 +153,18 @@ const ThemeFormSchema = z.object({

export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData()
const submission = parse(formData, {
const submission = parseWithZod(formData, {
schema: ThemeFormSchema,
})
if (submission.intent !== 'submit') {
return json({ status: 'idle', submission } as const)
}
if (!submission.value) {
return json({ status: 'error', submission } as const, { status: 400 })
}

invariantResponse(submission.status === 'success', 'Invalid theme received')

const { theme } = submission.value

const responseInit = {
headers: { 'set-cookie': setTheme(theme) },
}
return json({ success: true, submission }, responseInit)
return json({ result: submission.reply() }, responseInit)
}

function Document({
Expand Down Expand Up @@ -282,10 +279,13 @@ export function useOptimisticThemeMode() {
const themeFetcher = fetchers.find(f => f.formAction === '/')

if (themeFetcher && themeFetcher.formData) {
const submission = parse(themeFetcher.formData, {
const submission = parseWithZod(themeFetcher.formData, {
schema: ThemeFormSchema,
})
return submission.value?.theme

if (submission.status === 'success') {
return submission.value.theme
}
}
}

Expand All @@ -294,7 +294,7 @@ function ThemeSwitch({ userPreference }: { userPreference?: Theme | null }) {

const [form] = useForm({
id: 'theme-switch',
lastSubmission: fetcher.data?.submission,
lastResult: fetcher.data?.result,
})

const optimisticMode = useOptimisticThemeMode()
Expand All @@ -320,7 +320,7 @@ function ThemeSwitch({ userPreference }: { userPreference?: Theme | null }) {
}

return (
<fetcher.Form method="POST" {...form.props}>
<fetcher.Form method="POST" {...getFormProps(form)}>
<input type="hidden" name="theme" value={nextMode} />
<div className="flex gap-2">
<button
Expand All @@ -330,7 +330,6 @@ function ThemeSwitch({ userPreference }: { userPreference?: Theme | null }) {
{modeLabel[mode]}
</button>
</div>
<ErrorList errors={form.errors} id={form.errorId} />
</fetcher.Form>
)
}
Expand Down
12 changes: 6 additions & 6 deletions app/utils/db.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const prisma = remember('prisma', () => {
e.duration < logThreshold * 1.1
? 'green'
: e.duration < logThreshold * 1.2
? 'blue'
: e.duration < logThreshold * 1.3
? 'yellow'
: e.duration < logThreshold * 1.4
? 'redBright'
: 'red'
? 'blue'
: e.duration < logThreshold * 1.3
? 'yellow'
: e.duration < logThreshold * 1.4
? 'redBright'
: 'red'
const dur = chalk[color](`${e.duration}ms`)
console.info(`prisma:query - ${dur} - ${e.query}`)
})
Expand Down
2 changes: 1 addition & 1 deletion app/utils/misc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function useDoubleCheck() {
: e => {
e.preventDefault()
setDoubleCheck(true)
}
}

const onKeyUp: React.ButtonHTMLAttributes<HTMLButtonElement>['onKeyUp'] =
e => {
Expand Down
54 changes: 28 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"/server-build"
],
"dependencies": {
"@conform-to/react": "^0.9.1",
"@conform-to/zod": "^0.9.1",
"@conform-to/react": "^1.0.0",
"@conform-to/zod": "^1.0.0",
"@epic-web/cachified": "^4.0.0",
"@epic-web/client-hints": "^1.2.2",
"@epic-web/invariant": "^1.0.0",
Expand Down Expand Up @@ -114,7 +114,7 @@
"@remix-run/serve": "^2.5.1",
"@remix-run/testing": "^2.5.1",
"@sly-cli/sly": "^1.8.0",
"@testing-library/jest-dom": "^6.4.0",
"@testing-library/jest-dom": "^6.4.1",
"@testing-library/react": "^14.2.0",
"@testing-library/user-event": "^14.5.2",
"@total-typescript/ts-reset": "^0.5.1",
Expand All @@ -127,9 +127,9 @@
"@types/fs-extra": "^11.0.4",
"@types/glob": "^8.1.0",
"@types/morgan": "^1.9.9",
"@types/node": "^20.11.13",
"@types/node": "^20.11.14",
"@types/qrcode": "^1.5.5",
"@types/react": "^18.2.48",
"@types/react": "^18.2.50",
"@types/react-dom": "^18.2.18",
"@types/set-cookie-parser": "^2.4.7",
"@types/source-map-support": "^0.5.10",
Expand Down

0 comments on commit 1d76aa3

Please sign in to comment.