-
-
Notifications
You must be signed in to change notification settings - Fork 5
Refactor/date fns #1069
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
base: dev
Are you sure you want to change the base?
Refactor/date fns #1069
Changes from all commits
1507cf8
4205b56
e92ae2b
2ae3c26
29cba82
8c666be
dabf422
b698123
2e026c8
228a6d3
f2ff9d0
53d1ae1
cffeb11
5cf6600
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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |
| } from '~/components/ui/dropdown-menu' | ||
| import { type Device } from '~/db/schema' | ||
| import { DeviceIdCell } from './device-id-cell' | ||
| import { useHydrated } from '~/hooks/use-hydrated' | ||
|
|
||
| export type SenseBox = { | ||
| id: string | ||
|
|
@@ -32,8 +33,10 @@ export function getColumns( | |
| useTranslation: UseTranslationResponse<'data-table', any>, | ||
| opts?: { isOwner?: boolean }, | ||
| ): ColumnDef<SenseBox>[] { | ||
| const { t } = useTranslation | ||
| const { t, i18n } = useTranslation | ||
| const isOwner = opts?.isOwner ?? false | ||
| const hydrated = useHydrated() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As |
||
|
|
||
| return [ | ||
| { | ||
| accessorKey: 'name', | ||
|
|
@@ -90,7 +93,7 @@ export function getColumns( | |
| }, | ||
| cell: ({ row }) => { | ||
| const date = new Date(row.getValue('createdAt')) | ||
| return <div>{date.toLocaleDateString()}</div> | ||
| return <div>{hydrated && date.toLocaleDateString(i18n.language)}</div> | ||
| }, | ||
| }, | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| const ONE_MINUTE_IN_S = 60 | ||
| const ONE_HOUR_IN_S = 60 * ONE_MINUTE_IN_S | ||
| const ONE_DAY_IN_S = 24 * ONE_HOUR_IN_S | ||
| const ONE_WEEK_IN_S = 7 * ONE_DAY_IN_S | ||
| const ONE_MONTH_IN_S = 31 * ONE_DAY_IN_S | ||
| const ONE_YEAR_IN_S = 365 * ONE_DAY_IN_S | ||
| const ONE_QUARTER_IN_S = ONE_YEAR_IN_S / 4 | ||
|
|
||
| export const dateDiffToNowInWords = (locale: string, date: Date) => { | ||
| const r = new Intl.RelativeTimeFormat(locale) | ||
| const now = new Date() | ||
| const diffInSeconds = Math.round((now.getTime() - date.getTime()) / 1000) | ||
| const absDiffInSeconds = Math.abs(diffInSeconds) | ||
|
|
||
| if (absDiffInSeconds < ONE_MINUTE_IN_S) | ||
| return r.format(-diffInSeconds, 'second') | ||
| if (absDiffInSeconds < ONE_HOUR_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_MINUTE_IN_S), 'minute') | ||
| if (absDiffInSeconds < ONE_DAY_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_HOUR_IN_S), 'hour') | ||
| if (absDiffInSeconds < ONE_WEEK_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_DAY_IN_S), 'day') | ||
| if (absDiffInSeconds < ONE_MONTH_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_WEEK_IN_S), 'week') | ||
| if (absDiffInSeconds < ONE_QUARTER_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_MONTH_IN_S), 'month') | ||
| if (absDiffInSeconds < ONE_YEAR_IN_S) | ||
| return r.format(-Math.round(diffInSeconds / ONE_QUARTER_IN_S), 'quarter') | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont know how this is handled in general, but quarter sounds a bit weird to me, what about having only month and year level instead? |
||
| return r.format(-Math.round(diffInSeconds / ONE_YEAR_IN_S), 'year') | ||
| } | ||
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.
The previous format method displayed a combination of date and time, the new method requests only hours and minutes. Maybe it is fine though as we dont usually have multi-day trips?