Skip to content

Commit

Permalink
fix: logic nest & stories
Browse files Browse the repository at this point in the history
  • Loading branch information
yamato0211 committed Nov 16, 2023
1 parent d0a84a4 commit 4a2a76d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/components/ui/DateDiffLabel/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ export const ThreeMonthAgo: Story = {
args: { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30 * 3) },
}

export const SixMonthAgo: Story = {
args: { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30 * 6) },
}

export const YearAgo: Story = {
args: { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 365) },
};

export const TwoYearAgo: Story = {
args: { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 365 * 2) },
};
9 changes: 3 additions & 6 deletions src/components/ui/DateDiffLabel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ export const DateDiffLabel: FC<Prop> = ({
date,
now = new Date(),
className,
}) => {
const nowDate = now ?? new Date();
return (
}) => (
<time className={cn(className)} dateTime={date.toISOString()}>
{DateToDiffLabel(date, nowDate)}
{DateToDiffLabel(date, now)}
</time>
);
};
);
53 changes: 25 additions & 28 deletions src/components/ui/DateDiffLabel/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,32 @@ export const DateToDiffLabel = (
Math.floor((now.getTime() - date.getTime()) / 1000)
);

if(diffInSeconds <= MONTH * 3) {
if (diffInSeconds < MINUTE)
return `${diffInSeconds}${
DATE_DIFF_LABEL['second']
}`;
if (diffInSeconds < HOUR)
return `${Math.floor(diffInSeconds / MINUTE)}${
DATE_DIFF_LABEL['minute']
}`;
if (diffInSeconds < DAY)
return `${Math.floor(diffInSeconds / HOUR)}${
DATE_DIFF_LABEL['hour']
}`;
if (diffInSeconds < WEEK)
return `${Math.floor(diffInSeconds / DAY)}${
DATE_DIFF_LABEL['day']
}`;
if (diffInSeconds < MONTH)
return `${Math.floor(diffInSeconds / WEEK)}${
DATE_DIFF_LABEL['week']
}`;
if (diffInSeconds < MINUTE)
return `${diffInSeconds}${
DATE_DIFF_LABEL['second']
}`;
if (diffInSeconds < HOUR)
return `${Math.floor(diffInSeconds / MINUTE)}${
DATE_DIFF_LABEL['minute']
}`;
if (diffInSeconds < DAY)
return `${Math.floor(diffInSeconds / HOUR)}${
DATE_DIFF_LABEL['hour']
}`;
if (diffInSeconds < WEEK)
return `${Math.floor(diffInSeconds / DAY)}${
DATE_DIFF_LABEL['day']
}`;
if (diffInSeconds < MONTH)
return `${Math.floor(diffInSeconds / WEEK)}${
DATE_DIFF_LABEL['week']
}`;
if (diffInSeconds <= MONTH * 3)
return `${Math.floor(diffInSeconds / MONTH)}${
DATE_DIFF_LABEL['month']
}`;
}

if(date.getFullYear() === now.getFullYear()) {
return `${date.getMonth()+1}/${date.getDate()}`
} else {
return `${date.getFullYear()}/${date.getMonth()+1}/${date.getDate()}`
}
if(date.getFullYear() === now.getFullYear())
return `${date.getMonth()+1}/${date.getDate()}`;

return `${date.getFullYear()}/${date.getMonth()+1}/${date.getDate()}`;
};

0 comments on commit 4a2a76d

Please sign in to comment.