-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.tsx
142 lines (130 loc) · 4.21 KB
/
home.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
"use client";
import React from "react";
import dynamic from "next/dynamic";
import { commitChange } from "@/action";
import { Card } from "@/components/ui/card";
import { ScrollArea } from "@/components/ui/scroll-area";
import { toastVariants } from "@/components/ui/toast";
import { useToast } from "@/components/ui/use-toast";
import { cn } from "@/lib/utils";
import ListSuggestion from "@/components/listSuggestion";
import FormComponent from "./formComponent";
import ErrorComponent from "./errorComponent";
import LoadingComponent from "./loadingComponent";
import Loader from "./loader";
const EmptyScreen = dynamic(() => import("@/components/emptyScreen"), {
ssr: false,
loading: () => <Loader />,
});
export default function Home() {
const [message, setMessage] = React.useState<string | null>(null);
const [isLoading, setIsLoading] = React.useState(false);
const [error, setError] = React.useState<string | null>(null);
const [commitChanges, setCommitChanges] = React.useState<string | null>(null);
const [isEmojiSupport, setIsEmojiSupport] = React.useState<boolean>(false);
const [commitMessages, setCommitMessages] = React.useState<string | null>(
null
);
const { toast } = useToast();
const handleSubmit = async ({
suggestion,
force = false,
}: {
suggestion: string;
force?: boolean;
}) => {
if (!force && suggestion === commitChanges) {
toast({
variant: "destructive",
title: "Duplicate Message",
description: "Please enter a different message.",
});
return;
}
setIsLoading(true);
setError(null);
try {
const { data, error } = await commitChange({
message: suggestion,
isEmojiSupport: isEmojiSupport,
});
if (error) {
setError(error);
toast({
variant: "destructive",
title: "Uh oh! Something went wrong.",
description: error,
action: (
<button
className={cn(
toastVariants({
variant: "destructive",
className: "w-fit m-0 p-2 text-xs hover:bg-[#815305]/35",
})
)}
onClick={() => handleSubmit({ suggestion })}
>
Try again
</button>
),
});
} else {
if (data) {
setCommitMessages(data.text);
console.log(data.text);
setCommitChanges(suggestion);
setMessage("");
}
}
} catch (error) {
console.log(error);
} finally {
setIsLoading(false);
}
};
const submitForm = (
e: React.FormEvent<HTMLFormElement>,
message: string | null
): void => {
e.preventDefault();
handleSubmit({ suggestion: message || "" });
};
return (
<div className="h-full py-10 flex items-center justify-center">
<ScrollArea className="h-screen w-full">
<div className="flex h-screen mx-0 lg:mx-48 xl:mx-60 flex-col p-4 lg:col-span-2">
<div className="w-full h-5/6 mb-2 flex items-end">
<Card className="h-full w-full py-4 flex justify-center items-center bg-primary-foreground/25">
<ScrollArea className="flex justify-center items-center w-full">
{error ? (
<ErrorComponent error={error} />
) : isLoading ? (
<LoadingComponent />
) : commitMessages ? (
<ListSuggestion
suggestions={commitMessages!}
commitChanges={commitChanges || ""}
submitForm={submitForm}
forceSubmit={handleSubmit}
/>
) : (
<div className="w-full flex items-center justify-center">
<EmptyScreen onSubmit={handleSubmit} />
</div>
)}
</ScrollArea>
</Card>
</div>
<div className="flex-1" />
<FormComponent
isLoading={isLoading}
message={message}
setMessage={setMessage}
submitForm={submitForm}
setIsEmojiSupport={setIsEmojiSupport}
/>
</div>
</ScrollArea>
</div>
);
}