Skip to content

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

feat(tags): implement search functionality for tags across mobile and web

daniel31x13694f8e7f48e6
diff --git a/apps/mobile/app.json b/apps/mobile/app.json
index ec519d95..e0e7a9df 100644
--- a/apps/mobile/app.json
+++ b/apps/mobile/app.json
@@ -2,7 +2,7 @@
"expo": {
"name": "Linkwarden",
"slug": "linkwarden",
- "version": "1.2.2",
+ "version": "1.2.3",
"orientation": "portrait",
"icon": "./assets/images/icon.png",
"scheme": "linkwarden",
diff --git a/apps/mobile/app/(tabs)/tags/index.tsx b/apps/mobile/app/(tabs)/tags/index.tsx
index e5722058..1d8e2d45 100644
--- a/apps/mobile/app/(tabs)/tags/index.tsx
+++ b/apps/mobile/app/(tabs)/tags/index.tsx
@@ -9,32 +9,48 @@ import {
import useAuthStore from "@/store/auth";
import TagListing from "@/components/TagListing";
import { useLocalSearchParams } from "expo-router";
-import React, { useEffect, useState } from "react";
+import React, { useMemo } from "react";
import Spinner from "@/components/ui/Spinner";
import { rawTheme, ThemeName } from "@/lib/colors";
import { useColorScheme } from "nativewind";
-import { TagIncludingLinkCount, TagSort } from "@linkwarden/types/global";
+import { TagSort } from "@linkwarden/types/global";
import { useTags } from "@linkwarden/router/tags";
+import { isAtLeastInstanceVersion, useConfig } from "@linkwarden/router/config";
+
+const MIN_TAG_SEARCH_VERSION = "2.14.1";
+
+const decodeSearchParam = (value?: string) => {
+ if (!value) return "";
+
+ try {
+ return decodeURIComponent(value);
+ } catch {
+ return value;
+ }
+};
export default function TagsScreen() {
const { colorScheme } = useColorScheme();
const { auth } = useAuthStore();
const { search } = useLocalSearchParams<{ search?: string }>();
+ const config = useConfig(auth);
+ const searchQuery = decodeSearchParam(search);
+ const supportsTagSearch = isAtLeastInstanceVersion(
+ config.data?.INSTANCE_VERSION,
+ MIN_TAG_SEARCH_VERSION
+ );
const tags = useTags(auth, {
sort: TagSort.NameAZ,
+ search: supportsTagSearch ? searchQuery : undefined,
});
+ const filteredTags = useMemo(() => {
+ const q = searchQuery.trim().toLowerCase();
- const [filteredTags, setFilteredTags] = useState<TagIncludingLinkCount[]>([]);
-
- useEffect(() => {
- const filter =
- tags.data?.filter((e) =>
- e.name.includes(decodeURIComponent(search || ""))
- ) || [];
+ if (q === "") return tags.data;
- setFilteredTags(filter);
- }, [search, tags.data]);
+ return tags.data.filter((tag) => tag.name.toLowerCase().includes(q));
+ }, [searchQuery, tags.data]);
return (
<View
diff --git a/apps/mobile/components/ActionSheets/EditLinkSheet.tsx b/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
index f1c92322..9df07343 100644
--- a/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
+++ b/apps/mobile/components/ActionSheets/EditLinkSheet.tsx
@@ -25,6 +25,9 @@ import { Folder, ChevronRight, ChevronLeft, Check } from "lucide-react-native";
import useTmpStore from "@/store/tmp";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { useTags } from "@linkwarden/router/tags";
+import { isAtLeastInstanceVersion, useConfig } from "@linkwarden/router/config";
+
+const MIN_TAG_SEARCH_VERSION = "2.14.1";
const Main = (props: SheetProps<"edit-link-sheet">) => {
const { auth } = useAuthStore();
@@ -267,8 +270,14 @@ const Tags = () => {
const [searchQuery, setSearchQuery] = useState("");
const router = useSheetRouter("edit-link-sheet");
const params = useSheetRouteParams("edit-link-sheet", "tags");
+ const config = useConfig(auth);
+ const supportsTagSearch = isAtLeastInstanceVersion(
+ config.data?.INSTANCE_VERSION,
+ MIN_TAG_SEARCH_VERSION
+ );
const tags = useTags(auth, {
sort: TagSort.NameAZ,
+ search: supportsTagSearch ? searchQuery : undefined,
});
const { colorScheme } = useColorScheme();
const [updatedLink, setUpdatedLink] =
diff --git a/apps/web/components/InputSelect/TagSelection.tsx b/apps/web/components/InputSelect/TagSelection.tsx
index 851a5e8a..451fef5a 100644
--- a/apps/web/components/InputSelect/TagSelection.tsx
+++ b/apps/web/components/InputSelect/TagSelection.tsx
@@ -1,5 +1,6 @@
import { useEffect, useState } from "react";
import CreatableSelect from "react-select/creatable";
+import { InputActionMeta } from "react-select";
import { styles } from "./styles";
import { ArchivalTagOption, Option } from "@linkwarden/types/inputSelect";
import { useTags } from "@linkwarden/router/tags";
@@ -26,6 +27,7 @@ export default function TagSelection({
autoFocus,
onBlur,
}: Props) {
+ const [searchQuery, setSearchQuery] = useState("");
const {
data: tags = [],
hasNextPage,
@@ -33,6 +35,7 @@ export default function TagSelection({
fetchNextPage,
} = useTags(undefined, {
sort: TagSort.NameAZ,
+ search: searchQuery,
});
const { t } = useTranslation();
@@ -61,6 +64,15 @@ export default function TagSelection({
autoFocus={autoFocus}
onBlur={onBlur}
isLoading={isFetchingNextPage}
+ onInputChange={(value: string, { action }: InputActionMeta) => {
+ if (action === "input-change") setSearchQuery(value);
+ else if (
+ action === "input-blur" ||
+ action === "menu-close" ||
+ action === "set-value"
+ )
+ setSearchQuery("");
+ }}
onMenuScrollToBottom={() => {
if (!hasNextPage || isFetchingNextPage) return;
fetchNextPage();
diff --git a/apps/web/lib/api/controllers/tags/getTags.ts b/apps/web/lib/api/controllers/tags/getTags.ts
index 80e7fb84..35d7b2a1 100644
--- a/apps/web/lib/api/controllers/tags/getTags.ts
+++ b/apps/web/lib/api/controllers/tags/getTags.ts
@@ -10,7 +10,28 @@ export default async function getTags({
collectionId?: number;
query?: TagRequestQuery;
}) {
+ const normalizeSearch = (value?: string) => {
+ if (!value) return undefined;
+
+ try {
+ return decodeURIComponent(value).trim() || undefined;
+ } catch {
+ return value.trim() || undefined;
+ }
+ };
+
const paginationTakeCount = Number(process.env.PAGINATION_TAKE_COUNT) || 50;
+ const POSTGRES_IS_ENABLED =
+ process.env.DATABASE_URL?.startsWith("postgresql");
+ const search = normalizeSearch(query.search);
+ const searchCondition = search
+ ? {
+ name: {
+ contains: search,
+ mode: POSTGRES_IS_ENABLED ? ("insensitive" as const) : undefined,
+ },
+ }
+ : undefined;
let orderBy: any[] = [{ id: "desc" }];
@@ -43,21 +64,26 @@ export default async function getTags({
skip: query.cursor ? 1 : undefined,
cursor: query.cursor ? { id: query.cursor } : undefined,
where: {
- OR: [
- { ownerId: userId }, // Tags owned by the user
- ...(memberCollectionIds.length > 0
- ? [
- {
- links: {
- some: {
- collectionId: {
- in: memberCollectionIds,
+ AND: [
+ ...(searchCondition ? [searchCondition] : []),
+ {
+ OR: [
+ { ownerId: userId }, // Tags owned by the user
+ ...(memberCollectionIds.length > 0
+ ? [
+ {
+ links: {
+ some: {
+ collectionId: {
+ in: memberCollectionIds,
+ },
+ },
},
},
- },
- },
- ]
- : []),
+ ]
+ : []),
+ ],
+ },
],
},
include: {
@@ -81,11 +107,16 @@ export default async function getTags({
} else if (collectionId) {
const tags = await prisma.tag.findMany({
where: {
- links: {
- some: {
- collectionId,
+ AND: [
+ ...(searchCondition ? [searchCondition] : []),
+ {
+ links: {
+ some: {
+ collectionId,
+ },
+ },
},
- },
+ ],
},
include: {
_count: {
diff --git a/apps/web/package.json b/apps/web/package.json
index 57a15147..b2e1d24c 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -1,6 +1,6 @@
{
"name": "@linkwarden/web",
- "version": "v2.14.0",
+ "version": "v2.14.1",
"main": "index.js",
"repository": "https://github.com/linkwarden/linkwarden.git",
"author": "Daniel31X13 <[email protected]>",
diff --git a/apps/web/pages/api/v1/public/collections/tags/index.ts b/apps/web/pages/api/v1/public/collections/tags/index.ts
index 54e11213..6e545a67 100644
--- a/apps/web/pages/api/v1/public/collections/tags/index.ts
+++ b/apps/web/pages/api/v1/public/collections/tags/index.ts
@@ -1,6 +1,6 @@
import getTags from "@/lib/api/controllers/tags/getTags";
import { prisma } from "@linkwarden/prisma";
-import { LinkRequestQuery } from "@linkwarden/types/global";
+import { TagRequestQuery } from "@linkwarden/types/global";
import type { NextApiRequest, NextApiResponse } from "next";
export default async function collections(
@@ -8,15 +8,15 @@ export default async function collections(
res: NextApiResponse
) {
if (req.method === "GET") {
- // Convert the type of the request query to "LinkRequestQuery"
- const convertedData: LinkRequestQuery = {
- sort: Number(req.query.sort as string),
- collectionId: req.query.collectionId
- ? Number(req.query.collectionId as string)
- : undefined,
+ const collectionId = req.query.collectionId
+ ? Number(req.query.collectionId as string)
+ : undefined;
+ const convertedData: TagRequestQuery = {
+ search:
+ typeof req.query.search === "string" ? req.query.search : undefined,
};
- if (!convertedData.collectionId) {
+ if (!collectionId) {
return res
.status(400)
.json({ response: "Please choose a valid collection." });
@@ -24,7 +24,7 @@ export default async function collections(
const collection = await prisma.collection.findFirst({
where: {
- id: convertedData.collectionId,
+ id: collectionId,
isPublic: true,
},
});
@@ -35,6 +35,7 @@ export default async function collections(
const tags = await getTags({
collectionId: collection.id,
+ query: convertedData,
});
const { statusCode, ...data } = tags;
diff --git a/apps/web/pages/api/v1/tags/index.ts b/apps/web/pages/api/v1/tags/index.ts
index 20d13241..9f456f12 100644
--- a/apps/web/pages/api/v1/tags/index.ts
+++ b/apps/web/pages/api/v1/tags/index.ts
@@ -14,6 +14,8 @@ export default async function tags(req: NextApiRequest, res: NextApiResponse) {
const convertedData: TagRequestQuery = {
sort: req.query.sort ? Number(req.query.sort as string) : undefined,
cursor: req.query.cursor ? Number(req.query.cursor as string) : undefined,
+ search:
+ typeof req.query.search === "string" ? req.query.search : undefined,
};
const tags = await getTags({
diff --git a/packages/router/tags.tsx b/packages/router/tags.tsx
index 47a0a9ce..d7342077 100644
--- a/packages/router/tags.tsx
+++ b/packages/router/tags.tsx
@@ -27,6 +27,7 @@ type TagsPage = {
type UseTagsOptions = {
sort?: TagSort;
+ search?: string;
enabled?: boolean;
};
@@ -164,13 +165,15 @@ const removeTagFromQueryData = (
const useTags = (auth?: MobileAuth, options: UseTagsOptions = {}) => {
const status = getAuthStatus(auth);
const config = useConfig(auth);
+ const normalizedSearch = options.search?.trim() || undefined;
const params = useMemo(
() =>
buildQueryString({
sort: options.sort,
+ search: normalizedSearch,
}),
- [options.sort]
+ [normalizedSearch, options.sort]
);
const shouldUsePaginatedSchema = supportsTagPagination(
@@ -193,7 +196,9 @@ const useTags = (auth?: MobileAuth, options: UseTagsOptions = {}) => {
? "&" + (queryKey[1] as any).params
: ""
}`
- : "");
+ : (queryKey[1] as any).params
+ ? `?${(queryKey[1] as any).params}`
+ : "");
const response = await fetch(
url,
diff --git a/packages/types/global.ts b/packages/types/global.ts
index 9017a4e3..59136e44 100644
--- a/packages/types/global.ts
+++ b/packages/types/global.ts
@@ -114,6 +114,7 @@ export type LinkRequestQuery = {
export type TagRequestQuery = {
sort?: TagSort;
cursor?: number;
+ search?: string;
};
export type PublicLinkRequestQuery = {