feat: release v0.1.3 chat, profiles, and media
Keep read receipts for the other person only, and let photos, voice, and files stay cached until they are actually replaced.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { mediaObjectKey } from "@/lib/media";
|
||||
import { loadCachedMedia } from "@/lib/transfer";
|
||||
|
||||
/** Local bytes for an image or similar object. A replacement has a new key and loads again. Video is not cached. */
|
||||
export function useCachedObjectUrl(url: string, cacheable = true): string {
|
||||
const key = mediaObjectKey(url);
|
||||
const [local, setLocal] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
if (!cacheable || !url) {
|
||||
setLocal("");
|
||||
return;
|
||||
}
|
||||
let dead = false;
|
||||
let objectUrl = "";
|
||||
void loadCachedMedia(url)
|
||||
.then((blob) => {
|
||||
if (dead) return;
|
||||
objectUrl = URL.createObjectURL(blob);
|
||||
setLocal(objectUrl);
|
||||
})
|
||||
.catch(() => {
|
||||
if (!dead) setLocal(url);
|
||||
});
|
||||
return () => {
|
||||
dead = true;
|
||||
if (objectUrl) URL.revokeObjectURL(objectUrl);
|
||||
};
|
||||
}, [cacheable, key, url]);
|
||||
|
||||
if (!cacheable) return url;
|
||||
return local;
|
||||
}
|
||||
Reference in New Issue
Block a user