Skip to content
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

[#127] Add Time component #149

Merged
merged 6 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ui/packages/mr-c.app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@mrc/common-components": "workspace:*",
"@mrc/common-utils": "workspace:*",
"clsx": "^2.1.0",
"dayjs": "^1.11.10",
"next": "^14.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
19 changes: 19 additions & 0 deletions ui/packages/mr-c.app/src/components/time/client-time.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client';

import { type Locale, formatDateToLocal } from '@/lib/utils/format-date-to-local';
import Text, { type TextProps } from '@/components/atomic/text';

interface Props extends Omit<TextProps, 'children'> {
dateStr: string;
locale?: Locale;
relative?: boolean;
}

export default function ClientTime({
dateStr,
locale = 'ko-KR',
relative = false,
...textProps
}: Props) {
return <Text {...textProps}>{formatDateToLocal(dateStr, locale, relative)}</Text>;
}
4 changes: 4 additions & 0 deletions ui/packages/mr-c.app/src/components/time/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import dynamic from 'next/dynamic';
const Time = dynamic(() => import('@/components/time/client-time'), { ssr: false });

export default Time;
27 changes: 27 additions & 0 deletions ui/packages/mr-c.app/src/lib/utils/format-date-to-local.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import 'dayjs/locale/ko';

dayjs.extend(relativeTime);

export type Locale = 'ko-KR' | 'en-US';

// If this function is called on server, it would reflect
// the timezone where the server at rather than the client at.
// This could cause a react hydration error.
export function formatDateToLocal(dateStr: string, locale: Locale, relative = false) {
isutare412 marked this conversation as resolved.
Show resolved Hide resolved
const date = new Date(dateStr);

if (relative) {
return dayjs(date).locale(locale).fromNow();
}

const options: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'short',
year: 'numeric',
};
const formatter = new Intl.DateTimeFormat(locale, options);

return formatter.format(date);
}
8 changes: 8 additions & 0 deletions ui/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2291,6 +2291,7 @@ __metadata:
"@typescript-eslint/parser": ^6.7.3
autoprefixer: ^10.4.16
clsx: ^2.1.0
dayjs: ^1.11.10
eslint: ^8.51.0
eslint-config-next: ^13.5.5
jest: ^29.7.0
Expand Down Expand Up @@ -6656,6 +6657,13 @@ __metadata:
languageName: node
linkType: hard

"dayjs@npm:^1.11.10":
version: 1.11.10
resolution: "dayjs@npm:1.11.10"
checksum: a6b5a3813b8884f5cd557e2e6b7fa569f4c5d0c97aca9558e38534af4f2d60daafd3ff8c2000fed3435cfcec9e805bcebd99f90130c6d1c5ef524084ced588c4
languageName: node
linkType: hard

"debug@npm:2.6.9, debug@npm:^2.6.9":
version: 2.6.9
resolution: "debug@npm:2.6.9"
Expand Down