Fix sorting
This commit is contained in:
@ -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]);
|
||||||
|
|
||||||
|
function sortCreatedAt(a: NostrEvent, b: NostrEvent) {
|
||||||
|
return b.created_at > a.created_at ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortStarts(a: NostrEvent, b: NostrEvent) {
|
||||||
|
const aStart = Number(findTag(a, "starts") ?? "0");
|
||||||
|
const bStart = Number(findTag(b, "starts") ?? "0");
|
||||||
|
return bStart > aStart ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
const feed = useRequestBuilder<NoteCollection>(System, NoteCollection, rb);
|
const feed = useRequestBuilder<NoteCollection>(System, NoteCollection, rb);
|
||||||
const feedSorted = useMemo(() => {
|
const feedSorted = useMemo(() => {
|
||||||
if (feed.data) {
|
if (feed.data) {
|
||||||
return [...feed.data].sort((a, b) => {
|
return [...feed.data];
|
||||||
const status = findTag(a, "status");
|
|
||||||
if (status === StreamState.Ended) {
|
|
||||||
return b.created_at > a.created_at ? 1 : -1;
|
|
||||||
}
|
|
||||||
const aStart = Number(findTag(a, "starts") ?? "0");
|
|
||||||
const bStart = Number(findTag(b, "starts") ?? "0");
|
|
||||||
return bStart > aStart ? 1 : -1;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
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 };
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user