ZuhairAhmed commited on
Commit
9f43bd2
·
1 Parent(s): 923b915

downgrade to next 14, theme handling using next-themes and made used of app api router

Browse files
.eslintrc.json CHANGED
@@ -1,10 +1,14 @@
1
  {
2
  "extends": ["next/core-web-vitals", "next/typescript"],
 
3
  "rules": {
4
  "react/no-unescaped-entities": "off",
5
  "@next/next/no-page-custom-font": "off",
6
  "@typescript-eslint/no-unused-vars": "off",
7
  "@next/next/no-assign-module-variable": "off",
8
- "react/display-name": "off"
 
 
 
9
  }
10
  }
 
1
  {
2
  "extends": ["next/core-web-vitals", "next/typescript"],
3
+ "ignorePatterns": ["src/components/generated/*"],
4
  "rules": {
5
  "react/no-unescaped-entities": "off",
6
  "@next/next/no-page-custom-font": "off",
7
  "@typescript-eslint/no-unused-vars": "off",
8
  "@next/next/no-assign-module-variable": "off",
9
+ "react/display-name": "off",
10
+ "@next/next/no-img-element": "off",
11
+ "@typescript-eslint/no-empty-object-type": "off",
12
+ "@typescript-eslint/ban-ts-comment": "off"
13
  }
14
  }
