Skip to content

Commit

Permalink
Use date-fns for relative time functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo committed Apr 26, 2024
1 parent 05d1851 commit 76c5a10
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
72 changes: 61 additions & 11 deletions dashboard/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"cron-validator": "^1.3.1",
"d3-array": "^2.11.0",
"d3-time-format": "^3.0.0",
"date-fns": "^3.6.0",
"dayjs": "^1.11.5",
"deep-diff": "^1.0.2",
"dotenv": "^8.2.0",
Expand Down
12 changes: 3 additions & 9 deletions dashboard/src/main/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { fakeGuardedRoute } from "shared/auth/RouteGuard";
import { Context } from "shared/Context";
import DeploymentTargetProvider from "shared/DeploymentTargetContext";
import { pushFiltered, pushQueryParams, type PorterUrl } from "shared/routing";
import { relativeDate, timeFrom } from "shared/string_utils";
import { intlFormatDistance, isPast } from "date-fns";
import midnight from "shared/themes/midnight";
import standard from "shared/themes/standard";
import {
Expand Down Expand Up @@ -377,13 +377,7 @@ const Home: React.FC<Props> = (props) => {
if (timestamp === "") {
return true;
}

const diff = timeFrom(timestamp);
if (diff.when === "future") {
return false;
}

return true;
return isPast(new Date(timestamp)) ? true : false;
};

const showCardBanner = !hasPaymentEnabled;
Expand Down Expand Up @@ -424,7 +418,7 @@ const Home: React.FC<Props> = (props) => {
connect a valid payment method
</Link>
. Your free trial is ending {" "}
{relativeDate(plan.trial_info.ending_before, true)}.
{intlFormatDistance(Date.parse(plan.trial_info.ending_before), new Date())}.
</GlobalBanner>
)}
{!trialExpired && showBillingModal && (
Expand Down
8 changes: 3 additions & 5 deletions dashboard/src/main/home/project-settings/BillingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
usePorterCredits,
useSetDefaultPaymentMethod,
} from "lib/hooks/useStripe";
import { relativeDate } from "shared/string_utils";
import { intlFormat, intlFormatDistance } from "date-fns";

import { Context } from "shared/Context";
import cardIcon from "assets/credit-card.svg";
Expand Down Expand Up @@ -82,8 +82,6 @@ function BillingPage(): JSX.Element {
return (credits / 100).toFixed(2);
};

const readableDate = (s: string): string => new Date(s).toLocaleDateString();

const onCreate = async (): Promise<void> => {
await refetchPaymentMethods({ throwOnError: false, cancelRefetch: false });
setShouldCreate(false);
Expand Down Expand Up @@ -233,10 +231,10 @@ function BillingPage(): JSX.Element {
plan.trial_info.ending_before !== "" ? (
<Text>
Free trial ends{" "}
{relativeDate(plan.trial_info.ending_before, true)}
{intlFormatDistance(Date.parse(plan.trial_info.ending_before), new Date())}
</Text>
) : (
<Text>Started on {readableDate(plan.starting_on)}</Text>
<Text>Started on {intlFormat(Date.parse(plan.starting_on))}</Text>
)}
</Container>
</Container>
Expand Down

0 comments on commit 76c5a10

Please sign in to comment.