generated from chiffre-io/template-library
-
-
Notifications
You must be signed in to change notification settings - Fork 256
feat: Add processUrlSearchParams middleware as adapter prop #1110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
135afcb
feat: process url search params middleware
Multiply 458663b
doc: Wording tweaks
franky47 98333ed
doc: Wording
franky47 222b05a
fix: Add processUrlSearchParams to SUF dep array
franky47 9221815
test: Add processUrlSearchParams reactivity test
franky47 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,127 @@ | ||
| 'use client' | ||
|
|
||
| import { Button } from '@/src/components/ui/button' | ||
| import { Checkbox } from '@/src/components/ui/checkbox' | ||
| import { Label } from '@/src/components/ui/label' | ||
| import { parseAsInteger, useQueryState } from 'nuqs' | ||
| import { NuqsAdapter } from 'nuqs/adapters/react' | ||
| import { useEffect, useState } from 'react' | ||
|
|
||
| export function DemoSkeleton() { | ||
| return ( | ||
| <figure className="flex animate-pulse flex-wrap justify-around gap-2 rounded-md border border-dashed p-2"> | ||
| <ComponentSkeleton /> | ||
| <ComponentSkeleton /> | ||
| <ComponentSkeleton /> | ||
| </figure> | ||
| ) | ||
| } | ||
|
|
||
| function sortAlphabetically(search: URLSearchParams): URLSearchParams { | ||
| const entries = Array.from(search.entries()) | ||
| entries.sort(([a], [b]) => a.localeCompare(b)) | ||
| return new URLSearchParams(entries) | ||
| } | ||
| function passThrough(search: URLSearchParams): URLSearchParams { | ||
| return search | ||
| } | ||
|
|
||
| export function AlphabeticalSortDemo() { | ||
| const [hydrated, setHydrated] = useState(false) | ||
| const [enableSorting, setEnableSorting] = useState(true) | ||
| useEffect(() => { | ||
| setHydrated(true) | ||
| }, []) | ||
|
|
||
| if (!hydrated) { | ||
| return <DemoSkeleton /> | ||
| } | ||
|
|
||
| return ( | ||
| <NuqsAdapter | ||
| processUrlSearchParams={enableSorting ? sortAlphabetically : passThrough} | ||
| > | ||
| <> | ||
| <Label className="flex items-center gap-2"> | ||
| <Checkbox | ||
| checked={enableSorting} | ||
| onCheckedChange={checked => setEnableSorting(checked === true)} | ||
| />{' '} | ||
| Enable alphabetical sorting on updates | ||
| </Label> | ||
| <figure className="not-prose mt-4 mb-8 flex flex-wrap justify-around gap-2 rounded-md border border-dashed p-2"> | ||
| <ComponentToggle id="a" /> | ||
| <ComponentToggle id="b" /> | ||
| <ComponentToggle id="c" /> | ||
| </figure> | ||
| </> | ||
| </NuqsAdapter> | ||
| ) | ||
| } | ||
|
|
||
| export function TimestampDemo() { | ||
| const [hydrated, setHydrated] = useState(false) | ||
| useEffect(() => { | ||
| setHydrated(true) | ||
| }, []) | ||
|
|
||
| if (!hydrated) { | ||
| return <DemoSkeleton /> | ||
| } | ||
|
|
||
| return ( | ||
| <NuqsAdapter | ||
| processUrlSearchParams={search => { | ||
| const params = new URLSearchParams(search) | ||
| params.set('ts', Date.now().toString()) | ||
| return params | ||
| }} | ||
| > | ||
| <figure className="flex flex-wrap justify-around gap-2 rounded-md border border-dashed p-2"> | ||
| <ComponentIncrement id="d" /> | ||
| <ComponentIncrement id="e" /> | ||
| <ComponentIncrement id="f" /> | ||
| </figure> | ||
| </NuqsAdapter> | ||
| ) | ||
| } | ||
|
|
||
| function ComponentIncrement({ id }: { id: string }) { | ||
| const [count, setCount] = useQueryState(id, parseAsInteger.withDefault(0)) | ||
|
|
||
| return ( | ||
| <div className="rounded-xl p-1.5"> | ||
| <Button | ||
| onClick={() => setCount(c => c + 1)} | ||
| className="min-w-42 tabular-nums" | ||
| > | ||
| Increment "{id}": {count} | ||
| </Button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function ComponentToggle({ id }: { id: string }) { | ||
| const [, setCount] = useQueryState(id, parseAsInteger.withDefault(0)) | ||
|
|
||
| return ( | ||
| <div className="rounded-xl p-1.5"> | ||
| <Button | ||
| onClick={() => setCount(c => (c ? 0 : 1))} | ||
| className="min-w-42 tabular-nums" | ||
| > | ||
| Toggle "{id}" | ||
| </Button> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| function ComponentSkeleton() { | ||
| return ( | ||
| <div className="rounded-xl p-1.5"> | ||
| <Button disabled className="min-w-42 tabular-nums"> | ||
| Loading demo... | ||
| </Button> | ||
| </div> | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was it considered to recommend the usage of the native
sort()function onURLSearchParams?Something along the lines of:
Whilst it isn't the exact same sorting algorithm, I was thinking maybe it'd be preferable to recommend following the standard by default!
I can update the docs if you agree 😄
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't care about immutability, simply call
search.sort()and returningsearchdirectly could also do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imagine building an entire library around URLSearchParams and only discovering the .sort function now 😅
Yes standards would be better to advertise in the docs. It's a shame it doesn't return itself though, that could have been a one-liner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1113