This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/gitgram/mobile/hooks/useImageCache.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +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 };