Files
pocket-id/frontend/src/lib/utils/profile-picture-util.ts
2025-05-24 22:55:46 +02:00

35 lines
997 B
TypeScript

type SkipCacheUntil = {
[key: string]: number;
};
export function getProfilePictureUrl(userId?: string) {
if (!userId) return '';
let url = `/api/users/${userId}/profile-picture.png`;
const skipCacheUntil = getSkipCacheUntil(userId);
const skipCache = skipCacheUntil > Date.now();
if (skipCache) {
const skipCacheParam = new URLSearchParams();
skipCacheParam.append('skip-cache', skipCacheUntil.toString());
url += '?' + skipCacheParam.toString();
}
return url.toString();
}
function getSkipCacheUntil(userId: string) {
const skipCacheUntil: SkipCacheUntil = JSON.parse(
localStorage.getItem('skip-cache-until') ?? '{}'
);
return skipCacheUntil[userId] ?? 0;
}
export function bustProfilePictureCache(userId: string) {
const skipCacheUntil: SkipCacheUntil = JSON.parse(
localStorage.getItem('skip-cache-until') ?? '{}'
);
skipCacheUntil[userId] = Date.now() + 1000 * 60 * 15; // 15 minutes
localStorage.setItem('skip-cache-until', JSON.stringify(skipCacheUntil));
}