generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Update throttling example with user delays
- Loading branch information
Showing
3 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
103 changes: 91 additions & 12 deletions
103
packages/playground/src/app/demos/throttling/client.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,102 @@ | ||
'use client' | ||
|
||
import { parseAsString, useQueryState } from 'next-usequerystate' | ||
import { useQueryState } from 'next-usequerystate' | ||
import { useRouter } from 'next/navigation' | ||
import React from 'react' | ||
import { delayParser, queryParser } from './parsers' | ||
|
||
const autoFillMessage = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor.` | ||
|
||
export function Client() { | ||
const [q, setQ] = useQueryState( | ||
'q', | ||
parseAsString | ||
.withOptions({ throttleMs: 350, shallow: false }) | ||
.withDefault('') | ||
const [serverDelay, setServerDelay] = useQueryState('serverDelay', { | ||
...delayParser, | ||
shallow: false | ||
}) | ||
const [clientDelay, setClientDelay] = useQueryState( | ||
'clientDelay', | ||
delayParser | ||
) | ||
const [q, setQ] = useQueryState('q', { | ||
...queryParser, | ||
throttleMs: clientDelay, | ||
shallow: false | ||
}) | ||
const router = useRouter() | ||
|
||
const timeoutRef = React.useRef<number>() | ||
const [index, setIndex] = React.useState(0) | ||
|
||
React.useEffect(() => { | ||
if (index === 0) { | ||
return | ||
} | ||
setQ(autoFillMessage.slice(0, index)) | ||
clearTimeout(timeoutRef.current) | ||
if (index === autoFillMessage.length) { | ||
return | ||
} | ||
timeoutRef.current = window.setTimeout(() => { | ||
setIndex(i => Math.min(i + 1, autoFillMessage.length)) | ||
}, 80) | ||
}, [index]) | ||
|
||
return ( | ||
<> | ||
<input | ||
value={q} | ||
onChange={e => setQ(e.target.value)} | ||
placeholder="Search" | ||
/> | ||
<p>Query: {q || <em>empty</em>}</p> | ||
<h2>Client</h2> | ||
<div> | ||
<label>Server latency simulation </label> | ||
<select | ||
value={serverDelay} | ||
onChange={e => | ||
setServerDelay(parseInt(e.target.value)).then(() => | ||
router.refresh() | ||
) | ||
} | ||
> | ||
<option value="0">No delay</option> | ||
<option value="100">100ms</option> | ||
<option value="200">200ms</option> | ||
<option value="500">500ms</option> | ||
<option value="1000">1s</option> | ||
</select> | ||
</div> | ||
<div> | ||
<label>Throttle URL updates at </label> | ||
<select | ||
value={clientDelay} | ||
onChange={e => setClientDelay(parseInt(e.target.value))} | ||
> | ||
<option value="50">Default (50ms)</option> | ||
<option value="100">100ms</option> | ||
<option value="200">200ms</option> | ||
<option value="500">500ms</option> | ||
<option value="1000">1s</option> | ||
</select> | ||
</div> | ||
<br /> | ||
<div> | ||
<label>Query </label> | ||
<input | ||
value={q} | ||
onChange={e => setQ(e.target.value)} | ||
placeholder="Search" | ||
/> | ||
{timeoutRef.current ? ( | ||
<button | ||
onClick={() => { | ||
setIndex(0) | ||
clearTimeout(timeoutRef.current) | ||
timeoutRef.current = undefined | ||
setQ(null) | ||
}} | ||
> | ||
Cancel | ||
</button> | ||
) : ( | ||
<button onClick={() => setIndex(1)}>Simulate typing</button> | ||
)} | ||
<p>Client state: {q || <em>empty</em>}</p> | ||
</div> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { parseAsInteger, parseAsString } from 'next-usequerystate/parsers' | ||
|
||
export const delayParser = parseAsInteger.withDefault(0) | ||
export const queryParser = parseAsString.withDefault('') |