Skip to content

Commit ccb952a

Browse files
committed
feat: implement message editing feature
1 parent 03cd4bc commit ccb952a

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

app/page.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export default function Home() {
115115
<ListSuggestion
116116
suggestions={commitMessages!}
117117
commitChanges={commitChanges || ""}
118+
submitForm={submitForm}
118119
/>
119120
) : (
120121
<div className="w-full flex items-center justify-center">

components/listSuggestion.tsx

+76-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,89 @@
1-
import React from "react";
1+
import React, { useEffect, useState } from "react";
22
import MarkdownReader from "./md-components";
3+
import { CheckIcon, Cross1Icon, Pencil1Icon } from "@radix-ui/react-icons";
4+
import { Button } from "./ui/button";
5+
import { Input } from "./ui/input";
36

47
const ListSuggestion = ({
58
suggestions,
69
commitChanges,
10+
submitForm,
711
}: {
812
suggestions: string;
913
commitChanges: string;
14+
submitForm: (
15+
e: React.FormEvent<HTMLFormElement>,
16+
message: string | null
17+
) => void;
1018
}) => {
19+
const [isEditMode, setIsEditMode] = useState<boolean>(false);
20+
const [updatedMessage, setUpdatedMessage] = useState(commitChanges);
21+
22+
useEffect(() => {
23+
setIsEditMode(false);
24+
}, [commitChanges, suggestions]);
25+
26+
const toggelEditMode = () => {
27+
setIsEditMode((pre) => !pre);
28+
};
29+
1130
return (
12-
<div className="mx-4 md:mx-10">
13-
<h1 className="text-xl font-bold">{commitChanges}</h1>
31+
<div className="mx-4 md:mx-10 pt-1">
32+
{isEditMode ? (
33+
<form
34+
onSubmit={(e) => submitForm(e, updatedMessage)}
35+
className="flex gap-2 w-full items-center justify-between "
36+
>
37+
<Input
38+
className="w-full"
39+
value={updatedMessage}
40+
autoComplete="off"
41+
onChange={(e) => {
42+
setUpdatedMessage(e.target.value);
43+
}}
44+
autoFocus
45+
required
46+
></Input>
47+
<Button
48+
variant={"outline"}
49+
size={"icon"}
50+
type="submit"
51+
className={
52+
"bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground"
53+
}
54+
disabled={
55+
updatedMessage === commitChanges || updatedMessage.trim() === ""
56+
}
57+
>
58+
<CheckIcon />
59+
</Button>
60+
<Button
61+
variant={"outline"}
62+
size={"icon"}
63+
type="button"
64+
className={
65+
"bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground"
66+
}
67+
onClick={toggelEditMode}
68+
>
69+
<Cross1Icon />
70+
</Button>
71+
</form>
72+
) : (
73+
<div className="flex w-full items-center justify-between ">
74+
<h1 className="text-xl font-bold">{commitChanges}</h1>
75+
<Button
76+
variant={"outline"}
77+
size={"icon"}
78+
className={
79+
"bg-primary-foreground text-muted-foreground border hover:bg-primary-foreground"
80+
}
81+
onClick={toggelEditMode}
82+
>
83+
<Pencil1Icon />
84+
</Button>
85+
</div>
86+
)}
1487
<MarkdownReader markdown={suggestions} />
1588
</div>
1689
);

components/ui/button.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const buttonVariants = cva(
2424
default: "h-9 px-4 py-2",
2525
sm: "h-8 rounded-md px-3 text-xs",
2626
lg: "h-10 rounded-md px-8",
27-
icon: "h-9 w-9",
27+
icon: "h-8 w-8",
2828
},
2929
},
3030
defaultVariants: {

0 commit comments

Comments
 (0)