Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

explore-card: relative update times, other stuff (#22)

PickleNik8dd240a497a5
diff --git a/src/components/explore/explore-card.tsx b/src/components/explore/explore-card.tsx
index 083a06fd..8d430e16 100644
--- a/src/components/explore/explore-card.tsx
+++ b/src/components/explore/explore-card.tsx
@@ -19,6 +19,32 @@ const BORDERS_BY_DIFFICULTY = {
'dark:hover:border-orange-300 hover:border-orange-500 dark:group-focus:border-orange-300 group-focus:border-orange-500',
};
+// in miliseconds
+const units = {
+ year: 24 * 60 * 60 * 1000 * 365,
+ month: (24 * 60 * 60 * 1000 * 365) / 12,
+ day: 24 * 60 * 60 * 1000,
+ hour: 60 * 60 * 1000,
+ minute: 60 * 1000,
+ second: 1000,
+};
+
+const rtf = new Intl.RelativeTimeFormat('en', {
+ numeric: 'auto',
+ // style: 'short'
+});
+
+// TODO: typescript
+const getRelativeTime = (date) => {
+ const now = new Date();
+ const elapsed = date - now;
+
+ // "Math.abs" accounts for both "past" & "future" scenarios
+ for (const u in units)
+ if (Math.abs(elapsed) > units[u] || u == 'second')
+ return rtf.format(Math.round(elapsed / units[u]), u);
+};
+
export function ExploreCard({ challenge }: Props) {
return (
<Card className={`duration-300 ${BORDERS_BY_DIFFICULTY[challenge.difficulty]}`}>
@@ -26,11 +52,7 @@ export function ExploreCard({ challenge }: Props) {
<div className="space-y-1">
<CardTitle>{challenge.name}</CardTitle>
<CardDescription className="relative h-48 overflow-hidden pb-4">
- {/* it didn't like the div */}
- {/* <div
- className="pointer-events-none absolute inset-0 h-full w-full"
- style={{ boxShadow: 'inset 0 -1.5rem 1rem -0.5rem #000' }}
- ></div> */}
+ <div className="pointer-events-none absolute inset-0 h-full w-full shadow-[inset_0_-1.5rem_1rem_-0.5rem_hsl(var(--card))]"></div>
{challenge.description}
</CardDescription>
</div>
@@ -45,7 +67,7 @@ export function ExploreCard({ challenge }: Props) {
<ArrowBigUp /> {challenge._count.Vote}
</div>
</div>
- <div>Updated {challenge.updatedAt.toLocaleDateString()}</div>
+ <div>Updated {getRelativeTime(challenge.updatedAt)}</div>
</div>
</CardContent>
</Card>
diff --git a/src/components/ui/card.tsx b/src/components/ui/card.tsx
index 7c466a29..6cf3a9e2 100644
--- a/src/components/ui/card.tsx
+++ b/src/components/ui/card.tsx
@@ -31,12 +31,11 @@ const CardTitle = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HT
);
CardTitle.displayName = 'CardTitle';
-const CardDescription = React.forwardRef<
- HTMLParagraphElement,
- React.HTMLAttributes<HTMLParagraphElement>
->(({ className, ...props }, ref) => (
- <p ref={ref} className={cn('text-sm text-muted-foreground', className)} {...props} />
-));
+const CardDescription = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(
+ ({ className, ...props }, ref) => (
+ <div ref={ref} className={cn('text-sm text-muted-foreground', className)} {...props} />
+ ),
+);
CardDescription.displayName = 'CardDescription';
const CardContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(