-
Notifications
You must be signed in to change notification settings - Fork 8
feat: search on all pub page #1290
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
14 commits
Select commit
Hold shift + click to select a range
f633a09
feat: search on all pub page
tefkah c25c21f
fix: some layout + accessibility adjustments to list
tefkah 060833f
feat: properly track server update status for highlighting
tefkah 9384d35
fix: spacing in pubcard
tefkah 1687a1d
feat: add keyboard shortcut handler
tefkah 37c3406
fix: make pagination work properly when querying
tefkah 19b03bc
fix: fix lint and type errors
tefkah c3061a9
feat: add clear search input button
tefkah fe90cd1
fix: properly suspend a bunch of queries on the pub page
tefkah 648e5ac
fix: fix layout
tefkah 40432dd
fix: don't flash Ctrl -> Cmd on mac
tefkah 7d041da
fix: remove keyboard shortcut for sidebar
tefkah a173804
Merge branch 'main' into tfk/all-pubs-search
tefkah 575ba9b
Merge branch 'main' into tfk/all-pubs-search
3mcd 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| "use client"; | ||
|
|
||
| import React, { useDeferredValue, useEffect, useRef, useState } from "react"; | ||
| import { Search, X } from "lucide-react"; | ||
| import { parseAsInteger, parseAsString, useQueryStates } from "nuqs"; | ||
| import { useDebouncedCallback } from "use-debounce"; | ||
|
|
||
| import { KeyboardShortcutPriority, useKeyboardShortcut, usePlatformModifierKey } from "ui/hooks"; | ||
| import { Input } from "ui/input"; | ||
| import { cn } from "utils"; | ||
|
|
||
| type PubSearchProps = React.PropsWithChildren<{}>; | ||
|
|
||
| const DEBOUNCE_TIME = 300; | ||
|
|
||
| export const PubSearch = (props: PubSearchProps) => { | ||
| const [query, setQuery] = useQueryStates( | ||
| { | ||
| query: parseAsString.withDefault(""), | ||
| page: parseAsInteger.withDefault(1), | ||
| }, | ||
| { | ||
| shallow: false, | ||
| } | ||
| ); | ||
|
|
||
| // local input state for immediate UI responsiveness + sync with URL | ||
| // otherwise, when navigating back/forward or refreshing, the input will be empty | ||
| const [inputValue, setInputValue] = useState(query.query); | ||
|
|
||
| // deferred query to keep track of server updates | ||
| // without this, we can't only check if inputValue !== query.query, | ||
| // which only tells us that the debounce has happened | ||
| const deferredQuery = useDeferredValue(query.query); | ||
|
|
||
| const inputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| useKeyboardShortcut( | ||
| "Mod+k", | ||
| () => { | ||
| inputRef.current?.focus(); | ||
| inputRef.current?.select(); | ||
| }, | ||
| { | ||
| priority: KeyboardShortcutPriority.MEDIUM, | ||
| } | ||
| ); | ||
|
|
||
| // sync input with URL when navigating back/forward | ||
| useEffect(() => { | ||
| if (query.query === inputValue) { | ||
| return; | ||
| } | ||
| setInputValue(query.query); | ||
| }, [query.query]); | ||
|
|
||
| const { symbol, platform } = usePlatformModifierKey(); | ||
|
|
||
| const debouncedSetQuery = useDebouncedCallback((value: string) => { | ||
| if (value.length >= 2 || value.length === 0) { | ||
| setQuery({ query: value, page: 1 }); // reset to page 1 on new search | ||
| } | ||
| }, DEBOUNCE_TIME); | ||
|
|
||
| const handleClearInput = () => { | ||
| setInputValue(""); | ||
| setQuery({ query: "", page: 1 }); | ||
| }; | ||
|
|
||
| // determine if content is stale, in order to provide a visual feedback to the user | ||
| const isStale = inputValue !== deferredQuery; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col gap-4"> | ||
| <div className="sticky top-0 z-20 flex max-w-md items-center gap-x-2"> | ||
| <Search | ||
| className="absolute left-2 top-1/2 -translate-y-1/2 text-gray-500" | ||
| size={16} | ||
| /> | ||
| <Input | ||
| ref={inputRef} | ||
| value={inputValue} | ||
| onChange={(e) => { | ||
| setInputValue(e.target.value); | ||
| debouncedSetQuery(e.target.value); | ||
| }} | ||
| placeholder="Search updates as you type..." | ||
| className={cn("bg-white pl-8 tracking-wide shadow-none", inputValue && "pr-8")} | ||
| /> | ||
| <span className="absolute right-2 top-1/2 hidden -translate-y-1/2 items-center gap-x-2 font-mono text-xs text-gray-500 opacity-50 md:flex"> | ||
| {inputValue && ( | ||
| <button | ||
| onClick={handleClearInput} | ||
| className="rounded-full p-1 text-gray-500 hover:bg-gray-100 hover:text-gray-700 md:right-16" | ||
| type="button" | ||
| aria-label="Clear search" | ||
| > | ||
| <X size={14} /> | ||
| </button> | ||
| )} | ||
| <span | ||
| className={cn( | ||
| "flex w-10 items-center justify-center gap-x-1 transition-opacity duration-200", | ||
| { | ||
| // hide until hydrated, otherwise you see flash of `Ctrl` -> `Cmd` on mac | ||
| "opacity-0": platform === "unknown", | ||
| } | ||
| )} | ||
| > | ||
| <span className={cn({ "mt-0.5 text-lg": platform === "mac" })}> | ||
| {symbol} | ||
| </span>{" "} | ||
| K | ||
| </span> | ||
| </span> | ||
| </div> | ||
| <div className={cn(isStale && "opacity-50 transition-opacity duration-200")}> | ||
| {props.children} | ||
| </div> | ||
| </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
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.
this is kind of mysterious to me, but this allows me to keep the body of the search at half-opacity until the content has fully refreshed. this could also be done by using
useTransition.