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 1 commit
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
25 changes: 8 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,15 @@ export default function FeedItem({
? recentDetections.filter((det) => det.category === selectedCategory)
: recentDetections;

const fifteenMinutesAgo = new Date(now.valueOf() - 15 * 60 * 1000);
const fiveMinutesAgo = new Date(now.valueOf() - 5 * 60 * 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using useMemo for time calculations

While extracting these calculations improves readability, they're based on now which is only initialized once when the component mounts. For long-lived components, this could lead to stale calculations.

Consider using useMemo to keep the calculations fresh:

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

Committable suggestion skipped: line range outside the PR's diff.


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 +102,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