Fix sorting

This commit is contained in:
2023-08-04 16:25:29 +01:00
parent 14aeff4a80
commit 22a799aac7

View File

@ -1,6 +1,6 @@
import { useMemo } from "react"; import { useMemo } from "react";
import { NoteCollection, RequestBuilder } from "@snort/system"; import { NostrEvent, NoteCollection, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react"; import { useRequestBuilder } from "@snort/system-react";
import { unixNow } from "@snort/shared"; import { unixNow } from "@snort/shared";
@ -24,33 +24,35 @@ export function useStreamsFeed(tag?: string) {
return rb; return rb;
}, [tag, since]); }, [tag, since]);
const feed = useRequestBuilder<NoteCollection>(System, NoteCollection, rb); function sortCreatedAt(a: NostrEvent, b: NostrEvent) {
const feedSorted = useMemo(() => {
if (feed.data) {
return [...feed.data].sort((a, b) => {
const status = findTag(a, "status");
if (status === StreamState.Ended) {
return b.created_at > a.created_at ? 1 : -1; return b.created_at > a.created_at ? 1 : -1;
} }
function sortStarts(a: NostrEvent, b: NostrEvent) {
const aStart = Number(findTag(a, "starts") ?? "0"); const aStart = Number(findTag(a, "starts") ?? "0");
const bStart = Number(findTag(b, "starts") ?? "0"); const bStart = Number(findTag(b, "starts") ?? "0");
return bStart > aStart ? 1 : -1; return bStart > aStart ? 1 : -1;
}); }
const feed = useRequestBuilder<NoteCollection>(System, NoteCollection, rb);
const feedSorted = useMemo(() => {
if (feed.data) {
return [...feed.data];
} }
return []; return [];
}, [feed.data]); }, [feed.data]);
const live = feedSorted.filter( const live = feedSorted.filter(
(a) => findTag(a, "status") === StreamState.Live (a) => findTag(a, "status") === StreamState.Live
); ).sort(sortStarts);
const planned = feedSorted.filter( const planned = feedSorted.filter(
(a) => findTag(a, "status") === StreamState.Planned (a) => findTag(a, "status") === StreamState.Planned
); ).sort(sortStarts);
const ended = feedSorted.filter((a) => { const ended = feedSorted.filter((a) => {
const hasEnded = findTag(a, "status") === StreamState.Ended; const hasEnded = findTag(a, "status") === StreamState.Ended;
const recording = findTag(a, "recording") ?? ""; const recording = findTag(a, "recording") ?? "";
return hasEnded && recording?.length > 0; return hasEnded && recording?.length > 0;
}); }).sort(sortCreatedAt);
return { live, planned, ended }; return { live, planned, ended };
} }