Skip to content

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

feat(mobile): implement caching for link previews

daniel31x133a8152819f31
diff --git a/apps/mobile/components/LinkListing.tsx b/apps/mobile/components/LinkListing.tsx
index 54a5eb57..32b3bd69 100644
--- a/apps/mobile/components/LinkListing.tsx
+++ b/apps/mobile/components/LinkListing.tsx
@@ -8,6 +8,8 @@ import {
ActivityIndicator,
Linking,
} from "react-native";
+import * as FileSystem from "expo-file-system/legacy";
+import NetInfo from "@react-native-community/netinfo";
import { decode } from "html-entities";
import { LinkIncludingShortenedCollectionAndTags } from "@linkwarden/types/global";
import { ArchivedFormat } from "@linkwarden/types/global";
@@ -48,6 +50,7 @@ const LinkListing = ({ link, dashboard }: Props) => {
const deleteLink = useDeleteLink({ auth, Alert });
const [url, setUrl] = useState("");
+ const [preview, setPreview] = useState("");
useEffect(() => {
try {
@@ -59,6 +62,47 @@ const LinkListing = ({ link, dashboard }: Props) => {
}
}, [link.url]);
+ useEffect(() => {
+ async function loadCacheOrFetch() {
+ setPreview("");
+
+ const filePath =
+ FileSystem.documentDirectory +
+ `archivedData/previews/link_${link.id}.jpg`;
+
+ await FileSystem.makeDirectoryAsync(
+ filePath.substring(0, filePath.lastIndexOf("/")),
+ {
+ intermediates: true,
+ }
+ ).catch(() => {});
+
+ const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);
+
+ if (info.exists) {
+ setPreview(filePath);
+ }
+
+ const net = await NetInfo.fetch();
+
+ if (net.isConnected && formatAvailable(link, "preview")) {
+ const apiUrl = `${auth.instance}/api/v1/archives/${link.id}?format=${ArchivedFormat.jpeg}&preview=true&updatedAt=${link.updatedAt}`;
+
+ try {
+ const result = await FileSystem.downloadAsync(apiUrl, filePath, {
+ headers: { Authorization: `Bearer ${auth.session}` },
+ });
+
+ setPreview(result.uri);
+ } catch (e) {
+ console.error("Failed to fetch preview", e);
+ }
+ }
+ }
+
+ loadCacheOrFetch();
+ }, [auth.instance, auth.session, link.id, link.preview, link.updatedAt]);
+
return (
<ContextMenu.Root>
<ContextMenu.Trigger asChild>
@@ -137,15 +181,16 @@ const LinkListing = ({ link, dashboard }: Props) => {
<View className="flex-col items-end">
<View className="rounded-lg overflow-hidden relative">
{formatAvailable(link, "preview") ? (
- <Image
- source={{
- uri: `${auth.instance}/api/v1/archives/${link.id}?format=${ArchivedFormat.jpeg}&preview=true&updatedAt=${link.updatedAt}`,
- headers: {
- Authorization: `Bearer ${auth.session}`,
- },
- }}
- className="rounded-md h-[60px] w-[90px] object-cover scale-105"
- />
+ preview ? (
+ <Image
+ source={{
+ uri: preview,
+ }}
+ className="rounded-md h-[60px] w-[90px] object-cover scale-105"
+ />
+ ) : (
+ <View className="h-[60px] w-[90px]" />
+ )
) : !link.preview ? (
<ActivityIndicator
size="small"
diff --git a/apps/mobile/lib/cache.ts b/apps/mobile/lib/cache.ts
index 8cdcd269..85819e54 100644
--- a/apps/mobile/lib/cache.ts
+++ b/apps/mobile/lib/cache.ts
@@ -22,6 +22,8 @@ export const deleteLinkCache = async (linkId: number) => {
FileSystem.documentDirectory + `archivedData/png/link_${linkId}.png`;
const pdfPath =
FileSystem.documentDirectory + `archivedData/pdf/link_${linkId}.pdf`;
+ const previewPath =
+ FileSystem.documentDirectory + `archivedData/previews/link_${linkId}.jpg`;
await Promise.all([
FileSystem.deleteAsync(readablePath, { idempotent: true }),
@@ -29,5 +31,6 @@ export const deleteLinkCache = async (linkId: number) => {
FileSystem.deleteAsync(jpegPath, { idempotent: true }),
FileSystem.deleteAsync(pngPath, { idempotent: true }),
FileSystem.deleteAsync(pdfPath, { idempotent: true }),
+ FileSystem.deleteAsync(previewPath, { idempotent: true }),
]);
};