1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
// Badge count cache helpers
import { BookmarkTypes } from "@karakeep/shared/types/bookmarks";
import { getPluginSettings } from "./settings";
import { getApiClient, getQueryClient } from "./trpc";
import { urlsMatchIgnoringAnchorAndTrailingSlash } from "./url";
/**
* Fetches the bookmark status for a given URL from the API.
* This function will be used by our cache as the "fetcher".
* @param url The URL to check.
* @returns The bookmark id if found, null if not found.
*/
async function fetchBadgeStatus(url: string): Promise<string | null> {
const api = await getApiClient();
if (!api) {
// This case should ideally not happen if settings are correct
throw new Error("[badgeCache] API client not configured");
}
try {
const data = await api.bookmarks.searchBookmarks.query({
text: "url:" + url,
});
const bookmarks = data.bookmarks;
const bookmarksLength = bookmarks.length;
if (bookmarksLength === 0) {
return null;
}
// First check the exact match (including anchor points)
const exactMatch =
bookmarks.find(
(b) =>
b.content.type === BookmarkTypes.LINK &&
urlsMatchIgnoringAnchorAndTrailingSlash(url, b.content.url),
) || null;
return exactMatch ? exactMatch.id : null;
} catch (error) {
console.error(`[badgeCache] Failed to fetch status for ${url}:`, error);
// In case of API error, return a non-cacheable empty status
// Propagate so cache treats this as a miss and doesn’t store
throw error;
}
}
/**
* Get badge status for a URL using the SWR cache.
* @param url The URL to get the status for.
*/
export async function getBadgeStatus(url: string): Promise<string | null> {
const { useBadgeCache, badgeCacheExpireMs } = await getPluginSettings();
if (!useBadgeCache) return fetchBadgeStatus(url);
const queryClient = await getQueryClient();
if (!queryClient) return fetchBadgeStatus(url);
return await queryClient.fetchQuery({
queryKey: ["badgeStatus", url],
queryFn: () => fetchBadgeStatus(url),
// Keep in memory for twice as long as stale time
gcTime: badgeCacheExpireMs * 2,
// Use the user-configured cache expire time
staleTime: badgeCacheExpireMs,
});
}
/**
* Clear badge status cache for a specific URL or all URLs.
* @param url The URL to clear. If not provided, clears the entire cache.
*/
export async function clearBadgeStatus(url?: string): Promise<void> {
const queryClient = await getQueryClient();
if (!queryClient) return;
if (url) {
await queryClient.invalidateQueries({ queryKey: ["badgeStatus", url] });
} else {
await queryClient.invalidateQueries({ queryKey: ["badgeStatus"] });
}
console.log(`[badgeCache] Invalidated cache for: ${url || "all"}`);
}
|