-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathmain-card.tsx
253 lines (235 loc) · 9.05 KB
/
main-card.tsx
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
249
250
251
252
253
"use client";
import { useState, useEffect } from "react";
import { useRouter } from "next/navigation";
import { Card } from "~/components/ui/card";
import { Input } from "~/components/ui/input";
import { Button } from "~/components/ui/button";
import { Sparkles } from "lucide-react";
import React from "react";
import { CustomizationDropdown } from "./customization-dropdown";
import { exampleRepos } from "~/lib/exampleRepos";
import { ExportDropdown } from "./export-dropdown";
import { ChevronUp, ChevronDown } from "lucide-react";
import { Switch } from "~/components/ui/switch";
interface MainCardProps {
isHome?: boolean;
username?: string;
repo?: string;
showCustomization?: boolean;
onModify?: (instructions: string) => void;
onRegenerate?: (instructions: string) => void;
onCopy?: () => void;
lastGenerated?: Date;
onExportImage?: () => void;
zoomingEnabled?: boolean;
onZoomToggle?: () => void;
loading?: boolean;
}
export default function MainCard({
isHome = true,
username,
repo,
showCustomization,
onModify,
onRegenerate,
onCopy,
lastGenerated,
onExportImage,
zoomingEnabled,
onZoomToggle,
loading,
}: MainCardProps) {
const [repoUrl, setRepoUrl] = useState("");
const [error, setError] = useState("");
const [activeDropdown, setActiveDropdown] = useState<
"customize" | "export" | null
>(null);
const router = useRouter();
useEffect(() => {
if (username && repo) {
setRepoUrl(`https://github.com/${username}/${repo}`);
}
}, [username, repo]);
useEffect(() => {
if (loading) {
setActiveDropdown(null);
}
}, [loading]);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
setError("");
const githubUrlPattern =
/^https?:\/\/github\.com\/([a-zA-Z0-9-_]+)\/([a-zA-Z0-9-_\.]+)\/?$/;
const match = githubUrlPattern.exec(repoUrl.trim());
if (!match) {
setError("Please enter a valid GitHub repository URL");
return;
}
const [, username, repo] = match || [];
if (!username || !repo) {
setError("Invalid repository URL format");
return;
}
const sanitizedUsername = encodeURIComponent(username);
const sanitizedRepo = encodeURIComponent(repo);
router.push(`/${sanitizedUsername}/${sanitizedRepo}`);
};
const handleExampleClick = (repoPath: string, e: React.MouseEvent) => {
e.preventDefault();
router.push(repoPath);
};
const handleDropdownToggle = (dropdown: "customize" | "export") => {
setActiveDropdown(activeDropdown === dropdown ? null : dropdown);
};
return (
<Card className="relative w-full max-w-3xl border-[3px] border-black bg-purple-200 p-4 shadow-[8px_8px_0_0_#000000] sm:p-8">
<form onSubmit={handleSubmit} className="space-y-4 sm:space-y-6">
<div className="flex flex-col gap-3 sm:flex-row sm:gap-4">
<Input
placeholder="https://github.com/username/repo"
className="flex-1 rounded-md border-[3px] border-black px-3 py-4 text-base font-bold shadow-[4px_4px_0_0_#000000] placeholder:text-base placeholder:font-normal placeholder:text-gray-700 sm:px-4 sm:py-6 sm:text-lg sm:placeholder:text-lg"
value={repoUrl}
onChange={(e) => setRepoUrl(e.target.value)}
required
/>
<Button
tabIndex={0}
type="submit"
className="border-[3px] border-black bg-purple-400 p-4 px-4 text-base text-black shadow-[4px_4px_0_0_#000000] transition-transform hover:-translate-x-0.5 hover:-translate-y-0.5 hover:transform hover:bg-purple-400 sm:p-6 sm:px-6 sm:text-lg"
>
Diagram
</Button>
</div>
{error && <p className="text-sm text-red-600">{error}</p>}
{/* Dropdowns Container */}
{!isHome && (
<div className="space-y-4">
{/* Only show buttons and dropdowns when not loading */}
{!loading && (
<>
{/* Buttons Container */}
<div className="flex flex-col items-center gap-4 sm:flex-row sm:gap-4">
{showCustomization &&
onModify &&
onRegenerate &&
lastGenerated && (
<button
onClick={(e) => {
e.preventDefault();
handleDropdownToggle("customize");
}}
className={`flex items-center justify-between gap-2 rounded-md border-[3px] border-black px-4 py-2 font-medium text-black transition-colors sm:max-w-[250px] ${
activeDropdown === "customize"
? "bg-purple-400"
: "bg-purple-300 hover:bg-purple-400"
}`}
tabIndex={0}
>
<span>Customize Diagram</span>
{activeDropdown === "customize" ? (
<ChevronUp size={20} role="img" aria-label="Customize Diagram"/>
) : (
<ChevronDown size={20} role="img" aria-label="Customize Diagram"/>
)}
</button>
)}
{onCopy && lastGenerated && onExportImage && (
<div className="flex flex-col items-center justify-center gap-2">
<button
onClick={(e) => {
e.preventDefault();
handleDropdownToggle("export");
}}
className={`flex items-center justify-between gap-2 rounded-md border-[3px] border-black px-4 py-2 font-medium text-black transition-colors sm:max-w-[250px] ${
activeDropdown === "export"
? "bg-purple-400"
: "bg-purple-300 hover:bg-purple-400"
}`}
tabIndex={0}
>
<span>Export Diagram</span>
{activeDropdown === "export" ? (
<ChevronUp size={20} role="img" aria-label="Export Diagram"/>
) : (
<ChevronDown size={20} role="img" aria-label="Export Diagram"/>
)}
</button>
</div>
)}
{lastGenerated && (
<>
<label className="font-medium text-black" htmlFor="zoom-toggle">
Enable Zoom
</label>
<Switch
id="zoom-toggle"
checked={zoomingEnabled}
onCheckedChange={onZoomToggle}
/>
</>
)}
</div>
{/* Dropdown Content */}
<div
className={`transition-all duration-200 ${
activeDropdown
? "pointer-events-auto max-h-[500px] opacity-100"
: "pointer-events-none max-h-0 opacity-0"
}`}
>
{activeDropdown === "customize" && (
<CustomizationDropdown
onModify={onModify!}
onRegenerate={onRegenerate!}
lastGenerated={lastGenerated!}
isOpen={true}
/>
)}
{activeDropdown === "export" && (
<ExportDropdown
onCopy={onCopy!}
lastGenerated={lastGenerated!}
onExportImage={onExportImage!}
isOpen={true}
/>
)}
</div>
</>
)}
</div>
)}
{/* Example Repositories */}
{isHome && (
<div className="space-y-2">
<div className="text-sm text-gray-700 sm:text-base">
Try these example repositories:
</div>
<div className="flex flex-wrap gap-2">
{Object.entries(exampleRepos).map(([name, path]) => (
<Button
key={name}
variant="outline"
className="border-2 border-black bg-purple-400 text-sm text-black transition-transform hover:-translate-y-0.5 hover:transform hover:bg-purple-300 sm:text-base"
onClick={(e) => handleExampleClick(path, e)}
tabIndex={0}
>
{name}
</Button>
))}
</div>
</div>
)}
</form>
{/* Decorative Sparkle */}
<div className="absolute -bottom-8 -left-12 hidden sm:block">
<Sparkles
className="h-20 w-20 fill-sky-400 text-black"
strokeWidth={0.6}
style={{ transform: "rotate(-15deg)" }}
role="img"
aria-hidden
/>
</div>
</Card>
);
}