forked from Nutlope/llamacoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-runner-react.tsx
More file actions
218 lines (208 loc) · 7.53 KB
/
Copy pathcode-runner-react.tsx
File metadata and controls
218 lines (208 loc) · 7.53 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
"use client";
import * as shadcnComponents from "@/lib/shadcn";
import {
SandpackPreview,
SandpackProvider,
useSandpack,
} from "@codesandbox/sandpack-react/unstyled";
import dedent from "dedent";
import { CheckIcon, CopyIcon } from "lucide-react";
import { useState } from "react";
export default function ReactCodeRunner({
code,
onRequestFix,
}: {
code: string;
onRequestFix?: (e: string) => void;
}) {
return (
<SandpackProvider
key={code}
template="react-ts"
className="relative h-full w-full [&_.sp-preview-container]:flex [&_.sp-preview-container]:h-full [&_.sp-preview-container]:w-full [&_.sp-preview-container]:grow [&_.sp-preview-container]:flex-col [&_.sp-preview-container]:justify-center [&_.sp-preview-iframe]:grow"
files={{
"App.tsx": code,
...shadcnFiles,
"/tsconfig.json": {
code: `{
"include": [
"./**/*"
],
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"lib": [ "dom", "es2015" ],
"jsx": "react-jsx",
"baseUrl": "./",
"paths": {
"@/components/*": ["components/*"]
}
}
}
`,
},
}}
options={{
externalResources: [
"https://unpkg.com/@tailwindcss/ui/dist/tailwind-ui.min.css",
],
}}
customSetup={{
dependencies,
}}
>
<SandpackPreview
showNavigator={false}
showOpenInCodeSandbox={false}
showRefreshButton={false}
showRestartButton={false}
showOpenNewtab={false}
className="h-full w-full"
/>
{onRequestFix && <ErrorMessage onRequestFix={onRequestFix} />}
</SandpackProvider>
);
}
function ErrorMessage({ onRequestFix }: { onRequestFix: (e: string) => void }) {
const { sandpack } = useSandpack();
const [didCopy, setDidCopy] = useState(false);
if (!sandpack.error) return null;
return (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-4 bg-white/5 text-base backdrop-blur-sm">
<div className="max-w-[400px] rounded-md bg-red-500 p-4 text-white shadow-xl shadow-black/20">
<p className="text-lg font-medium">Error</p>
<p className="mt-4 line-clamp-[10] overflow-x-auto whitespace-pre font-mono text-xs">
{sandpack.error.message}
</p>
<div className="mt-8 flex justify-between gap-4">
<button
onClick={async () => {
if (!sandpack.error) return;
setDidCopy(true);
await window.navigator.clipboard.writeText(
sandpack.error.message,
);
await new Promise((resolve) => setTimeout(resolve, 2000));
setDidCopy(false);
}}
className="rounded border-red-300 px-2.5 py-1.5 text-sm font-semibold text-red-50"
>
{didCopy ? <CheckIcon size={18} /> : <CopyIcon size={18} />}
</button>
<button
onClick={() => {
if (!sandpack.error) return;
onRequestFix(sandpack.error.message);
}}
className="rounded bg-white px-2.5 py-1.5 text-sm font-medium text-black"
>
Try to fix
</button>
</div>
</div>
</div>
);
}
const shadcnFiles = {
"/lib/utils.ts": shadcnComponents.utils,
"/components/ui/accordion.tsx": shadcnComponents.accordian,
"/components/ui/alert-dialog.tsx": shadcnComponents.alertDialog,
"/components/ui/alert.tsx": shadcnComponents.alert,
"/components/ui/avatar.tsx": shadcnComponents.avatar,
"/components/ui/badge.tsx": shadcnComponents.badge,
"/components/ui/breadcrumb.tsx": shadcnComponents.breadcrumb,
"/components/ui/button.tsx": shadcnComponents.button,
"/components/ui/calendar.tsx": shadcnComponents.calendar,
"/components/ui/card.tsx": shadcnComponents.card,
"/components/ui/carousel.tsx": shadcnComponents.carousel,
"/components/ui/checkbox.tsx": shadcnComponents.checkbox,
"/components/ui/collapsible.tsx": shadcnComponents.collapsible,
"/components/ui/dialog.tsx": shadcnComponents.dialog,
"/components/ui/drawer.tsx": shadcnComponents.drawer,
"/components/ui/dropdown-menu.tsx": shadcnComponents.dropdownMenu,
"/components/ui/input.tsx": shadcnComponents.input,
"/components/ui/label.tsx": shadcnComponents.label,
"/components/ui/menubar.tsx": shadcnComponents.menuBar,
"/components/ui/navigation-menu.tsx": shadcnComponents.navigationMenu,
"/components/ui/pagination.tsx": shadcnComponents.pagination,
"/components/ui/popover.tsx": shadcnComponents.popover,
"/components/ui/progress.tsx": shadcnComponents.progress,
"/components/ui/radio-group.tsx": shadcnComponents.radioGroup,
"/components/ui/select.tsx": shadcnComponents.select,
"/components/ui/separator.tsx": shadcnComponents.separator,
"/components/ui/skeleton.tsx": shadcnComponents.skeleton,
"/components/ui/slider.tsx": shadcnComponents.slider,
"/components/ui/switch.tsx": shadcnComponents.switchComponent,
"/components/ui/table.tsx": shadcnComponents.table,
"/components/ui/tabs.tsx": shadcnComponents.tabs,
"/components/ui/textarea.tsx": shadcnComponents.textarea,
"/components/ui/toast.tsx": shadcnComponents.toast,
"/components/ui/toaster.tsx": shadcnComponents.toaster,
"/components/ui/toggle-group.tsx": shadcnComponents.toggleGroup,
"/components/ui/toggle.tsx": shadcnComponents.toggle,
"/components/ui/tooltip.tsx": shadcnComponents.tooltip,
"/components/ui/use-toast.tsx": shadcnComponents.useToast,
"/components/ui/index.tsx": `
export * from "./button"
export * from "./card"
export * from "./input"
export * from "./label"
export * from "./select"
export * from "./textarea"
export * from "./avatar"
export * from "./radio-group"
`,
"/public/index.html": dedent`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<div id="root"></div>
</body>
</html>
`,
};
const dependencies = {
"lucide-react": "latest",
recharts: "2.9.0",
"react-router-dom": "latest",
"@radix-ui/react-accordion": "^1.2.0",
"@radix-ui/react-alert-dialog": "^1.1.1",
"@radix-ui/react-aspect-ratio": "^1.1.0",
"@radix-ui/react-avatar": "^1.1.0",
"@radix-ui/react-checkbox": "^1.1.1",
"@radix-ui/react-collapsible": "^1.1.0",
"@radix-ui/react-dialog": "^1.1.1",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-hover-card": "^1.1.1",
"@radix-ui/react-label": "^2.1.0",
"@radix-ui/react-menubar": "^1.1.1",
"@radix-ui/react-navigation-menu": "^1.2.0",
"@radix-ui/react-popover": "^1.1.1",
"@radix-ui/react-progress": "^1.1.0",
"@radix-ui/react-radio-group": "^1.2.0",
"@radix-ui/react-select": "^2.1.1",
"@radix-ui/react-separator": "^1.1.0",
"@radix-ui/react-slider": "^1.2.0",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-switch": "^1.1.0",
"@radix-ui/react-tabs": "^1.1.0",
"@radix-ui/react-toast": "^1.2.1",
"@radix-ui/react-toggle": "^1.1.0",
"@radix-ui/react-toggle-group": "^1.1.0",
"@radix-ui/react-tooltip": "^1.1.2",
"class-variance-authority": "^0.7.0",
clsx: "^2.1.1",
"date-fns": "^3.6.0",
"embla-carousel-react": "^8.1.8",
"react-day-picker": "^8.10.1",
"tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7",
"framer-motion": "^11.15.0",
vaul: "^0.9.1",
};