fix: init after login
This commit is contained in:
parent
0c07258e58
commit
b4ec25eeb4
@ -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, {
|
||||
|
@ -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;
|
||||
}
|
||||
|
17
src/login.ts
17
src/login.ts
@ -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();
|
||||
}
|
||||
|
||||
|
@ -23,7 +23,7 @@ export function LayoutPage() {
|
||||
login.state.checkIsStandardList(USER_CARDS);
|
||||
login.state.init(login.signer(), system);
|
||||
}
|
||||
}, []);
|
||||
}, [login]);
|
||||
|
||||
useEffect(() => {
|
||||
trackEvent("pageview");
|
||||
|
Loading…
x
Reference in New Issue
Block a user