Skip to content

LB-1528: Added shortcut to today's date in Fresh Releases using a Calendar Icon #3125

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

Merged
merged 10 commits into from
Jan 21, 2025
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import * as React from "react";
import Slider from "rc-slider";
import { countBy, debounce, zipObject } from "lodash";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCalendarCheck } from "@fortawesome/free-solid-svg-icons";
import { formatReleaseDate, useMediaQuery } from "../utils";
import { SortDirection } from "../FreshReleases";
import { COLOR_LB_BLUE } from "../../../utils/constants";

type ReleaseTimelineProps = {
releases: Array<FreshReleaseItem>;
Expand All @@ -15,7 +18,7 @@ function createMarks(
sortDirection: string,
order: string
) {
let dataArr: Array<string> = [];
let dataArr: Array<React.ReactNode> = [];
let percentArr: Array<number> = [];
// We want to filter out the keys that have less than 1.5% of the total releases
const minReleasesThreshold = Math.floor(releases.length * 0.015);
Expand All @@ -24,11 +27,26 @@ function createMarks(
releases,
(item: FreshReleaseItem) => item.release_date
);
const filteredDates = Object.keys(releasesPerDate).filter(
(date) => releasesPerDate[date] >= minReleasesThreshold
const today = new Date();
const todayString = today.toISOString().split("T")[0];
const formattedTodayString = formatReleaseDate(todayString);
const filteredDates = Object.keys(releasesPerDate).filter((date) =>
date !== todayString
? releasesPerDate[date] >= minReleasesThreshold
: releasesPerDate[date] > 0
);

dataArr = filteredDates.map((item) => formatReleaseDate(item));
dataArr = filteredDates.map((item) =>
formatReleaseDate(item) === formattedTodayString ? (
<FontAwesomeIcon
icon={faCalendarCheck}
size="2xl"
color={COLOR_LB_BLUE}
/>
) : (
formatReleaseDate(item)
)
);
Copy link
Member

Choose a reason for hiding this comment

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

I had another read through the code and I figured we can simplify this quite a lot thanks to a utility function from the date-fns library that we use in LB:
https://date-fns.org/v4.1.0/docs/isToday

From testing in the console on their website, it should be as simple as doing:

image
and removing today, todayString and formattedTodayString.

Of course you'll need to import { isToday } from "date-fns";

    const filteredDates = Object.keys(releasesPerDate).filter((date) =>
      !isToday(new Date(date))
        ? releasesPerDate[date] >= minReleasesThreshold
        : releasesPerDate[date] > 0
    );

    dataArr = filteredDates.map((item) =>
      isToday(new Date(item)) ? (
        <FontAwesomeIcon
          icon={faCalendarCheck}
          size="2xl"
          color={COLOR_LB_BLUE}
        />
      ) : (
        formatReleaseDate(item)
      )
    );

percentArr = filteredDates
.map((item) => (releasesPerDate[item] / releases.length) * 100)
.map((_, index, arr) =>
Expand Down Expand Up @@ -103,7 +121,9 @@ export default function ReleaseTimeline(props: ReleaseTimelineProps) {
const { releases, order, direction } = props;

const [currentValue, setCurrentValue] = React.useState<number | number[]>();
const [marks, setMarks] = React.useState<{ [key: number]: string }>({});
const [marks, setMarks] = React.useState<{ [key: number]: React.ReactNode }>(
{}
);

const screenMd = useMediaQuery("(max-width: 992px)"); // @screen-md

Expand Down
Loading