|
1 | 1 | "use client";
|
2 | 2 |
|
3 |
| -import { commitMessage } from "@/action"; |
4 |
| -import AiLoading from "@/components/AiLoading"; |
5 |
| -import Navbar from "@/components/Navbar"; |
6 |
| -import ListChat from "@/components/listChat"; |
| 3 | +import { commitChange } from "@/action"; |
7 | 4 | import { Button } from "@/components/ui/button";
|
| 5 | +import { Card } from "@/components/ui/card"; |
8 | 6 | import { Input } from "@/components/ui/input";
|
9 | 7 | import { Label } from "@/components/ui/label";
|
10 | 8 | import { ScrollArea } from "@/components/ui/scroll-area";
|
| 9 | +import { toastVariants } from "@/components/ui/toast"; |
11 | 10 | import { useToast } from "@/components/ui/use-toast";
|
12 |
| -import { Content } from "@google/generative-ai"; |
| 11 | +import { cn } from "@/lib/utils"; |
13 | 12 | import { CornerDownLeft } from "lucide-react";
|
14 | 13 | import React from "react";
|
| 14 | +import dynamic from "next/dynamic"; |
| 15 | +import ListSuggestion from "@/components/listSuggestion"; |
| 16 | +import Loader from "@/components/loader"; |
| 17 | + |
| 18 | +const EmptyScreen = dynamic(() => import("@/components/emptyScreen"), { |
| 19 | + ssr: false, |
| 20 | + loading: () => <Loader />, |
| 21 | +}); |
15 | 22 |
|
16 | 23 | export default function Home() {
|
17 |
| - const [chatHistory, setChatHistory] = React.useState<Content[] | []>([]); |
18 | 24 | const [message, setMessage] = React.useState<string | null>(null);
|
19 | 25 | const [isLoading, setIsLoading] = React.useState(false);
|
| 26 | + const [error, setError] = React.useState<string | null>(null); |
| 27 | + const [commitChanges, setcommitChanges] = React.useState<string | null>(null); |
| 28 | + const [commitMessages, setcommitMessages] = React.useState<string | null>( |
| 29 | + null |
| 30 | + ); |
20 | 31 |
|
21 | 32 | const { toast } = useToast();
|
22 | 33 |
|
23 |
| - const handelSubmit = async (e: React.FormEvent) => { |
24 |
| - e.preventDefault(); |
25 |
| - |
26 |
| - if (!message || message.trim() === "") { |
| 34 | + const handelSubmit = async ({ suggestion }: { suggestion: string }) => { |
| 35 | + if (suggestion === commitChanges) { |
27 | 36 | toast({
|
28 |
| - title: "Error", |
29 |
| - description: "Please enter a message", |
| 37 | + variant: "destructive", |
| 38 | + title: "Uh oh! Something went wrong.", |
| 39 | + description: "error: Please enter a different message.", |
30 | 40 | });
|
31 | 41 | return;
|
32 | 42 | }
|
33 | 43 |
|
34 | 44 | setIsLoading(true);
|
| 45 | + setError(null); |
35 | 46 |
|
36 | 47 | try {
|
37 |
| - setChatHistory((prevMsg) => [ |
38 |
| - ...prevMsg, |
39 |
| - { role: "user", parts: [{ text: message! }] }, |
40 |
| - ]); |
41 |
| - |
42 |
| - const data = await commitMessage({ |
43 |
| - message: message!, |
44 |
| - history: chatHistory, |
| 48 | + const { data, error } = await commitChange({ |
| 49 | + message: suggestion, |
45 | 50 | });
|
46 | 51 |
|
47 |
| - if (data.success && data.text) { |
48 |
| - setChatHistory((prevMsg) => [ |
49 |
| - ...prevMsg, |
50 |
| - { role: "model", parts: [{ text: data.text }] }, |
51 |
| - ]); |
| 52 | + if (error) { |
| 53 | + setError(error); |
| 54 | + toast({ |
| 55 | + variant: "destructive", |
| 56 | + title: "Uh oh! Something went wrong.", |
| 57 | + description: error, |
| 58 | + action: ( |
| 59 | + <button |
| 60 | + className={cn( |
| 61 | + toastVariants({ |
| 62 | + variant: "destructive", |
| 63 | + className: "w-fit m-0 p-2 text-xs hover:bg-[#815305]/35", |
| 64 | + }) |
| 65 | + )} |
| 66 | + onClick={() => handelSubmit({ suggestion })} |
| 67 | + > |
| 68 | + Try again |
| 69 | + </button> |
| 70 | + ), |
| 71 | + }); |
52 | 72 | } else {
|
53 |
| - console.log(data.error); |
54 |
| - if (data.error) { |
55 |
| - toast({ |
56 |
| - description: data.error, |
57 |
| - }); |
58 |
| - } else { |
| 73 | + if (data) { |
| 74 | + setcommitMessages(data.text); |
| 75 | + setcommitChanges(suggestion); |
| 76 | + setMessage(""); |
59 | 77 | }
|
60 | 78 | }
|
61 | 79 | } catch (error) {
|
62 | 80 | console.log(error);
|
63 | 81 | } finally {
|
64 | 82 | setIsLoading(false);
|
65 |
| - setMessage(""); |
66 | 83 | }
|
67 | 84 | };
|
68 | 85 |
|
| 86 | + const submitForm = ( |
| 87 | + e: React.FormEvent<HTMLFormElement>, |
| 88 | + message: string | null |
| 89 | + ): void => { |
| 90 | + e.preventDefault(); |
| 91 | + handelSubmit({ suggestion: message || "" }); |
| 92 | + }; |
| 93 | + |
69 | 94 | return (
|
70 |
| - <main className="mx-10 lg:mx-20"> |
71 |
| - <section className="h-screen py-10"> |
72 |
| - <Navbar /> |
73 |
| - <div className=" h-full py-10 flex items-center justify-center"> |
74 |
| - <ScrollArea className="h-screen w-full"> |
75 |
| - <div className="relative flex h-screen mx-0 sm:mx-10 lg:mx-60 flex-col p-4 lg:col-span-2"> |
76 |
| - <ScrollArea className="w-full h-5/6 mb-2"> |
77 |
| - <ListChat chatHistory={chatHistory} /> |
78 |
| - {isLoading && <AiLoading />} |
79 |
| - </ScrollArea> |
| 95 | + <div className=" h-full py-10 flex items-center justify-center"> |
| 96 | + <ScrollArea className="h-screen w-full"> |
| 97 | + <div className=" flex h-screen mx-0 sm:mx-10 lg:mx-60 flex-col p-4 lg:col-span-2"> |
| 98 | + <div className="w-full h-5/6 mb-2 flex items-end"> |
| 99 | + <Card className="h-96 w-full flex justify-center items-center bg-primary-foreground/50"> |
| 100 | + {error ? ( |
| 101 | + <div> |
| 102 | + <h3 className="scroll-m-20 text-2xl font-semibold tracking-tight"> |
| 103 | + Oops! Something Went Wrong!{" "} |
| 104 | + <span className="ml-2"> : ( </span> |
| 105 | + </h3> |
| 106 | + <p className="text-sm text-muted-foreground">{error}</p> |
| 107 | + </div> |
| 108 | + ) : isLoading ? ( |
| 109 | + <Loader /> |
| 110 | + ) : commitMessages ? ( |
| 111 | + <ListSuggestion |
| 112 | + suggestions={commitMessages!} |
| 113 | + commitChanges={commitChanges || ""} |
| 114 | + /> |
| 115 | + ) : ( |
| 116 | + <EmptyScreen onSubmit={handelSubmit} /> |
| 117 | + )} |
| 118 | + </Card> |
| 119 | + </div> |
80 | 120 |
|
81 |
| - <div className="flex-1" /> |
82 |
| - <form |
83 |
| - onSubmit={handelSubmit} |
84 |
| - className="relative overflow-hidden rounded-lg border bg-background focus-within:ring-1 focus-within:ring-ring" |
| 121 | + <div className="flex-1" /> |
| 122 | + |
| 123 | + <form |
| 124 | + onSubmit={(e) => submitForm(e, message)} |
| 125 | + className="relative overflow-hidden rounded-lg border bg-primary-foreground/50 focus-within:ring-1 focus-within:ring-ring" |
| 126 | + > |
| 127 | + <Label htmlFor="message" className="sr-only"> |
| 128 | + Message |
| 129 | + </Label> |
| 130 | + <Input |
| 131 | + id="message" |
| 132 | + placeholder="Describe your changes here..." |
| 133 | + className="min-h-12 resize-none border-0 p-3 shadow-none focus-visible:ring-0 bg-primary-foreground/50" |
| 134 | + onChange={(e) => setMessage(e.target.value)} |
| 135 | + value={message || ""} |
| 136 | + disabled={isLoading} |
| 137 | + autoComplete="off" |
| 138 | + autoFocus |
| 139 | + required |
| 140 | + /> |
| 141 | + <div className="flex items-center p-3 pt-0"> |
| 142 | + <Button |
| 143 | + type="submit" |
| 144 | + size="sm" |
| 145 | + className="ml-auto gap-1.5" |
| 146 | + disabled={isLoading} |
85 | 147 | >
|
86 |
| - <Label htmlFor="message" className="sr-only"> |
87 |
| - Message |
88 |
| - </Label> |
89 |
| - <Input |
90 |
| - id="message" |
91 |
| - placeholder="Describe your changes here..." |
92 |
| - className="min-h-12 resize-none border-0 p-3 shadow-none focus-visible:ring-0" |
93 |
| - onChange={(e) => setMessage(e.target.value)} |
94 |
| - value={message!} |
95 |
| - disabled={isLoading} |
96 |
| - autoComplete="off" |
97 |
| - autoFocus |
98 |
| - required |
99 |
| - /> |
100 |
| - <div className="flex items-center p-3 pt-0"> |
101 |
| - <Button |
102 |
| - type="submit" |
103 |
| - size="sm" |
104 |
| - className="ml-auto gap-1.5" |
105 |
| - disabled={isLoading} |
106 |
| - > |
107 |
| - Send Message |
108 |
| - <CornerDownLeft className="size-3.5" /> |
109 |
| - </Button> |
110 |
| - </div> |
111 |
| - </form> |
| 148 | + Send Message |
| 149 | + <CornerDownLeft className="size-3.5" /> |
| 150 | + </Button> |
112 | 151 | </div>
|
113 |
| - </ScrollArea> |
| 152 | + </form> |
114 | 153 | </div>
|
115 |
| - </section> |
116 |
| - </main> |
| 154 | + </ScrollArea> |
| 155 | + </div> |
117 | 156 | );
|
118 | 157 | }
|
0 commit comments