From d0212e84c6d03459cc03b8c015c72df17910299e Mon Sep 17 00:00:00 2001 From: Brendan Viscomi Date: Sat, 24 Aug 2024 13:53:32 +0000 Subject: [PATCH] Support searching links in command menu --- frontend/src/components/CommandMenu.tsx | 18 ++++++- frontend/src/components/SearchBar.tsx | 64 ++++++++++++++++--------- frontend/src/components/pages/Home.tsx | 19 ++++++-- frontend/src/lib/urls.ts | 1 + 4 files changed, 74 insertions(+), 28 deletions(-) diff --git a/frontend/src/components/CommandMenu.tsx b/frontend/src/components/CommandMenu.tsx index 8f267ca..b9eb7be 100644 --- a/frontend/src/components/CommandMenu.tsx +++ b/frontend/src/components/CommandMenu.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from "react"; +import React, { useCallback, useState } from "react"; import { useNavigate } from "react-router-dom"; import { CommandDialog, @@ -23,6 +23,7 @@ const CommandMenu: React.FC = ({ customCommands, }) => { const navigate = useNavigate(); + const [search, setSearch] = useState(""); const runCommand = (command: () => void) => { onOpenChange(false); command(); @@ -54,7 +55,11 @@ const CommandMenu: React.FC = ({ return ( - + No results found. {(customCommands || []).map((group) => ( @@ -75,6 +80,15 @@ const CommandMenu: React.FC = ({ Feeds Add Link + + + runCommand(() => navigate(URLS.HOME_WITH_SEARCH_STRING(search))) + } + > + Search for "{search}" + + ); diff --git a/frontend/src/components/SearchBar.tsx b/frontend/src/components/SearchBar.tsx index 0d51f66..11b161a 100644 --- a/frontend/src/components/SearchBar.tsx +++ b/frontend/src/components/SearchBar.tsx @@ -19,18 +19,16 @@ export type SearchParams = { }; type SearchBarProps = { + searchParams: SearchParams; onSearchParamsChange: (params: SearchParams) => void; }; -const SearchBar: React.FC = ({ onSearchParamsChange }) => { +const SearchBar: React.FC = ({ + searchParams, + onSearchParamsChange, +}) => { const [filterOpen, setFilterOpen] = useState(false); const [sortOpen, setSortOpen] = useState(false); - const [searchParams, setSearchParams] = useState({ - searchText: "", - readState: "all", - tagId: undefined, - sortBy: "added_to_library", - }); const [tags, setTags] = useState<{ id: string; name: string }[]>([]); const { pb } = usePocketBase(); @@ -53,7 +51,6 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { const updateSearchParams = (newParams: Partial) => { const updatedParams = { ...searchParams, ...newParams }; - setSearchParams(updatedParams); onSearchParamsChange(updatedParams); }; @@ -64,7 +61,6 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { tagId: undefined, sortBy: "added_to_library", }; - setSearchParams(clearedParams); onSearchParamsChange(clearedParams); }; @@ -79,9 +75,8 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { { value: "article_date", label: "Article Date" }, ]; - const activeFiltersCount = - (searchParams.readState !== "all" ? 1 : 0) + - (searchParams.tagId ? 1 : 0); + const activeFiltersCount = + (searchParams.readState !== "all" ? 1 : 0) + (searchParams.tagId ? 1 : 0); return (
@@ -99,7 +94,10 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { Filters {activeFiltersCount > 0 && ( - + {activeFiltersCount} )} @@ -113,12 +111,18 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { key={state.value} variant="ghost" className="w-full justify-start" - onClick={() => updateSearchParams({ readState: state.value as "all" | "read" | "unread" })} + onClick={() => + updateSearchParams({ + readState: state.value as "all" | "read" | "unread", + }) + } > {state.label} @@ -130,12 +134,18 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { key={tag.id} variant="ghost" className="w-full justify-start" - onClick={() => updateSearchParams({ tagId: searchParams.tagId === tag.id ? undefined : tag.id })} + onClick={() => + updateSearchParams({ + tagId: searchParams.tagId === tag.id ? undefined : tag.id, + }) + } > {tag.name} @@ -160,14 +170,20 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { variant="ghost" className="w-full justify-start" onClick={() => { - updateSearchParams({ sortBy: option.value as "added_to_library" | "article_date" }); + updateSearchParams({ + sortBy: option.value as + | "added_to_library" + | "article_date", + }); setSortOpen(false); }} > {option.label} @@ -198,14 +214,18 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { )} {searchParams.sortBy !== "added_to_library" && ( - Sorted by: {searchParams.sortBy === "article_date" ? "Article Date" : "Date Added"} + Sorted by:{" "} + {searchParams.sortBy === "article_date" + ? "Article Date" + : "Date Added"} updateSearchParams({ sortBy: "added_to_library" })} /> )} - {(activeFiltersCount > 0 || searchParams.sortBy !== "added_to_library") && ( + {(activeFiltersCount > 0 || + searchParams.sortBy !== "added_to_library") && ( @@ -215,4 +235,4 @@ const SearchBar: React.FC = ({ onSearchParamsChange }) => { ); }; -export default SearchBar; \ No newline at end of file +export default SearchBar; diff --git a/frontend/src/components/pages/Home.tsx b/frontend/src/components/pages/Home.tsx index 4d4f04f..337b869 100644 --- a/frontend/src/components/pages/Home.tsx +++ b/frontend/src/components/pages/Home.tsx @@ -7,15 +7,19 @@ import SearchBar, { SearchParams } from "@/components/SearchBar"; import Paginator from "@/components/Paginator"; import { usePageTitle } from "@/hooks/usePageTitle"; import { Toaster } from "@/components/ui/toaster"; +import { useSearchParams } from "react-router-dom"; const Home: React.FC = () => { - usePageTitle("My Feed") + usePageTitle("My Feed"); + const [urlParams, setUrlParams] = useSearchParams(); const [page, setPage] = useState(1); - const [searchParams, setSearchParams] = useState({ - searchText: "", + const [searchParams, setSearchParams] = useState< + Omit + >({ readState: "all", sortBy: "added_to_library", }); + const searchText = urlParams.get("s") || ""; const { loading: feedLoading, @@ -25,10 +29,14 @@ const Home: React.FC = () => { } = useLinksFeedQuery({ page, ...searchParams, + searchText, }); const handleSearchParamsChange = (newSearchParams: SearchParams) => { setSearchParams(newSearchParams); + setUrlParams({ + s: newSearchParams.searchText, + }); setPage(1); }; @@ -76,7 +84,10 @@ const Home: React.FC = () => { <>
- +
{renderContent()}
diff --git a/frontend/src/lib/urls.ts b/frontend/src/lib/urls.ts index e24455a..68bcf64 100644 --- a/frontend/src/lib/urls.ts +++ b/frontend/src/lib/urls.ts @@ -1,5 +1,6 @@ export default { HOME: "/", + HOME_WITH_SEARCH_STRING: (search: string) => `/?s=${search}`, LOGIN: "/login", PROFILE: "/profile", FEEDS: "/feeds",