Refactor: timeAgo 메서드 분리
This commit is contained in:
parent
3ea74a88f8
commit
c90e487ea2
@ -1,4 +1,5 @@
|
||||
import { cn } from '@/lib/utils';
|
||||
import timeAgo from '@/utils/timeAgo';
|
||||
import { AlarmResponse } from '@/types';
|
||||
import { Mail, MailOpen, Trash2 } from 'lucide-react';
|
||||
|
||||
@ -11,20 +12,6 @@ export default function AlarmItem({
|
||||
onRead: (alarmId: number) => void;
|
||||
onDelete: (alarmId: number) => void;
|
||||
}) {
|
||||
const timeAgo = (date: string | Date) => {
|
||||
const now = new Date();
|
||||
const past = new Date(date);
|
||||
const diffInSeconds = Math.floor((now.getTime() - past.getTime()) / 1000);
|
||||
|
||||
if (diffInSeconds < 60) return `${Math.max(diffInSeconds, 0)}초 전`;
|
||||
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
||||
if (diffInMinutes < 60) return `${diffInMinutes}분 전`;
|
||||
const diffInHours = Math.floor(diffInMinutes / 60);
|
||||
if (diffInHours < 24) return `${diffInHours}시간 전`;
|
||||
const diffInDays = Math.floor(diffInHours / 24);
|
||||
return `${diffInDays}일 전`;
|
||||
};
|
||||
|
||||
const handleRead = () => {
|
||||
onRead(alarm.id);
|
||||
};
|
||||
|
13
frontend/src/utils/timeAgo.ts
Normal file
13
frontend/src/utils/timeAgo.ts
Normal file
@ -0,0 +1,13 @@
|
||||
export default function timeAgo(date: string | Date) {
|
||||
const now = new Date();
|
||||
const past = new Date(date);
|
||||
const diffInSeconds = Math.floor((now.getTime() - past.getTime()) / 1000);
|
||||
|
||||
if (diffInSeconds < 60) return `${Math.max(diffInSeconds, 0)}초 전`;
|
||||
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
||||
if (diffInMinutes < 60) return `${diffInMinutes}분 전`;
|
||||
const diffInHours = Math.floor(diffInMinutes / 60);
|
||||
if (diffInHours < 24) return `${diffInHours}시간 전`;
|
||||
const diffInDays = Math.floor(diffInHours / 24);
|
||||
return `${diffInDays}일 전`;
|
||||
}
|
Loading…
Reference in New Issue
Block a user