next.config.ts → next.config.mjs RENAMED
@@ -1,9 +1,5 @@
1
- import type { NextConfig } from "next";
2
-
3
- const nextConfig: NextConfig = {
4
- experimental: {
5
- externalDir: true,
6
- },
7
  images: {
8
  domains: [],
9
  remotePatterns: [
 
1
+ /** @type {import('next').NextConfig} */
2
+ const nextConfig = {
 
 
 
 
3
  images: {
4
  domains: [],
5
  remotePatterns: [
package-lock.json CHANGED
The diff for this file is too large to render. See raw diff
 
package.json CHANGED
@@ -3,7 +3,7 @@
3
  "version": "0.1.0",
4
  "private": true,
5
  "scripts": {
6
- "dev": "next dev --turbopack",
7
  "build": "next build",
8
  "start": "next start",
9
  "lint": "next lint"
@@ -44,7 +44,7 @@
44
  "embla-carousel-react": "^8.5.1",
45
  "input-otp": "^1.4.1",
46
  "lucide-react": "^0.460.0",
47
- "next": "15.0.3",
48
  "next-themes": "^0.4.3",
49
  "react": "19.0.0-rc-66855b96-20241106",
50
  "react-day-picker": "^8.10.1",
 
3
  "version": "0.1.0",
4
  "private": true,
5
  "scripts": {
6
+ "dev": "next dev",
7
  "build": "next build",
8
  "start": "next start",
9
  "lint": "next lint"
 
44
  "embla-carousel-react": "^8.5.1",
45
  "input-otp": "^1.4.1",
46
  "lucide-react": "^0.460.0",
47
+ "next": "14.2.16",
48
  "next-themes": "^0.4.3",
49
  "react": "19.0.0-rc-66855b96-20241106",
50
  "react-day-picker": "^8.10.1",
pages/api/sendMessage.js → src/app/api/new/route.ts RENAMED
@@ -1,17 +1,17 @@
1
- import { generateUUID } from "@/lib/utils";
2
  import { promises as fs } from "fs";
 
 
3
 
4
- const url = "https://web-zero-web-zero.hf.space/api/v1/run/new?stream=false";
5
-
6
- export default async function handler(req, res) {
7
- if (req.method !== "POST") {
8
- res.status(405).json({ error: "Method not allowed" });
9
- return;
10
- }
11
 
12
- const { content } = req.body;
13
 
 
14
  try {
 
 
 
15
  const response = await fetch(url, {
16
  method: "POST",
17
  headers: {
@@ -29,19 +29,29 @@ export default async function handler(req, res) {
29
  const code = data.outputs[0].outputs[0].outputs.message.message.text;
30
  const formattedCode = code.replace(/^```tsx\n|\n```$/g, "");
31
 
32
- const id = generateUUID().toString();
33
- const filename = `generated/${id}.tsx`;
 
 
 
 
 
 
 
 
34
 
35
- await fs.mkdir("generated", { recursive: true });
36
  await fs.writeFile(filename, formattedCode, "utf8");
37
 
38
- res.status(200).json({
39
  id,
40
  from: "ai",
41
  content: formattedCode,
42
  });
43
  } catch (error) {
44
  console.error("Error in sendMessage API:", error);
45
- res.status(500).json({ error: "Failed to process the request" });
 
 
 
46
  }
47
  }
 
1
+ import { NextRequest, NextResponse } from "next/server";
2
  import { promises as fs } from "fs";
3
+ import path from "path";
4
+ import { v4 as uuidv4 } from "uuid";
5
 
6
+ export const runtime = "nodejs";
 
 
 
 
 
 
7
 
8
+ const url = "https://web-zero-web-zero.hf.space/api/v1/run/new?stream=false";
9
 
10
+ export async function POST(req: NextRequest) {
11
  try {
12
+ const body = await req.json();
13
+ const { content } = body;
14
+
15
  const response = await fetch(url, {
16
  method: "POST",
17
  headers: {
 
29
  const code = data.outputs[0].outputs[0].outputs.message.message.text;
30
  const formattedCode = code.replace(/^```tsx\n|\n```$/g, "");
31
 
32
+ const id = uuidv4();
33
+ const generatedDir = path.resolve(
34
+ process.cwd(),
35
+ "src",
36
+ "components",
37
+ "generated"
38
+ );
39
+ const filename = path.join(generatedDir, `${id}.tsx`);
40
+
41
+ await fs.mkdir(generatedDir, { recursive: true });
42
 
 
43
  await fs.writeFile(filename, formattedCode, "utf8");
44
 
45
+ return NextResponse.json({
46
  id,
47
  from: "ai",
48
  content: formattedCode,
49
  });
50
  } catch (error) {
51
  console.error("Error in sendMessage API:", error);
52
+ return NextResponse.json(
53
+ { error: "Failed to process the request" },
54
+ { status: 500 }
55
+ );
56
  }
57
  }
src/app/layout.tsx CHANGED
@@ -14,21 +14,12 @@ export default function RootLayout({
14
  children: React.ReactNode;
15
  }>) {
16
  return (
17
- <html lang="en">
18
- <head>
19
- <script dangerouslySetInnerHTML={{ __html: "var exports = {};" }} />
20
- </head>
21
  <body className="antialiased overflow-hidden">
22
- {/* <ThemeProvider
23
- attribute="class"
24
- defaultTheme="light"
25
- enableSystem
26
- disableTransitionOnChange
27
- storageKey="theme"
28
- > */}
29
- <main>{children}</main>
30
- <Toaster />
31
- {/* </ThemeProvider> */}
32
  </body>
33
  </html>
34
  );
 
14
  children: React.ReactNode;
15
  }>) {
16
  return (
17
+ <html lang="en" suppressHydrationWarning>
 
 
 
18
  <body className="antialiased overflow-hidden">
19
+ <ThemeProvider>
20
+ <main>{children}</main>
21
+ <Toaster />
22
+ </ThemeProvider>
 
 
 
 
 
 
23
  </body>
24
  </html>
25
  );
src/components/ThemeToggle.tsx ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ "use client";
2
+ import { useTheme } from "next-themes";
3
+ import { Moon, Sun } from "lucide-react";
4
+ import { Button } from "@/components/ui/button";
5
+
6
+ export default function ThemeToggle() {
7
+ const { theme, setTheme } = useTheme();
8
+
9
+ return (
10
+ <Button
11
+ variant="ghost"
12
+ size="icon"
13
+ onClick={() => setTheme(theme === "dark" ? "light" : "dark")}
14
+ >
15
+ {theme === "dark" ? <Sun /> : <Moon />}
16
+ </Button>
17
+ );
18
+ }
src/components/theme-provider.tsx CHANGED
@@ -3,9 +3,16 @@
3
  import * as React from "react";
4
  import { ThemeProvider as NextThemesProvider } from "next-themes";
5
 
6
- export function ThemeProvider({
7
- children,
8
- ...props
9
- }: React.ComponentProps<typeof NextThemesProvider>) {
10
- return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
 
 
 
 
 
 
 
11
  }
 
3
  import * as React from "react";
4
  import { ThemeProvider as NextThemesProvider } from "next-themes";
5
 
6
+ export function ThemeProvider({ children }: React.PropsWithChildren<{}>) {
7
+ return (
8
+ <NextThemesProvider
9
+ attribute="class"
10
+ defaultTheme="system"
11
+ enableSystem
12
+ disableTransitionOnChange
13
+ storageKey="theme"
14
+ >
15
+ {children}
16
+ </NextThemesProvider>
17
+ );
18
  }
src/webzero/chat-page.tsx CHANGED
@@ -1,6 +1,7 @@
1
  "use client";
2
 
3
- import { useState, useEffect } from "react";
 
4
  import {
5
  Code,
6
  Eye,
@@ -17,14 +18,15 @@ import { Button } from "@/components/ui/button";
17
  import { Textarea } from "@/components/ui/textarea";
18
  import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
19
  import { ScrollArea } from "@/components/ui/scroll-area";
20
- import { Message, PreviewState } from "../lib/types";
21
- // @ts-expect-error shutup
22
  import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
23
  import { customOneDark, customOneLight, generateUUID } from "@/lib/utils";
24
- import CodeRenderer from "./preview";
 
25
 
26
  const sendMessage = async (content: string) => {
27
- const response = await fetch("/api/sendMessage", {
28
  method: "POST",
29
  headers: {
30
  "Content-Type": "application/json",
@@ -43,12 +45,6 @@ const sendMessage = async (content: string) => {
43
 
44
  export function ChatPage() {
45
  const [message, setMessage] = useState("");
46
- const [isDark, setIsDark] = useState(() => {
47
- if (typeof window !== "undefined") {
48
- return localStorage.getItem("theme") === "dark";
49
- }
50
- return false;
51
- });
52
  const [isLoading, setIsLoading] = useState(false);
53
  const [chatHistory, setChatHistory] = useState<Message[]>([]);
54
  const [selectedMessage, setSelectedMessage] = useState<Message | null>(null);
@@ -57,12 +53,8 @@ export function ChatPage() {
57
  isOpen: false,
58
  });
59
 
60
- useEffect(() => {
61
- localStorage.setItem("theme", isDark ? "dark" : "light");
62
- document.documentElement.classList.toggle("dark", isDark);
63
- }, [isDark]);
64
-
65
- const toggleTheme = () => setIsDark(!isDark);
66
 
67
  const handleSubmit = async (e: React.FormEvent) => {
68
  e.preventDefault();
@@ -109,23 +101,12 @@ export function ChatPage() {
109
  }));
110
  };
111
 
112
- const ThemeChangeButton = () => (
113
- <Button
114
- className="ml-auto"
115
- variant="ghost"
116
- size="icon"
117
- onClick={toggleTheme}
118
- >
119
- {isDark ? <Sun /> : <Moon />}
120
- </Button>
121
- );
122
-
123
  return (
124
  <div className={`min-h-screen flex ${isDark ? "dark" : ""}`}>
125
  {/* Main Content */}
126
  <div className="flex flex-1 flex-col w-full">
127
  <header className="h-16 border-b flex items-center justify-between px-4">
128
- <ThemeChangeButton />
129
  </header>
130
 
131
  <div className="flex flex-1">
@@ -162,9 +143,9 @@ export function ChatPage() {
162
  <form onSubmit={handleSubmit} className="relative">
163
  {isLoading ? (
164
  <div
165
- className={
166
- (isDark ? "loader-dark" : "loader-light") + " loader"
167
- }
168
  />
169
  ) : (
170
  <>
@@ -217,7 +198,7 @@ export function ChatPage() {
217
  </TabsList>
218
 
219
  <div className="p-4 border-b flex justify-end gap-2">
220
- {previewState.isFullscreen && <ThemeChangeButton />}
221
  <Button
222
  variant="outline"
223
  size="sm"
@@ -288,7 +269,7 @@ function CodeMessageCard({ onClick }: { onClick: () => void }) {
288
  className="bg-muted/50 p-3 rounded-lg mb-2 cursor-pointer hover:bg-muted/80 transition-colors flex items-center gap-2"
289
  >
290
  <FileCode2 />
291
- <div className="ml-2 ">
292
  <div className="font-semibold text-sm">Code File</div>
293
  </div>
294
  </div>
 
1
  "use client";
2
 
3
+ import { useState } from "react";
4
+ import { useTheme } from "next-themes"; // Import useTheme for theme management
5
  import {
6
  Code,
7
  Eye,
 
18
  import { Textarea } from "@/components/ui/textarea";
19
  import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
20
  import { ScrollArea } from "@/components/ui/scroll-area";
21
+ import { Message, PreviewState } from "@/lib/types";
22
+ // @ts-expect-error
23
  import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
24
  import { customOneDark, customOneLight, generateUUID } from "@/lib/utils";
25
+ import CodeRenderer from "@/webzero/preview";
26
+ import ThemeToggle from "@/components/ThemeToggle";
27
 
28
  const sendMessage = async (content: string) => {
29
+ const response = await fetch("/api/new", {
30
  method: "POST",
31
  headers: {
32
  "Content-Type": "application/json",
 
45
 
46
  export function ChatPage() {
47
  const [message, setMessage] = useState("");
 
 
 
 
 
 
48
  const [isLoading, setIsLoading] = useState(false);
49
  const [chatHistory, setChatHistory] = useState<Message[]>([]);
50
  const [selectedMessage, setSelectedMessage] = useState<Message | null>(null);
 
53
  isOpen: false,
54
  });
55
 
56
+ const { theme } = useTheme(); // Get the current theme
57
+ const isDark = theme === "dark"; // Determine if the current theme is dark
 
 
 
 
58
 
59
  const handleSubmit = async (e: React.FormEvent) => {
60
  e.preventDefault();
 
101
  }));
102
  };
103
 
 
 
 
 
 
 
 
 
 
 
 
104
  return (
105
  <div className={`min-h-screen flex ${isDark ? "dark" : ""}`}>
106
  {/* Main Content */}
107
  <div className="flex flex-1 flex-col w-full">
108
  <header className="h-16 border-b flex items-center justify-between px-4">
109
+ <ThemeToggle />
110
  </header>
111
 
112
  <div className="flex flex-1">
 
143
  <form onSubmit={handleSubmit} className="relative">
144
  {isLoading ? (
145
  <div
146
+ className={`loader ${
147
+ isDark ? "loader-dark" : "loader-light"
148
+ }`}
149
  />
150
  ) : (
151
  <>
 
198
  </TabsList>
199
 
200
  <div className="p-4 border-b flex justify-end gap-2">
201
+ {previewState.isFullscreen && <ThemeToggle />}
202
  <Button
203
  variant="outline"
204
  size="sm"
 
269
  className="bg-muted/50 p-3 rounded-lg mb-2 cursor-pointer hover:bg-muted/80 transition-colors flex items-center gap-2"
270
  >
271
  <FileCode2 />
272
+ <div className="ml-2">
273
  <div className="font-semibold text-sm">Code File</div>
274
  </div>
275
  </div>
src/webzero/preview.tsx CHANGED
@@ -1,32 +1,24 @@
1
- import React, { useState, useEffect } from "react";
 
2
 
3
  interface DynamicFileProps {
4
  id: string;
5
  }
6
 
7
  const DynamicFileRenderer: React.FC<DynamicFileProps> = ({ id }) => {
8
- const filepath = `../../generated/${id}.tsx`;
9
- const [DynamicComponent, setDynamicComponent] =
10
- useState<React.ComponentType | null>(null);
 
 
 
 
11
 
12
- useEffect(() => {
13
- const importComponent = async () => {
14
- try {
15
- const module = await import(filepath);
16
- setDynamicComponent(() => module.default);
17
- } catch (error) {
18
- console.error("Error loading component:", error);
19
- setDynamicComponent(() => () => <div>Failed to load component</div>);
20
- }
21
- };
22
-
23
- importComponent();
24
- }, [filepath]);
25
-
26
- if (!DynamicComponent) {
27
- return <div>Loading...</div>;
28
- }
29
- return <DynamicComponent />;
30
  };
31
 
32
  export default DynamicFileRenderer;
 
1
+ import React, { Suspense } from "react";
2
+ import dynamic from "next/dynamic";
3
 
4
  interface DynamicFileProps {
5
  id: string;
6
  }
7
 
8
  const DynamicFileRenderer: React.FC<DynamicFileProps> = ({ id }) => {
9
+ const DynamicComponent = dynamic(
10
+ () => import(`../components/generated/${id}.tsx`),
11
+ {
12
+ loading: () => <div>Loading...</div>,
13
+ ssr: false,
14
+ }
15
+ );
16
 
17
+ return (
18
+ <Suspense fallback={<div>Loading...</div>}>
19
+ <DynamicComponent />
20
+ </Suspense>
21
+ );
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  };
23
 
24
  export default DynamicFileRenderer;