fix: init after login

This commit is contained in:
2024-05-28 15:34:25 +01:00
parent 0c07258e58
commit b4ec25eeb4
4 changed files with 67 additions and 64 deletions

View File

@ -41,7 +41,6 @@ export function VideoTile({
const bestVideo = video.bestVideo(); const bestVideo = video.bestVideo();
const [hasImg, setHasImage] = useState(true); const [hasImg, setHasImage] = useState(true);
if (!liveMedia) return; if (!liveMedia) return;
console.debug(liveMedia)
return ( return (
<div <div
className={classNames("flex gap-2", className, { className={classNames("flex gap-2", className, {

View File

@ -8,66 +8,67 @@ const saveDeadLink = (link: string, val: boolean) => localStorage.setItem(`dead-
const getDeadLink = (link: string) => localStorage.getItem(`dead-link:${link}`); const getDeadLink = (link: string) => localStorage.getItem(`dead-link:${link}`);
export function useDeadLink(ev: TaggedNostrEvent | NostrEvent) { export function useDeadLink(ev: TaggedNostrEvent | NostrEvent) {
const [alive, setAlive] = useState<MediaPayload>(); const [alive, setAlive] = useState<MediaPayload>();
async function testLink(link: string) { async function testLink(link: string) {
const u = new URL(link); const u = new URL(link);
link = u.toString(); // normalize link link = u.toString(); // normalize link
const existing = getDeadLink(link); const existing = getDeadLink(link);
if (existing === null) { if (existing === null) {
// youtube links cant be played // youtube links cant be played
if (u.hostname.endsWith("youtube.com") || u.hostname.endsWith("youtu.be")) { if (u.hostname.endsWith("youtube.com") || u.hostname.endsWith("youtu.be")) {
saveDeadLink(link, false); saveDeadLink(link, false);
return false; return false;
} }
const req = await fetch(link, { const req = await fetch(link, {
method: "HEAD", method: "HEAD",
}); });
saveDeadLink(link, req.ok); saveDeadLink(link, req.ok);
return req.ok; return req.ok;
} else { } else {
return existing === "true"; return existing === "true";
}
} }
}
async function testPayload(pl: MediaPayload) { async function testPayload(pl: MediaPayload) {
const alive = await testLink(pl.url); const alive = await testLink(pl.url);
if (pl.url && alive) { if (pl.url && alive) {
return pl; return pl;
}
for (const alt of pl.alternatives) {
const alive = await testLink(alt);
if (alt && alive) {
return {
...pl,
url: alt
};
}
}
} }
for (const alt of pl.alternatives) {
async function getLiveLink(links: Array<MediaPayload>) { const alive = await testLink(alt);
for (const l of links) { if (alt && alive) {
const live = await testPayload(l); return {
if (live) { ...pl,
setAlive(live); url: alt,
break; };
} }
}
} }
}
useEffect(() => { async function getLiveLink(links: Array<MediaPayload>) {
const links = ev.kind === VIDEO_KIND || ev.kind === SHORTS_KIND ? for (const l of links) {
VideoInfo.parse(ev)?.sources() : const live = await testPayload(l);
[ if (live) {
{ setAlive(live);
url: findTag(ev, "streaming") ?? findTag(ev, "recording"), break;
alternatives: [] }
} as MediaPayload }
]; }
getLiveLink(links).catch(console.error); useEffect(() => {
}, [ev]); const links =
ev.kind === VIDEO_KIND || ev.kind === SHORTS_KIND
? VideoInfo.parse(ev)?.sources()
: [
{
url: findTag(ev, "streaming") ?? findTag(ev, "recording"),
alternatives: [],
} as MediaPayload,
];
return alive; getLiveLink(links).catch(console.error);
} }, [ev]);
return alive;
}

View File

@ -32,13 +32,8 @@ export class LoginStore extends ExternalStore<LoginSession | undefined> {
this.#session = JSON.parse(json); this.#session = JSON.parse(json);
if (this.#session) { if (this.#session) {
let save = false; let save = false;
this.#session.state = new UserState( this.#session.state = this.#makeState();
this.#session?.pubkey, this.#session.state?.on("change", () => {
undefined,
this.#session.state as UserStateObject<never> | undefined,
);
this.#session.state.on("change", () => {
this.#save(); this.#save();
}); });
//reset //reset
@ -67,11 +62,18 @@ export class LoginStore extends ExternalStore<LoginSession | undefined> {
} }
} }
#makeState() {
if (this.#session) {
return new UserState(this.#session.pubkey, undefined, this.#session.state as UserStateObject<never> | undefined);
}
}
loginWithPubkey(pk: string, type = LoginType.Nip7) { loginWithPubkey(pk: string, type = LoginType.Nip7) {
this.#session = { this.#session = {
type, type,
pubkey: pk, pubkey: pk,
}; };
this.#session.state = this.#makeState();
this.#save(); this.#save();
} }
@ -81,6 +83,7 @@ export class LoginStore extends ExternalStore<LoginSession | undefined> {
pubkey: bytesToHex(schnorr.getPublicKey(key)), pubkey: bytesToHex(schnorr.getPublicKey(key)),
privateKey: key, privateKey: key,
}; };
this.#session.state = this.#makeState();
this.#save(); this.#save();
} }

View File

@ -23,7 +23,7 @@ export function LayoutPage() {
login.state.checkIsStandardList(USER_CARDS); login.state.checkIsStandardList(USER_CARDS);
login.state.init(login.signer(), system); login.state.init(login.signer(), system);
} }
}, []); }, [login]);
useEffect(() => { useEffect(() => {
trackEvent("pageview"); trackEvent("pageview");