Skip to content

Commit

Permalink
migrate date function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wbglaeser committed Dec 5, 2024
1 parent 5d01401 commit a3593b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import React from "react";
import { Box, Typography, Link } from "@mui/material";
import GitHubIcon from "@mui/icons-material/GitHub";
import {mapDate} from "../../../../../utils/dateUtils";
import useTranslation from "../../../../../language/useTranslation";

const GithubCommitElement = ({ commit }) => {
const { t } = useTranslation();

const mapDate = (date) => {
const dateObj = new Date(date);
const today = new Date();

dateObj.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);

const diffTime = today - dateObj;
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));

if (diffDays === 0) {
return t("activityLog.gitCommits.today");
} else if (diffDays === 1) {
return t("activityLog.gitCommits.yesterday");
} else {
return t("activityLog.gitCommits.daysAgo", { count: diffDays });
}
};

return (
<Box
sx={{
Expand Down Expand Up @@ -75,7 +57,7 @@ const GithubCommitElement = ({ commit }) => {
textAlign: "right",
}}
>
{mapDate(commit.timestamp)}
{mapDate(commit.timestamp, t)}
</Typography>
<Link href={commit.commit_url} target="_blank" rel="noopener noreferrer">
<GitHubIcon sx={{ fontSize: 24, color: "text.primary" }} />
Expand Down
18 changes: 18 additions & 0 deletions src/ui/utils/dateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const mapDate = (date, t) => {
const dateObj = new Date(date);
const today = new Date();

dateObj.setHours(0, 0, 0, 0);
today.setHours(0, 0, 0, 0);

const diffTime = today - dateObj;
const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));

if (diffDays === 0) {
return t("activityLog.gitCommits.today");
} else if (diffDays === 1) {
return t("activityLog.gitCommits.yesterday");
} else {
return t("activityLog.gitCommits.daysAgo", { count: diffDays });
}
};

0 comments on commit a3593b8

Please sign in to comment.