railyard/apps/ritgram/mobile/hooks/useImageCache.ts
caelum0x f80b04ae0f feat(ritgram): move GitGram app into railyard as Ritgram, next to Rithub
Moved later/gitgram-ai/gitgram/ to apps/ritgram/ (fresh history; full history stays in caelum0x/gitgram).
AITimeMachine stays separate in later/gitgram-ai per the Rithub master plan.
Logs, pid files and env files are ignored; GPL-3.0 mobile/proximity noted in THIRD_PARTY_NOTICES.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 04:18:46 +03:00

104 lines
2.2 KiB
TypeScript

import { useState, useEffect } from 'react';
import { Image } from 'expo-image';
interface CacheOptions {
maxSize?: number;
ttl?: number; // Time to live in milliseconds
}
class ImageCache {
private cache: Map<string, { url: string; timestamp: number }> = new Map();
private maxSize: number;
private ttl: number;
constructor(options: CacheOptions = {}) {
this.maxSize = options.maxSize || 100;
this.ttl = options.ttl || 24 * 60 * 60 * 1000; // 24 hours default
}
set(key: string, url: string) {
// Remove oldest entries if cache is full
if (this.cache.size >= this.maxSize && !this.cache.has(key)) {
const firstKey = this.cache.keys().next().value;
if (firstKey) {
this.cache.delete(firstKey);
}
}
this.cache.set(key, {
url,
timestamp: Date.now(),
});
}
get(key: string): string | null {
const entry = this.cache.get(key);
if (!entry) {
return null;
}
// Check if entry has expired
if (Date.now() - entry.timestamp > this.ttl) {
this.cache.delete(key);
return null;
}
return entry.url;
}
has(key: string): boolean {
return this.cache.has(key) &&
Date.now() - (this.cache.get(key)?.timestamp || 0) <= this.ttl;
}
clear() {
this.cache.clear();
}
size() {
return this.cache.size;
}
}
const imageCache = new ImageCache({ maxSize: 100, ttl: 24 * 60 * 60 * 1000 });
export function useImageCache(url: string | null | undefined) {
const [cachedUrl, setCachedUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
if (!url) {
setCachedUrl(null);
setLoading(false);
return;
}
// Check cache first
const cached = imageCache.get(url);
if (cached) {
setCachedUrl(cached);
setLoading(false);
return;
}
// Preload image
setLoading(true);
Image.prefetch(url)
.then(() => {
imageCache.set(url, url);
setCachedUrl(url);
setLoading(false);
})
.catch(() => {
setCachedUrl(url); // Fallback to original URL
setLoading(false);
});
}, [url]);
return { cachedUrl, loading };
}
export { imageCache };