forked from recodehive/recode-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDailyCodingTip.tsx
More file actions
85 lines (76 loc) · 2.34 KB
/
Copy pathDailyCodingTip.tsx
File metadata and controls
85 lines (76 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, { useState, useEffect } from "react";
import styles from "./DailyCodingTip.module.css";
import codingTips from "../data/codingTips.json";
type Tip = {
id: number;
category: string;
tip: string;
};
const CATEGORY_COLORS: Record<string, string> = {
"Git & GitHub Tips": "#f97316",
"JavaScript/React Tips": "#3b82f6",
"Open Source Contribution Tips": "#10b981",
"VS Code Shortcuts": "#8b5cf6",
"Productivity Tricks": "#ec4899",
};
function getTipOfTheDay(): Tip {
try {
const stored = localStorage.getItem("dailyCodingTip");
const today = new Date().toDateString();
if (stored) {
const parsed = JSON.parse(stored);
if (parsed.date === today) return parsed.tip;
}
const randomTip = codingTips[Math.floor(Math.random() * codingTips.length)] as Tip;
localStorage.setItem("dailyCodingTip", JSON.stringify({ date: today, tip: randomTip }));
return randomTip;
} catch {
return codingTips[0] as Tip;
}
}
export default function DailyCodingTip(): React.ReactElement {
const [tip, setTip] = useState<Tip | null>(null);
const [animate, setAnimate] = useState(false);
useEffect(() => {
setTip(getTipOfTheDay());
setAnimate(true);
}, []);
const handleRefresh = () => {
const randomTip = codingTips[Math.floor(Math.random() * codingTips.length)] as Tip;
setAnimate(false);
setTimeout(() => {
setTip(randomTip);
setAnimate(true);
}, 150);
};
if (!tip) return <></>;
const categoryColor = CATEGORY_COLORS[tip.category] || "#6b7280";
return (
<div className={styles.container}>
<div className={styles.header}>
<span className={styles.headerIcon}>💡</span>
<h3 className={styles.headerTitle}>Daily Coding Tip</h3>
</div>
<div className={`${styles.tipCard} ${animate ? styles.fadeIn : ""}`}>
<span
className={styles.categoryBadge}
style={{
backgroundColor: `${categoryColor}20`,
color: categoryColor,
borderColor: `${categoryColor}40`,
}}
>
{tip.category}
</span>
<p className={styles.tipText}>{tip.tip}</p>
</div>
<button
className={styles.refreshButton}
onClick={handleRefresh}
aria-label="Get a new random coding tip"
>
🔀 Get Another Tip
</button>
</div>
);
}