Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(mobile): improve perf of manage_tags page

Mohamed Bassem84dfea5ef3ac
diff --git a/apps/mobile/app/dashboard/bookmarks/[slug]/manage_tags.tsx b/apps/mobile/app/dashboard/bookmarks/[slug]/manage_tags.tsx
index c5b21425..82b666ba 100644
--- a/apps/mobile/app/dashboard/bookmarks/[slug]/manage_tags.tsx
+++ b/apps/mobile/app/dashboard/bookmarks/[slug]/manage_tags.tsx
@@ -2,25 +2,24 @@ import React, { useMemo } from "react";
import { Pressable, ScrollView, View } from "react-native";
import { Stack, useLocalSearchParams } from "expo-router";
import FullPageSpinner from "@/components/ui/FullPageSpinner";
+import { useTagAutocomplete } from "@karakeep/shared-react/hooks/tags";
import { GroupedSection, RowSeparator } from "@/components/ui/GroupedList";
import { Text } from "@/components/ui/Text";
import { useToast } from "@/components/ui/Toast";
import { useColorScheme } from "@/lib/useColorScheme";
-import { useQuery } from "@tanstack/react-query";
import { Check, Plus } from "lucide-react-native";
import { useHeaderHeight } from "@react-navigation/elements";
+import { useDebounce } from "@karakeep/shared-react/hooks/use-debounce";
import {
useAutoRefreshingBookmarkQuery,
useUpdateBookmarkTags,
} from "@karakeep/shared-react/hooks/bookmarks";
-import { useTRPC } from "@karakeep/shared-react/trpc";
const NEW_TAG_ID = "new-tag";
const TagPickerPage = () => {
const headerHeight = useHeaderHeight();
- const api = useTRPC();
const { colors } = useColorScheme();
const { slug: bookmarkId } = useLocalSearchParams();
const [search, setSearch] = React.useState("");
@@ -38,42 +37,86 @@ const TagPickerPage = () => {
});
};
- const { data: allTags, isPending: isAllTagsPending } = useQuery(
- api.tags.list.queryOptions(
- {},
- {
- select: React.useCallback(
- (data: { tags: { id: string; name: string }[] }) => {
- return data.tags
- .map((t) => ({
- id: t.id,
- name: t.name,
- lowered: t.name.toLowerCase(),
- }))
- .sort((a, b) => a.lowered.localeCompare(b.lowered));
- },
- [],
- ),
- },
- ),
- );
+ const searchQueryDebounced = useDebounce(search, 200);
+
+ const { data: allTags, isLoading: isAllTagsPending } = useTagAutocomplete({
+ nameContains: searchQueryDebounced,
+ select: (data) =>
+ data.tags.map((tag) => ({
+ id: tag.id,
+ name: tag.name,
+ lowered: tag.name.toLowerCase(),
+ })),
+ });
- const { data: existingTags } = useAutoRefreshingBookmarkQuery({
+ const { data: bookmark } = useAutoRefreshingBookmarkQuery({
bookmarkId,
});
+ const existingTags = bookmark?.tags;
const [optimisticTags, setOptimisticTags] = React.useState<
{ id: string; name: string; lowered: string }[]
>([]);
- React.useEffect(() => {
- setOptimisticTags(
- existingTags?.tags.map((t) => ({
- id: t.id,
- name: t.name,
- lowered: t.name.toLowerCase(),
- })) ?? [],
+ const filteredAllTags = useMemo(() => {
+ if (allTags === undefined) {
+ return [];
+ }
+
+ const loweredSearch = search.toLowerCase();
+ const filteredTags = allTags.filter(
+ (t) => !optimisticTags.find((o) => o.id === t.id),
);
+
+ if (search) {
+ const exactMatchExists =
+ allTags.some((t) => t.lowered === loweredSearch) ||
+ optimisticTags.some((t) => t.lowered === loweredSearch);
+
+ if (!exactMatchExists) {
+ return [
+ { id: NEW_TAG_ID, name: search, lowered: loweredSearch },
+ ...filteredTags,
+ ];
+ }
+ }
+
+ return filteredTags;
+ }, [allTags, optimisticTags, search]);
+
+ React.useEffect(() => {
+ if (!existingTags) {
+ return;
+ }
+ const bookmarkTags = existingTags.map((tag) => ({
+ id: tag.id,
+ name: tag.name,
+ lowered: tag.name.toLowerCase(),
+ }));
+
+ setOptimisticTags((prev) => {
+ const prevOrder = new Map<string, number>();
+ prev.forEach((tag, index) => {
+ prevOrder.set(tag.id, index);
+ prevOrder.set(tag.lowered, index);
+ });
+
+ return bookmarkTags
+ .map((tag, index) => ({ tag, index }))
+ .sort((a, b) => {
+ const aOrder =
+ prevOrder.get(a.tag.id) ??
+ prevOrder.get(a.tag.lowered) ??
+ Number.MAX_SAFE_INTEGER + a.index;
+ const bOrder =
+ prevOrder.get(b.tag.id) ??
+ prevOrder.get(b.tag.lowered) ??
+ Number.MAX_SAFE_INTEGER + b.index;
+
+ return aOrder - bOrder;
+ })
+ .map(({ tag }) => tag);
+ });
}, [existingTags]);
const { mutate: updateTags } = useUpdateBookmarkTags({
@@ -82,7 +125,7 @@ const TagPickerPage = () => {
setOptimisticTags((prev) => [
...prev,
{
- id: t.tagId!,
+ id: t.tagId ?? `${NEW_TAG_ID}:${t.tagName}`,
name: t.tagName!,
lowered: t.tagName!.toLowerCase(),
},
@@ -107,41 +150,6 @@ const TagPickerPage = () => {
});
};
- const optimisticExistingTagIds = useMemo(() => {
- return new Set(optimisticTags?.map((t) => t.id) ?? []);
- }, [optimisticTags]);
-
- const { filteredAllTags, filteredOptimisticTags } = useMemo(() => {
- const loweredSearch = search.toLowerCase();
- let filteredAll =
- allTags?.filter(
- (t) =>
- t.lowered.startsWith(loweredSearch) &&
- !optimisticExistingTagIds.has(t.id),
- ) ?? [];
-
- if (allTags && search) {
- const exactMatchExists =
- allTags.some((t) => t.lowered == loweredSearch) ||
- optimisticTags.some((t) => t.lowered == loweredSearch);
- if (!exactMatchExists) {
- filteredAll = [
- { id: NEW_TAG_ID, name: search, lowered: loweredSearch },
- ...filteredAll,
- ];
- }
- }
-
- const filteredExisting = optimisticTags.filter((t) =>
- t.lowered.startsWith(loweredSearch),
- );
-
- return {
- filteredAllTags: filteredAll,
- filteredOptimisticTags: filteredExisting,
- };
- }, [search, allTags, optimisticTags, optimisticExistingTagIds]);
-
const handleTagPress = (
tag: { id: string; name: string },
action: "attach" | "detach",
@@ -204,9 +212,9 @@ const TagPickerPage = () => {
}}
className="flex-1 bg-background"
>
- {filteredOptimisticTags.length > 0 && (
+ {optimisticTags.length > 0 && (
<GroupedSection header="Attached">
- {filteredOptimisticTags.map((tag, index) => (
+ {optimisticTags.map((tag, index) => (
<React.Fragment key={tag.id}>
{index > 0 && <RowSeparator />}
<Pressable
@@ -244,12 +252,11 @@ const TagPickerPage = () => {
))}
</GroupedSection>
)}
- {filteredOptimisticTags.length === 0 &&
- filteredAllTags.length === 0 && (
- <View className="items-center py-12">
- <Text color="tertiary">No tags found</Text>
- </View>
- )}
+ {optimisticTags.length === 0 && filteredAllTags.length === 0 && (
+ <View className="items-center py-12">
+ <Text color="tertiary">No tags found</Text>
+ </View>
+ )}
</ScrollView>
</>
);