-
Notifications
You must be signed in to change notification settings - Fork 18
refactor: replace date-fns with safe date formatting functions across… #1053
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { | ||
| formatDistanceStrict, | ||
| formatDistanceToNow, | ||
| formatDistanceToNowStrict, | ||
| type FormatDistanceStrictOptions, | ||
| type FormatDistanceToNowStrictOptions, | ||
| } from "date-fns"; | ||
|
|
||
| function toValidDate(value: Date | string | null | undefined): Date | null { | ||
| if (value == null) return null; | ||
| const date = value instanceof Date ? value : new Date(value); | ||
| if (isNaN(date.getTime())) return null; | ||
| return date; | ||
| } | ||
|
|
||
| export function safeFormatDistanceToNowStrict( | ||
| value: Date | string | null | undefined, | ||
| options?: FormatDistanceToNowStrictOptions, | ||
| ): string | null { | ||
| const date = toValidDate(value); | ||
| if (date == null) return null; | ||
| return formatDistanceToNowStrict(date, options); | ||
| } | ||
|
|
||
| export function safeFormatDistanceToNow( | ||
| value: Date | string | null | undefined, | ||
| options?: Parameters<typeof formatDistanceToNow>[1], | ||
| ): string | null { | ||
| const date = toValidDate(value); | ||
| if (date == null) return null; | ||
| return formatDistanceToNow(date, options); | ||
| } | ||
|
|
||
| export function safeFormatDistanceStrict( | ||
| a: Date | string | null | undefined, | ||
| b: Date | string | null | undefined, | ||
| options?: FormatDistanceStrictOptions, | ||
| ): string | null { | ||
| const dateA = toValidDate(a); | ||
| const dateB = toValidDate(b); | ||
| if (dateA == null || dateB == null) return null; | ||
| return formatDistanceStrict(dateA, dateB, options); | ||
| } | ||
|
|
||
| export function safeIsPast(value: Date | string | null | undefined): boolean { | ||
| const date = toValidDate(value); | ||
| if (date == null) return false; | ||
| return date.getTime() < Date.now(); | ||
| } | ||
|
|
||
| export function safeIsFuture(value: Date | string | null | undefined): boolean { | ||
| const date = toValidDate(value); | ||
| if (date == null) return false; | ||
| return date.getTime() > Date.now(); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,4 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useMemo } from "react"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { formatDistanceToNow } from "date-fns"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import _ from "lodash"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AlertCircleIcon, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -17,6 +16,7 @@ import { Link, useParams } from "react-router"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { toast } from "sonner"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { trpc } from "~/api/trpc"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { safeFormatDistanceToNow } from "~/lib/date"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Badge } from "~/components/ui/badge"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -245,18 +245,21 @@ function EvalRow({ evaluation }: { evaluation: Evaluation }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span className="flex items-center gap-1"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Clock className="size-3" /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Evaluated{" "} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {formatDistanceToNow(new Date(evaluation.evaluatedAt), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {safeFormatDistanceToNow(evaluation.evaluatedAt, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addSuffix: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) ?? "unknown"} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {evaluation.satisfiedAt != null && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Satisfied{" "} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {formatDistanceToNow(new Date(evaluation.satisfiedAt), { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addSuffix: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {evaluation.satisfiedAt != null && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| safeFormatDistanceToNow(evaluation.satisfiedAt, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addSuffix: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) != null && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Satisfied{" "} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {safeFormatDistanceToNow(evaluation.satisfiedAt, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| addSuffix: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+252
to
+262
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {evaluation.satisfiedAt != null && | |
| safeFormatDistanceToNow(evaluation.satisfiedAt, { | |
| addSuffix: true, | |
| }) != null && ( | |
| <span> | |
| Satisfied{" "} | |
| {safeFormatDistanceToNow(evaluation.satisfiedAt, { | |
| addSuffix: true, | |
| })} | |
| </span> | |
| )} | |
| {(() => { | |
| const satisfiedAtDistance = | |
| evaluation.satisfiedAt == null | |
| ? null | |
| : safeFormatDistanceToNow(evaluation.satisfiedAt, { | |
| addSuffix: true, | |
| }); | |
| return ( | |
| satisfiedAtDistance != null && ( | |
| <span> | |
| Satisfied {satisfiedAtDistance} | |
| </span> | |
| ) | |
| ); | |
| })()} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file still references
isPastandformatDistanceToNowStrictlater inWindowRow(e.g. checks for ended/next window), but thosedate-fnsimports were removed and the safe replacements are imported instead. This will cause a TypeScript compile error (unresolved identifiers) and also defeats the goal of using safe formatters. Update the remaining usages tosafeIsPast/safeFormatDistanceToNowStrict(and handlenullreturns) or reintroduce the needed imports consistently.