Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

feat(search): add advanced search operators and suggestions to SearchBar component

daniel31x130198a9148e28
diff --git a/apps/web/components/DashboardLayoutDropdown.tsx b/apps/web/components/DashboardLayoutDropdown.tsx
index 8a77fabd..d1e18bf0 100644
--- a/apps/web/components/DashboardLayoutDropdown.tsx
+++ b/apps/web/components/DashboardLayoutDropdown.tsx
@@ -274,7 +274,7 @@ export default function DashboardLayoutDropdown() {
>
<div className="flex flex-col gap-1">
<div className="flex flex-col gap-1 mx-2">
- <p className="text-sm text-neutral mb-1">
+ <p className="text-xs font-bold text-neutral mb-1">
{t("display_on_dashboard")}
</p>
diff --git a/apps/web/components/SearchBar.tsx b/apps/web/components/SearchBar.tsx
index 268e9813..46267de5 100644
--- a/apps/web/components/SearchBar.tsx
+++ b/apps/web/components/SearchBar.tsx
@@ -1,16 +1,79 @@
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
+import Link from "next/link";
import { useRouter } from "next/router";
import { toast } from "react-hot-toast";
import { useTranslation } from "next-i18next";
+import { Button } from "@/components/ui/button";
type Props = {
placeholder?: string;
};
+const ADVANCED_SEARCH_OPERATORS = [
+ {
+ operator: "name:",
+ labelKey: "search_operator_name",
+ icon: "bi-type",
+ },
+ {
+ operator: "url:",
+ labelKey: "search_operator_url",
+ icon: "bi-link-45deg",
+ },
+ {
+ operator: "tag:",
+ labelKey: "search_operator_tag",
+ icon: "bi-tag",
+ },
+ {
+ operator: "collection:",
+ labelKey: "search_operator_collection",
+ icon: "bi-folder2",
+ },
+ {
+ operator: "before:",
+ labelKey: "search_operator_before",
+ icon: "bi-calendar-minus",
+ },
+ {
+ operator: "after:",
+ labelKey: "search_operator_after",
+ icon: "bi-calendar-plus",
+ },
+ {
+ operator: "public:true",
+ labelKey: "search_operator_public",
+ icon: "bi-globe2",
+ },
+ {
+ operator: "description:",
+ labelKey: "search_operator_description",
+ icon: "bi-card-text",
+ },
+ {
+ operator: "type:",
+ labelKey: "search_operator_type",
+ icon: "bi-file-earmark",
+ },
+ {
+ operator: "pinned:true",
+ labelKey: "search_operator_pinned",
+ icon: "bi-pin-angle",
+ },
+ {
+ operator: "!",
+ labelKey: "search_operator_exclude",
+ icon: "bi-slash-circle",
+ },
+] as const;
+
export default function SearchBar({ placeholder }: Props) {
const router = useRouter();
const { t } = useTranslation();
const [searchQuery, setSearchQuery] = useState("");
+ const [isFocused, setIsFocused] = useState(false);
+ const [showSuggestions, setShowSuggestions] = useState(false);
+ const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
router.query.q
@@ -18,8 +81,17 @@ export default function SearchBar({ placeholder }: Props) {
: setSearchQuery("");
}, [router.query.q]);
+ const handleSuggestionClick = (operator: string) => {
+ setSearchQuery((prev) => {
+ const needsSpace = prev.length > 0 && !prev.endsWith(" ");
+ return `${prev}${needsSpace ? " " : ""}${operator}`;
+ });
+ setShowSuggestions(false);
+ requestAnimationFrame(() => inputRef.current?.focus());
+ };
+
return (
- <div className="flex items-center relative group">
+ <div className="flex items-center relative group z-50">
<label
htmlFor="search-box"
className="inline-flex items-center w-fit absolute left-1 pointer-events-none rounded-md p-1 text-primary"
@@ -30,8 +102,17 @@ export default function SearchBar({ placeholder }: Props) {
<input
id="search-box"
type="text"
+ ref={inputRef}
placeholder={placeholder || t("search_for_links")}
value={searchQuery}
+ onFocus={() => {
+ setIsFocused(true);
+ setShowSuggestions(true);
+ }}
+ onBlur={() => {
+ setIsFocused(false);
+ setShowSuggestions(false);
+ }}
onChange={(e) => {
e.target.value.includes("%") &&
toast.error(t("search_query_invalid_symbol"));
@@ -57,9 +138,55 @@ export default function SearchBar({ placeholder }: Props) {
}
}
}}
- style={{ transition: "width 0.2s ease-in-out" }}
- className="border border-neutral-content bg-base-200 focus:border-primary py-1 rounded-md pl-9 pr-2 w-full max-w-[15rem] md:focus:w-80 md:w-[15rem] md:max-w-full outline-none"
+ className="border border-neutral-content bg-base-200 focus:border-primary py-1 rounded-md pl-9 pr-2 w-full max-w-[15rem] md:w-80 md:max-w-full outline-none"
/>
+
+ {showSuggestions && (
+ <div className="absolute left-0 top-full mt-2 w-full">
+ <div
+ className="border border-neutral-content bg-base-200 shadow-md rounded-md px-2 py-1 flex flex-col gap-1"
+ onMouseDown={(e) => e.preventDefault()}
+ >
+ <div className="flex items-center justify-between">
+ <p className="text-xs font-bold text-neutral">
+ {t("search_operators")}
+ </p>
+ </div>
+ <div className="flex flex-col gap-1">
+ {ADVANCED_SEARCH_OPERATORS.map((entry) => (
+ <button
+ key={entry.operator}
+ type="button"
+ className="flex items-center gap-2 justify-between rounded-md px-2 py-1 text-left hover:bg-base-100 duration-100"
+ onClick={() => handleSuggestionClick(entry.operator)}
+ >
+ <div className="flex items-center gap-2">
+ <i className={`${entry.icon} text-primary text-sm`} />
+ <span className="text-xs text-neutral">
+ {t(entry.labelKey)}
+ </span>
+ </div>
+ <span className="font-mono text-xs px-1 rounded-md bg-base-100 border border-neutral-content text-base-content">
+ {entry.operator}
+ </span>
+ </button>
+ ))}
+ </div>
+ <div className="flex justify-end">
+ <Button asChild variant="simple" size="sm" className="text-xs">
+ <Link
+ href="https://docs.linkwarden.app/Usage/advanced-search"
+ target="_blank"
+ className="flex items-center gap-1"
+ >
+ {t("learn_more")}
+ <i className="bi-box-arrow-up-right text-xs" />
+ </Link>
+ </Button>
+ </div>
+ </div>
+ </div>
+ )}
</div>
);
}
diff --git a/apps/web/components/ViewDropdown.tsx b/apps/web/components/ViewDropdown.tsx
index 133ba3fb..b3b7f554 100644
--- a/apps/web/components/ViewDropdown.tsx
+++ b/apps/web/components/ViewDropdown.tsx
@@ -72,7 +72,7 @@ export default function ViewDropdown({
{!dashboard && (
<>
<div className="px-1">
- <p className="text-sm text-neutral mb-1">{t("view")}</p>
+ <p className="text-xs font-bold text-neutral mb-1">{t("view")}</p>
<div className="flex gap-1 border-border">
{[ViewMode.Card, ViewMode.Masonry, ViewMode.List].map(
(mode) => {
@@ -112,7 +112,7 @@ export default function ViewDropdown({
</>
)}
- <p className="text-sm text-neutral px-1 mb-1">{t("show")}</p>
+ <p className="text-xs font-bold text-neutral px-1 mb-1">{t("show")}</p>
{visibleShows.map((key) => (
<DropdownMenuCheckboxItem
key={key}
@@ -131,7 +131,7 @@ export default function ViewDropdown({
<DropdownMenuSeparator />
<div className="px-1">
- <p className="text-sm text-neutral mb-1">
+ <p className="text-xs font-bold text-neutral mb-1">
{t("columns")}:{" "}
{settings.columns === 0 ? t("default") : settings.columns}
</p>
diff --git a/apps/web/public/locales/en/common.json b/apps/web/public/locales/en/common.json
index d70ee739..2594327f 100644
--- a/apps/web/public/locales/en/common.json
+++ b/apps/web/public/locales/en/common.json
@@ -463,6 +463,18 @@
"collection_publicly_shared": "This collection is being shared publicly.",
"search_for_links": "Search for Links",
"search_query_invalid_symbol": "The search query should not contain '%'.",
+ "search_operators": "Search Operators",
+ "search_operator_name": "Title",
+ "search_operator_url": "URL",
+ "search_operator_tag": "Tag",
+ "search_operator_collection": "Collection",
+ "search_operator_before": "Before",
+ "search_operator_after": "After",
+ "search_operator_public": "Public",
+ "search_operator_description": "Description",
+ "search_operator_type": "Type",
+ "search_operator_pinned": "Pinned",
+ "search_operator_exclude": "Exclude",
"open_modal_new_tab": "Open this modal in a new tab",
"file": "File",
"tag_preservation_rule_label": "Preservation rules by tag (override global settings):",