-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic_input.tsx
More file actions
126 lines (117 loc) · 4.16 KB
/
Copy pathtopic_input.tsx
File metadata and controls
126 lines (117 loc) · 4.16 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { FaRegTrashCan } from "react-icons/fa6";
import { RxCross2 } from "react-icons/rx";
import { TopicTagAlt } from "@/components/topic_tag";
interface Topic {
id: number;
name: string;
color_hex: number;
}
type TopicSelection =
| { type: "existing"; id: number }
| { type: "new"; name: string; color_hex: number };
interface TopicInputProps {
availableTopics: Topic[];
topics: TopicSelection[];
setTopics: React.Dispatch<React.SetStateAction<TopicSelection[]>>;
}
export function TopicInput({
availableTopics,
topics,
setTopics,
}: TopicInputProps) {
return (
<div className="topic-input flex w-full flex-col items-center justify-center gap-2 text-slate-200">
<h1 className="topic-input-title text-xl hover:brightness-110">Topics</h1>
{/* Selected Topics Display */}
<div className="topics-container flex w-full flex-row flex-wrap items-center justify-start gap-2">
{topics.map((topic, index) => {
if (topic.type === "existing") {
const topicData = availableTopics.find((t) => t.id === topic.id);
if (!topicData) return null;
return (
<div key={index} className="flex w-fit flex-row gap-2 rounded-lg">
<TopicTagAlt
name={topicData.name}
color_hex={topicData.color_hex}
/>
<button
className="hover:brightness-90"
type="button"
onClick={() =>
setTopics(topics.filter((_, i) => i !== index))
}
>
<RxCross2 className="hover:text-red-500" />
</button>
</div>
);
}
return (
<div
key={index}
className="new-topic-input flex w-full flex-row justify-evenly gap-2 rounded-lg bg-indigo-400 p-2 brightness-90 hover:brightness-95"
>
<input
className="color-select h-5 w-5 border-0"
type="color"
value={`#${topic.color_hex.toString(16).padStart(6, "0")}`}
onChange={(e) => {
const hex = parseInt(e.target.value.replace("#", ""), 16);
const copy = [...topics];
copy[index] = { ...topic, color_hex: hex };
setTopics(copy);
}}
/>
<input
className="topic-name-input w-[75%] bg-indigo-400 text-justify placeholder:text-slate-300 hover:brightness-110"
type="text"
placeholder="Topic name..."
value={topic.name}
onChange={(e) => {
const newTopics = [...topics];
newTopics[index] = { ...topic, name: e.target.value };
setTopics(newTopics);
}}
/>
<button
className="hover:brightness-90"
type="button"
onClick={() => setTopics(topics.filter((_, i) => i !== index))}
>
<FaRegTrashCan className="hover:text-red-500" />
</button>
</div>
);
})}
</div>
{/* Existing Topic Selection */}
<select
className="topic-select w-full rounded-lg bg-indigo-400 p-2 text-center brightness-90 hover:brightness-110"
onChange={(e) => {
const id = Number(e.target.value);
if (!id) return;
if (!topics.some((t) => t.type === "existing" && t.id === id)) {
setTopics([...topics, { type: "existing", id }]);
}
e.target.value = "";
}}
>
<option value="">Add Existing Topic</option>
{availableTopics.map((topic) => (
<option key={topic.id} value={topic.id}>
{topic.name}
</option>
))}
</select>
<button
type="button"
className="add-topic-button w-12 rounded-full bg-indigo-400 text-xl hover:brightness-110"
onClick={() =>
setTopics([...topics, { type: "new", name: "", color_hex: 0xffffff }])
}
>
+
</button>
</div>
);
}