Skip to content

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

feat(mobile): show offline storage usage

Mohamed Bassemec810158aaa8
diff --git a/apps/mobile/app/dashboard/settings/offline.tsx b/apps/mobile/app/dashboard/settings/offline.tsx
index 1e30479d..baf844cb 100644
--- a/apps/mobile/app/dashboard/settings/offline.tsx
+++ b/apps/mobile/app/dashboard/settings/offline.tsx
@@ -6,13 +6,14 @@ import EmptyState from "@/components/ui/EmptyState";
import { Divider } from "@/components/ui/Divider";
import { Text } from "@/components/ui/Text";
import { useToast } from "@/components/ui/Toast";
-import { clearPersistedCache } from "@/lib/offlineCache";
+import { clearPersistedCache, usePersistedCacheSize } from "@/lib/offlineCache";
import {
getOfflineLibraryScope,
reconcileOfflineLibrary,
removeAllOfflineArticles,
removeOfflineArticle,
useOfflineLibrary,
+ useOfflineLibrarySize,
} from "@/lib/offlineLibrary";
import useAppSettings from "@/lib/settings";
import { useColorScheme } from "@/lib/useColorScheme";
@@ -20,6 +21,39 @@ import { useQueryClient } from "@tanstack/react-query";
import { formatDistanceToNow } from "date-fns";
import { BookOpen, Trash2 } from "lucide-react-native";
+const storageSizeFormatter = new Intl.NumberFormat(undefined, {
+ maximumFractionDigits: 1,
+});
+
+function formatStorageSize(bytes: number) {
+ if (bytes < 1024) {
+ return `${bytes} B`;
+ }
+
+ const units = ["KB", "MB", "GB"];
+ let value = bytes / 1024;
+ let unitIndex = 0;
+ while (value >= 1024 && unitIndex < units.length - 1) {
+ value /= 1024;
+ unitIndex += 1;
+ }
+
+ return `${storageSizeFormatter.format(value)} ${units[unitIndex]}`;
+}
+
+function CacheSectionHeader({ title, size }: { title: string; size: number }) {
+ return (
+ <View className="flex-row items-center justify-between px-1 pb-2">
+ <Text className="text-xs uppercase tracking-wide text-muted-foreground">
+ {title}
+ </Text>
+ <Text className="text-xs tabular-nums text-muted-foreground">
+ {formatStorageSize(size)}
+ </Text>
+ </View>
+ );
+}
+
export default function OfflineContent() {
const router = useRouter();
const headerHeight = useHeaderHeight();
@@ -29,6 +63,8 @@ export default function OfflineContent() {
const { colors } = useColorScheme();
const scope = getOfflineLibraryScope(settings);
const offlineLibrary = useOfflineLibrary(scope);
+ const offlineLibrarySize = useOfflineLibrarySize();
+ const recentCacheSize = usePersistedCacheSize();
useEffect(() => {
reconcileOfflineLibrary(scope);
@@ -96,6 +132,11 @@ export default function OfflineContent() {
only as you browse and can be cleared at any time.
</Text>
+ <CacheSectionHeader
+ title="Saved offline content"
+ size={offlineLibrarySize}
+ />
+
{offlineLibrary.length === 0 ? (
<EmptyState
icon={BookOpen}
@@ -162,9 +203,7 @@ export default function OfflineContent() {
)}
<View className="mt-8">
- <Text className="px-1 pb-2 text-xs uppercase tracking-wide text-muted-foreground">
- Recent cache
- </Text>
+ <CacheSectionHeader title="Recent cache" size={recentCacheSize} />
<Pressable
className="rounded-xl bg-card px-4 py-3"
onPress={confirmClearRecentCache}
diff --git a/apps/mobile/lib/offlineCache.ts b/apps/mobile/lib/offlineCache.ts
index 41831d30..78c34c93 100644
--- a/apps/mobile/lib/offlineCache.ts
+++ b/apps/mobile/lib/offlineCache.ts
@@ -3,6 +3,7 @@ import type {
PersistedClient,
Persister,
} from "@tanstack/react-query-persist-client";
+import { useSyncExternalStore } from "react";
import {
defaultShouldDehydrateQuery,
onlineManager,
@@ -246,6 +247,25 @@ export function clearPersistedCache() {
discardPersistedCache();
}
+function subscribeToPersistedCache(onStoreChange: () => void) {
+ const listener = queryCacheStorage.addOnValueChangedListener(() => {
+ onStoreChange();
+ });
+ return () => listener.remove();
+}
+
+function getPersistedCacheSize() {
+ return queryCacheStorage.byteSize;
+}
+
+export function usePersistedCacheSize() {
+ return useSyncExternalStore(
+ subscribeToPersistedCache,
+ getPersistedCacheSize,
+ getPersistedCacheSize,
+ );
+}
+
export function setupCachePersistence() {
const subscription = AppState.addEventListener("change", (state) => {
if (state !== "active") {
diff --git a/apps/mobile/lib/offlineLibrary.ts b/apps/mobile/lib/offlineLibrary.ts
index db4dd1af..59e2e95c 100644
--- a/apps/mobile/lib/offlineLibrary.ts
+++ b/apps/mobile/lib/offlineLibrary.ts
@@ -1,4 +1,4 @@
-import { useMemo } from "react";
+import { useMemo, useSyncExternalStore } from "react";
import { createMMKV, useMMKVString } from "react-native-mmkv";
import superjson from "superjson";
import { z } from "zod";
@@ -335,6 +335,25 @@ export function clearOfflineLibrary() {
offlineLibraryStorage.clearAll();
}
+function subscribeToOfflineLibrary(onStoreChange: () => void) {
+ const listener = offlineLibraryStorage.addOnValueChangedListener(() => {
+ onStoreChange();
+ });
+ return () => listener.remove();
+}
+
+function getOfflineLibrarySize() {
+ return offlineLibraryStorage.byteSize;
+}
+
+export function useOfflineLibrarySize() {
+ return useSyncExternalStore(
+ subscribeToOfflineLibrary,
+ getOfflineLibrarySize,
+ getOfflineLibrarySize,
+ );
+}
+
export function reconcileOfflineLibrary(scope: string) {
const current = parseManifest(
offlineLibraryStorage.getString(manifestKey(scope)),