Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FeedItem with precalculated time values and constant categories #723

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions ui/src/components/Bouts/FeedItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ import {
import { useListenerCount } from "@/hooks/useFeedPresence";
import { formatTimestamp } from "@/utils/time";

const categories: Array<DetectionCategory> = ["WHALE", "VESSEL", "OTHER"];

export default function FeedItem({
feed,
onStatUpdate,
}: {
feed: Pick<Feed, "id" | "name" | "slug" | "online">;
onStatUpdate?: (feedId: string, stat: string, value: number) => void;
}) {
const categories: Array<DetectionCategory> = useMemo(
() => ["WHALE", "VESSEL", "OTHER"],
[],
);

const [showTable, setShowTable] = useState(false);
const [selectedCategory, setSelectedCategory] = useState<DetectionCategory>();
const theme = useTheme();
Expand Down Expand Up @@ -64,14 +61,21 @@ export default function FeedItem({
? recentDetections.filter((det) => det.category === selectedCategory)
: recentDetections;

const fifteenMinutesAgo = useMemo(
() => new Date(now.valueOf() - 15 * 60 * 1000),
[now],
);
const fiveMinutesAgo = useMemo(
() => new Date(now.valueOf() - 5 * 60 * 1000),
[now],
);

const detsCount = recentDetections.length;
const detsCount15MinAgo = recentDetections.filter(
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 15 * 60 * 1000),
({ timestamp }) => new Date(timestamp) > fifteenMinutesAgo,
).length;
const detsCount5MinAgo = recentDetections.filter(
({ timestamp }) =>
new Date(timestamp) > new Date(now.valueOf() - 5 * 60 * 1000),
({ timestamp }) => new Date(timestamp) > fiveMinutesAgo,
).length;

const detectionChartData = useMemo(
Expand Down Expand Up @@ -104,14 +108,7 @@ export default function FeedItem({
);
});
}
}, [
feed.id,
recentDetections,
detsCount,
onStatUpdate,
listenerCount,
categories,
]);
}, [feed.id, recentDetections, detsCount, onStatUpdate, listenerCount]);

return (
<Card sx={{ width: "100%", p: 2, overflowX: "auto" }} elevation={1}>
Expand Down
Loading