fix: init after login

This commit is contained in:
kieran 2024-05-28 15:34:25 +01:00
parent 0c07258e58
commit b4ec25eeb4
No known key found for this signature in database
GPG Key ID: DE71CEB3925BE941
4 changed files with 67 additions and 64 deletions

View File

@ -41,7 +41,6 @@ export function VideoTile({
const bestVideo = video.bestVideo();
const [hasImg, setHasImage] = useState(true);
if (!liveMedia) return;
console.debug(liveMedia)
return (
<div
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}`);
export function useDeadLink(ev: TaggedNostrEvent | NostrEvent) {
const [alive, setAlive] = useState<MediaPayload>();
const [alive, setAlive] = useState<MediaPayload>();
async function testLink(link: string) {
const u = new URL(link);
link = u.toString(); // normalize link
const existing = getDeadLink(link);
if (existing === null) {
// youtube links cant be played
if (u.hostname.endsWith("youtube.com") || u.hostname.endsWith("youtu.be")) {
saveDeadLink(link, false);
return false;
}
const req = await fetch(link, {
method: "HEAD",
});
saveDeadLink(link, req.ok);
return req.ok;
} else {
return existing === "true";
}
async function testLink(link: string) {
const u = new URL(link);
link = u.toString(); // normalize link
const existing = getDeadLink(link);
if (existing === null) {
// youtube links cant be played
if (u.hostname.endsWith("youtube.com") || u.hostname.endsWith("youtu.be")) {
saveDeadLink(link, false);
return false;
}
const req = await fetch(link, {
method: "HEAD",
});
saveDeadLink(link, req.ok);
return req.ok;
} else {
return existing === "true";
}
}
async function testPayload(pl: MediaPayload) {
const alive = await testLink(pl.url);
if (pl.url && alive) {
return pl;
}
for (const alt of pl.alternatives) {
const alive = await testLink(alt);
if (alt && alive) {
return {
...pl,
url: alt
};
}
}
async function testPayload(pl: MediaPayload) {
const alive = await testLink(pl.url);
if (pl.url && alive) {
return pl;
}
async function getLiveLink(links: Array<MediaPayload>) {
for (const l of links) {
const live = await testPayload(l);
if (live) {
setAlive(live);
break;
}
}
for (const alt of pl.alternatives) {
const alive = await testLink(alt);
if (alt && alive) {
return {
...pl,
url: alt,
};
}
}
}
useEffect(() => {
const links = ev.kind === VIDEO_KIND || ev.kind === SHORTS_KIND ?
VideoInfo.parse(ev)?.sources() :
[
{
url: findTag(ev, "streaming") ?? findTag(ev, "recording"),
alternatives: []
} as MediaPayload
];
async function getLiveLink(links: Array<MediaPayload>) {
for (const l of links) {
const live = await testPayload(l);
if (live) {
setAlive(live);
break;
}
}
}
getLiveLink(links).catch(console.error);
}, [ev]);
useEffect(() => {
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);
if (this.#session) {
let save = false;
this.#session.state = new UserState(
this.#session?.pubkey,
undefined,
this.#session.state as UserStateObject<never> | undefined,
);
this.#session.state.on("change", () => {
this.#session.state = this.#makeState();
this.#session.state?.on("change", () => {
this.#save();
});
//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) {
this.#session = {
type,
pubkey: pk,
};
this.#session.state = this.#makeState();
this.#save();
}
@ -81,6 +83,7 @@ export class LoginStore extends ExternalStore<LoginSession | undefined> {
pubkey: bytesToHex(schnorr.getPublicKey(key)),
privateKey: key,
};
this.#session.state = this.#makeState();
this.#save();
}

View File

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