-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubTaskCard.tsx
More file actions
248 lines (232 loc) · 6.38 KB
/
Copy pathSubTaskCard.tsx
File metadata and controls
248 lines (232 loc) · 6.38 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { Pecha } from "@/components/ui/shadimport";
import { IoMdClose } from "react-icons/io";
import { FaMinus } from "react-icons/fa6";
import InlineImageUpload from "@/components/ui/molecules/form-upload/InlineImageUpload";
import {
VideoContent,
AudioContent,
ContentIcon,
} from "../content-sub/ContentComponents";
import { getYouTubeDuration } from "@/lib/utils";
interface VideoSubTask {
id?: string | null;
content_type: "VIDEO";
content: string;
display_order?: number;
duration?: string;
}
interface TextSubTask {
id?: string | null;
content_type: "TEXT";
content: string;
display_order?: number;
}
interface AudioSubTask {
id?: string | null;
content_type: "AUDIO";
content: string;
display_order?: number;
}
interface ImageSubTask {
id?: string | null;
content_type: "IMAGE";
content: string | null;
imagePreview: string | null;
display_order?: number;
}
interface SourceSubTask {
id?: string | null;
content_type: "SOURCE_REFERENCE";
content: string;
display_order?: number;
source_text_id?: string | null;
pecha_segment_id?: string | null;
segment_ids?: string[] | null;
}
export type SubTask =
| VideoSubTask
| TextSubTask
| AudioSubTask
| ImageSubTask
| SourceSubTask;
interface SubTaskCardProps {
subTask: SubTask;
index: number;
onUpdate: (index: number, updates: Partial<SubTask>) => void;
onRemove: (index: number) => void;
onImageUpload: (index: number, file: File) => void;
onRemoveImage: (index: number) => void;
}
const VideoSubtask = ({
subTask,
index,
onUpdate,
}: {
subTask: VideoSubTask;
index: number;
onUpdate: (index: number, updates: Partial<SubTask>) => void;
}) => {
const handleUrlChange = async (url: string) => {
onUpdate(index, { content: url, duration: "" });
// Fetch duration if it's a valid YouTube URL
if (url && (url.includes("youtube.com") || url.includes("youtu.be"))) {
try {
const duration = await getYouTubeDuration(url);
onUpdate(index, { content: url, duration });
} catch (error) {
console.error("Failed to fetch duration:", error);
onUpdate(index, { content: url, duration: "" });
}
}
};
return (
<>
<Pecha.Input
type="url"
placeholder="Enter YouTube URL"
className="h-12 text-base bg-[#FAFAFA] dark:bg-sidebar-secondary "
value={subTask.content}
onChange={(e) => handleUrlChange(e.target.value)}
/>
<VideoContent content={subTask.content} />
</>
);
};
const TextSubtask = ({
subTask,
index,
onUpdate,
}: {
subTask: TextSubTask;
index: number;
onUpdate: (index: number, updates: Partial<SubTask>) => void;
}) => (
<Pecha.Textarea
placeholder="Enter your text content"
className="w-full min-h-64 resize-none text-base bg-[#FAFAFA] dark:bg-sidebar-secondary "
value={subTask.content}
onChange={(e) => onUpdate(index, { content: e.target.value })}
/>
);
const AudioSubtask = ({
subTask,
index,
onUpdate,
}: {
subTask: AudioSubTask;
index: number;
onUpdate: (index: number, updates: Partial<SubTask>) => void;
}) => (
<>
<Pecha.Input
type="url"
placeholder="Enter Spotify or SoundCloud URL"
className="h-12 text-base bg-[#FAFAFA] dark:bg-sidebar-secondary "
value={subTask.content}
onChange={(e) => onUpdate(index, { content: e.target.value })}
/>
{subTask.content && <AudioContent content={subTask.content} />}
</>
);
const ImageSubtask = ({
subTask,
index,
onImageUpload,
onRemoveImage,
}: {
subTask: ImageSubTask;
index: number;
onImageUpload: (index: number, file: File) => void;
onRemoveImage: (index: number) => void;
}) => (
<>
{!subTask.imagePreview && (
<InlineImageUpload
onUpload={(file) => onImageUpload(index, file)}
uploadedImage={subTask.imagePreview}
/>
)}
{subTask.imagePreview && (
<div className="mt-4 flex w-full justify-center bg-[#FAFAFA] dark:bg-sidebar-secondary ">
<div className="relative">
<img
src={subTask.imagePreview}
alt="Final uploaded image"
className="w-full h-48 object-cover rounded-lg border"
/>
<Pecha.Button
variant="default"
className="absolute top-2 right-2"
type="button"
onClick={() => onRemoveImage(index)}
data-testid="remove-image-button"
>
<FaMinus className="w-4 h-4" />
</Pecha.Button>
</div>
</div>
)}
</>
);
const SourceSubtask = ({ subTask }: { subTask: SourceSubTask }) => (
<div className="w-full min-h-12 bg-[#FAFAFA] dark:bg-sidebar-secondary whitespace-pre-wrap text-base p-3 border rounded-md border-dashed border-gray-300 dark:border-[#313132]">
<div dangerouslySetInnerHTML={{ __html: subTask.content }} />
</div>
);
export const SubTaskCard = ({
subTask,
index,
onUpdate,
onRemove,
onImageUpload,
onRemoveImage,
}: SubTaskCardProps) => {
const renderContent = () => {
switch (subTask.content_type) {
case "VIDEO":
return (
<VideoSubtask subTask={subTask} index={index} onUpdate={onUpdate} />
);
case "TEXT":
return (
<TextSubtask subTask={subTask} index={index} onUpdate={onUpdate} />
);
case "AUDIO":
return (
<AudioSubtask subTask={subTask} index={index} onUpdate={onUpdate} />
);
case "IMAGE":
return (
<ImageSubtask
subTask={subTask}
index={index}
onImageUpload={onImageUpload}
onRemoveImage={onRemoveImage}
/>
);
case "SOURCE_REFERENCE":
return <SourceSubtask subTask={subTask} />;
}
};
return (
<div
key={index}
className={`border border-gray-300 bg-[#ffffff] dark:bg-[#161616] dark:border-input rounded-sm p-2 space-y-4`}
>
<div className="flex items-center justify-between">
<div className="flex items-center bg-[#F7F7F7] border dark:bg-sidebar-secondary px-2 py-1 text-sm rounded-md border-dashed gap-2">
<ContentIcon type={subTask.content_type} />
{subTask.content_type}
</div>
<Pecha.Button
variant="outline"
type="button"
onClick={() => onRemove(index)}
>
<IoMdClose className="w-4 h-4" />
</Pecha.Button>
</div>
{renderContent()}
</div>
);
};