⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
refactor(mobile): unify caching logic across formats
daniel31x13e7000bbb76b7
diff --git a/apps/mobile/components/ActionSheets/LinkDetailsSheet.tsx b/apps/mobile/components/ActionSheets/LinkDetailsSheet.tsxindex dde4a892..43e9e79f 100644--- a/apps/mobile/components/ActionSheets/LinkDetailsSheet.tsx+++ b/apps/mobile/components/ActionSheets/LinkDetailsSheet.tsx@@ -301,7 +301,6 @@ export default function LinkDetailsSheet(numberOfLines={1}>{link.url}- {link.url}</Text></TouchableOpacity></View>diff --git a/apps/mobile/components/Formats/ImageFormat.tsx b/apps/mobile/components/Formats/ImageFormat.tsxindex d72b49a5..c72f43f4 100644--- a/apps/mobile/components/Formats/ImageFormat.tsx+++ b/apps/mobile/components/Formats/ImageFormat.tsx@@ -1,11 +1,11 @@import React, { useEffect, useState } from "react";import * as FileSystem from "expo-file-system/legacy";-import NetInfo from "@react-native-community/netinfo";import useAuthStore from "@/store/auth";import { ArchivedFormat } from "@linkwarden/types/global";import { Link as LinkType } from "@linkwarden/prisma/client";import WebView from "react-native-webview";import { Image, Platform, ScrollView } from "react-native";+import { loadCacheOrFetch } from "@/lib/cache";type Props = {link: LinkType;@@ -33,43 +33,22 @@ export default function ImageFormat({ link, setIsLoading, format }: Props) {}, [content]);useEffect(() => {- async function loadCacheOrFetch() {- const filePath =+ loadCacheOrFetch({+ filePath:FileSystem.documentDirectory +- `archivedData/${extension}/link_${link.id}.${extension}`;-- await FileSystem.makeDirectoryAsync(- filePath.substring(0, filePath.lastIndexOf("/")),- {- intermediates: true,- }- ).catch(() => {});-- const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);-- if (info.exists) {- setContent(filePath);- }-- const net = await NetInfo.fetch();-- if (net.isConnected) {+ `archivedData/${extension}/link_${link.id}.${extension}`,+ setContent,+ fetchContent: async (filePath) => {const apiUrl = `${auth.instance}/api/v1/archives/${link.id}?format=${FORMAT}`;- try {- const result = await FileSystem.downloadAsync(apiUrl, filePath, {- headers: { Authorization: `Bearer ${auth.session}` },- });-- setContent(result.uri);- } catch (e) {- console.error("Failed to fetch content", e);- }- }- }+ const result = await FileSystem.downloadAsync(apiUrl, filePath, {+ headers: { Authorization: `Bearer ${auth.session}` },+ });- loadCacheOrFetch();- }, [link]);+ return result.uri;+ },+ });+ }, [FORMAT, auth.instance, auth.session, extension, link.id]);if (Platform.OS === "ios")return (diff --git a/apps/mobile/components/Formats/PdfFormat.tsx b/apps/mobile/components/Formats/PdfFormat.tsxindex e197b370..756810cc 100644--- a/apps/mobile/components/Formats/PdfFormat.tsx+++ b/apps/mobile/components/Formats/PdfFormat.tsx@@ -1,10 +1,10 @@import React, { useEffect, useState } from "react";import * as FileSystem from "expo-file-system/legacy";-import NetInfo from "@react-native-community/netinfo";import useAuthStore from "@/store/auth";import { ArchivedFormat } from "@linkwarden/types/global";import { Link as LinkType } from "@linkwarden/prisma/client";import Pdf from "react-native-pdf";+import { loadCacheOrFetch } from "@/lib/cache";type Props = {link: LinkType;@@ -18,42 +18,21 @@ export default function PdfFormat({ link, setIsLoading }: Props) {const [content, setContent] = useState<string>("");useEffect(() => {- async function loadCacheOrFetch() {- const filePath =- FileSystem.documentDirectory + `archivedData/pdf/link_${link.id}.pdf`;-- await FileSystem.makeDirectoryAsync(- filePath.substring(0, filePath.lastIndexOf("/")),- {- intermediates: true,- }- ).catch(() => {});-- const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);-- if (info.exists) {- setContent(filePath);- }-- const net = await NetInfo.fetch();-- if (net.isConnected) {+ loadCacheOrFetch({+ filePath:+ FileSystem.documentDirectory + `archivedData/pdf/link_${link.id}.pdf`,+ setContent,+ fetchContent: async (filePath) => {const apiUrl = `${auth.instance}/api/v1/archives/${link.id}?format=${FORMAT}`;- try {- const result = await FileSystem.downloadAsync(apiUrl, filePath, {- headers: { Authorization: `Bearer ${auth.session}` },- });-- setContent(result.uri);- } catch (e) {- console.error("Failed to fetch content", e);- }- }- }+ const result = await FileSystem.downloadAsync(apiUrl, filePath, {+ headers: { Authorization: `Bearer ${auth.session}` },+ });- loadCacheOrFetch();- }, [link]);+ return result.uri;+ },+ });+ }, [FORMAT, auth.instance, auth.session, link.id]);return (content && (diff --git a/apps/mobile/components/Formats/ReadableFormat.tsx b/apps/mobile/components/Formats/ReadableFormat.tsxindex 68cea070..09a002ec 100644--- a/apps/mobile/components/Formats/ReadableFormat.tsx+++ b/apps/mobile/components/Formats/ReadableFormat.tsx@@ -10,7 +10,6 @@ import React, {import { Alert, Linking, View } from "react-native";import * as FileSystem from "expo-file-system/legacy";import * as Clipboard from "expo-clipboard";-import NetInfo from "@react-native-community/netinfo";import useAuthStore from "@/store/auth";import WebView from "react-native-webview";import { SheetManager } from "react-native-actions-sheet";@@ -34,6 +33,7 @@ import useReaderStore, {getReadableFontFamily,resolveReaderTheme,} from "@/store/reader";+import { loadCacheOrFetch } from "@/lib/cache";type Props = {link: LinkType;@@ -250,47 +250,27 @@ const ReadableFormat = forwardRef<ReadableFormatRef, Props>(function ReadableForconst isWebViewReadyRef = useRef(false);useEffect(() => {- async function loadCacheOrFetch() {- const filePath =- FileSystem.documentDirectory +- `archivedData/readable/link_${link.id}.html`;-- await FileSystem.makeDirectoryAsync(- filePath.substring(0, filePath.lastIndexOf("/")),- {- intermediates: true,- }- ).catch(() => {});-- const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);-- if (info.exists) {- const rawContent = await FileSystem.readAsStringAsync(filePath);- setContent(rawContent);- }-- const net = await NetInfo.fetch();-- if (net.isConnected) {+ loadCacheOrFetch({+ filePath:+ FileSystem.documentDirectory + `archivedData/readable/link_${link.id}.html`,+ setContent,+ getCachedContent: (filePath) => FileSystem.readAsStringAsync(filePath),+ fetchContent: async (filePath) => {const apiUrl = `${auth.instance}/api/v1/archives/${link.id}?format=${FORMAT}`;- try {- const response = await fetch(apiUrl, {- headers: { Authorization: `Bearer ${auth.session}` },- });+ const response = await fetch(apiUrl, {+ headers: { Authorization: `Bearer ${auth.session}` },+ });- const data = (await response.json()).content;- setContent(data);- await FileSystem.writeAsStringAsync(filePath, data, {- encoding: FileSystem.EncodingType.UTF8,- });- } catch (e) {- console.error("Failed to fetch content", e);- }- }- }+ const data = (await response.json()).content;- loadCacheOrFetch();+ await FileSystem.writeAsStringAsync(filePath, data, {+ encoding: FileSystem.EncodingType.UTF8,+ });++ return data;+ },+ });}, [FORMAT, auth.instance, auth.session, link.id]);const systemTheme: ThemeName = colorScheme === "dark" ? "dark" : "light";diff --git a/apps/mobile/components/Formats/WebpageFormat.tsx b/apps/mobile/components/Formats/WebpageFormat.tsxindex 7d8ecf9c..8fb1a1b2 100644--- a/apps/mobile/components/Formats/WebpageFormat.tsx+++ b/apps/mobile/components/Formats/WebpageFormat.tsx@@ -1,12 +1,12 @@import React, { useEffect, useState } from "react";import * as FileSystem from "expo-file-system/legacy";-import NetInfo from "@react-native-community/netinfo";import getPreservedFormatUrl from "@linkwarden/lib/getPreservedFormatUrl";import { useConfig } from "@linkwarden/router/config";import useAuthStore from "@/store/auth";import { ArchivedFormat } from "@linkwarden/types/global";import { Link as LinkType } from "@linkwarden/prisma/client";import WebView from "react-native-webview";+import { loadCacheOrFetch } from "@/lib/cache";type Props = {link: LinkType;@@ -21,58 +21,34 @@ export default function WebpageFormat({ link, setIsLoading }: Props) {const [content, setContent] = useState<string>("");useEffect(() => {- async function loadCacheOrFetch() {- const filePath =- FileSystem.documentDirectory +- `archivedData/webpage/link_${link.id}.html`;+ loadCacheOrFetch({+ filePath:+ FileSystem.documentDirectory + `archivedData/webpage/link_${link.id}.html`,+ setContent,+ shouldFetch: !isConfigLoading,+ fetchContent: async (filePath) => {+ const apiUrl = config?.USER_CONTENT_DOMAIN+ ? await getPreservedFormatUrl({+ tokenEndpoint: `${auth.instance}/api/v1/preserved/token`,+ linkId: link.id,+ format: FORMAT,+ headers: {+ Authorization: `Bearer ${auth.session}`,+ },+ })+ : `${auth.instance}/api/v1/archives/${link.id}?format=${FORMAT}`;- await FileSystem.makeDirectoryAsync(- filePath.substring(0, filePath.lastIndexOf("/")),- {- intermediates: true,- }- ).catch(() => {});+ const result = await FileSystem.downloadAsync(apiUrl, filePath, {+ ...(config?.USER_CONTENT_DOMAIN+ ? {}+ : {+ headers: { Authorization: `Bearer ${auth.session}` },+ }),+ });- const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);-- if (info.exists) {- setContent(filePath);- }-- if (isConfigLoading) {- return;- }-- const net = await NetInfo.fetch();- if (net.isConnected) {- try {- const apiUrl = config?.USER_CONTENT_DOMAIN- ? await getPreservedFormatUrl({- tokenEndpoint: `${auth.instance}/api/v1/preserved/token`,- linkId: link.id,- format: FORMAT,- headers: {- Authorization: `Bearer ${auth.session}`,- },- })- : `${auth.instance}/api/v1/archives/${link.id}?format=${FORMAT}`;-- const result = await FileSystem.downloadAsync(apiUrl, filePath, {- ...(config?.USER_CONTENT_DOMAIN- ? {}- : {- headers: { Authorization: `Bearer ${auth.session}` },- }),- });-- setContent(result.uri);- } catch (e) {- console.error("Failed to fetch content", e);- }- }- }-- loadCacheOrFetch();+ return result.uri;+ },+ });}, [link, auth.instance, auth.session, config?.USER_CONTENT_DOMAIN, isConfigLoading]);return (diff --git a/apps/mobile/components/LinkListing.tsx b/apps/mobile/components/LinkListing.tsxindex 32b3bd69..b1327b12 100644--- a/apps/mobile/components/LinkListing.tsx+++ b/apps/mobile/components/LinkListing.tsx@@ -9,7 +9,6 @@ import {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";@@ -32,7 +31,7 @@ import { useColorScheme } from "nativewind";import { CalendarDays, Folder } from "lucide-react-native";import useDataStore from "@/store/data";import { useEffect, useState } from "react";-import { deleteLinkCache } from "@/lib/cache";+import { deleteLinkCache, loadCacheOrFetch } from "@/lib/cache";type Props = {link: LinkIncludingShortenedCollectionAndTags;@@ -63,44 +62,23 @@ 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")) {+ loadCacheOrFetch({+ filePath:+ FileSystem.documentDirectory + `archivedData/previews/link_${link.id}.jpg`,+ setContent: setPreview,+ shouldFetch: formatAvailable(link, "preview"),+ onStart: () => setPreview(""),+ errorMessage: "Failed to fetch preview",+ fetchContent: async (filePath) => {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);- }- }- }+ const result = await FileSystem.downloadAsync(apiUrl, filePath, {+ headers: { Authorization: `Bearer ${auth.session}` },+ });- loadCacheOrFetch();+ return result.uri;+ },+ });}, [auth.instance, auth.session, link.id, link.preview, link.updatedAt]);return (@@ -116,6 +94,14 @@ const LinkListing = ({ link, dashboard }: Props) => {onLongPress={() => {}}onPress={() => {if (user) {+ if (user.linksRouteTo === "DETAILS") {+ void SheetManager.show("link-details-sheet", {+ payload: { link },+ });++ return;+ }+const format = getFormatBasedOnPreference({link,preference: user.linksRouteTo,diff --git a/apps/mobile/lib/cache.ts b/apps/mobile/lib/cache.tsindex 85819e54..6b9b01d7 100644--- a/apps/mobile/lib/cache.ts+++ b/apps/mobile/lib/cache.ts@@ -1,4 +1,59 @@import * as FileSystem from "expo-file-system/legacy";+import NetInfo from "@react-native-community/netinfo";++type LoadCacheOrFetchOptions<T> = {+ filePath: string;+ setContent: (content: T) => void;+ getCachedContent?: (filePath: string) => Promise<T>;+ fetchContent?: (filePath: string) => Promise<T>;+ shouldFetch?: boolean;+ onStart?: () => void;+ errorMessage?: string;+};++export const loadCacheOrFetch = async <T = string>({+ filePath,+ setContent,+ getCachedContent = async (filePath) => filePath as T,+ fetchContent,+ shouldFetch = true,+ onStart,+ errorMessage = "Failed to fetch content",+}: LoadCacheOrFetchOptions<T>) => {+ onStart?.();++ await FileSystem.makeDirectoryAsync(+ filePath.substring(0, filePath.lastIndexOf("/")),+ {+ intermediates: true,+ }+ ).catch(() => {});++ const [info] = await Promise.all([FileSystem.getInfoAsync(filePath)]);++ if (info.exists) {+ const cachedContent = await getCachedContent(filePath);+ setContent(cachedContent);+ }++ if (!shouldFetch || !fetchContent) {+ return;+ }++ const net = await NetInfo.fetch();++ if (!net.isConnected) {+ return;+ }++ try {+ const freshContent = await fetchContent(filePath);++ setContent(freshContent);+ } catch (e) {+ console.error(errorMessage, e);+ }+};export const clearCache = async () => {await Promise.all([