Skip to content

Commit

Permalink
chore: Add debug controls to playground
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 committed Nov 8, 2023
1 parent 739ab7a commit 5ad750c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/playground/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dynamic from 'next/dynamic'
import React, { Suspense } from 'react'
import { HydrationMarker } from '../components/hydration-marker'

Expand All @@ -7,6 +8,18 @@ export const metadata = {
'useQueryState hook for Next.js - Like React.useState, but stored in the URL query string'
}

const DebugControlsSkeleton = () => (
<span style={{ opacity: 0.5, pointerEvents: 'none' }}>
<input type="checkbox" disabled />
<label>Console debugging</label>
</span>
)

const DebugControl = dynamic(() => import('../components/debug-control'), {
ssr: false,
loading: DebugControlsSkeleton
})

export default function RootLayout({
children
}: {
Expand All @@ -27,6 +40,10 @@ export default function RootLayout({
<a href="https://francoisbest.com/posts/2023/storing-react-state-in-the-url-with-nextjs">
How it works
</a>
{' • '}
<Suspense fallback={<DebugControlsSkeleton />}>
<DebugControl />
</Suspense>
</header>
<hr />
{children}
Expand Down
34 changes: 34 additions & 0 deletions packages/playground/src/components/debug-control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use client'

import React from 'react'

export default function DebugControl() {
const [checked, setChecked] = React.useState(() => {
if (typeof localStorage === 'undefined') {
return false
}
return (
localStorage.getItem('debug')?.includes('next-usequerystate') ?? false
)
})
const update = React.useCallback(() => {
setChecked(c => {
const checked = !c
if (typeof localStorage !== 'undefined') {
if (checked) {
localStorage.setItem('debug', 'next-usequerystate')
} else {
localStorage.removeItem('debug')
}
}
return checked
})
}, [])

return (
<span>
<input type="checkbox" checked={checked} onChange={update} />
<label>Console debugging</label>
</span>
)
}

0 comments on commit 5ad750c

Please sign in to comment.