Skip to content

Commit 1878265

Browse files
Refactor action-suggestions.tsx to reduce duplication
1 parent 79bc0e5 commit 1878265

File tree

1 file changed

+48
-31
lines changed

1 file changed

+48
-31
lines changed

Diff for: frontend/src/components/features/chat/action-suggestions.tsx

+48-31
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ interface ActionSuggestionsProps {
99
onSuggestionsClick: (value: string) => void;
1010
}
1111

12+
// Define button configurations to reduce duplication
13+
interface ButtonConfig {
14+
label: string;
15+
value: string;
16+
eventName: string;
17+
callback?: () => void;
18+
}
19+
1220
export function ActionSuggestions({
1321
onSuggestionsClick,
1422
}: ActionSuggestionsProps) {
@@ -19,6 +27,37 @@ export function ActionSuggestions({
1927

2028
const [hasPullRequest, setHasPullRequest] = React.useState(false);
2129

30+
// Define the button configurations
31+
const PUSH_TO_BRANCH: ButtonConfig = {
32+
label: "Push to Branch",
33+
value:
34+
"Please push the changes to a remote branch on GitHub, but do NOT create a pull request. Please use the exact SAME branch name as the one you are currently on.",
35+
eventName: "push_to_branch_button_clicked",
36+
};
37+
38+
const PUSH_AND_CREATE_PR: ButtonConfig = {
39+
label: "Push & Create PR",
40+
value:
41+
"Please push the changes to GitHub and open a pull request. Please create a meaningful branch name that describes the changes.",
42+
eventName: "create_pr_button_clicked",
43+
callback: () => setHasPullRequest(true),
44+
};
45+
46+
const PUSH_TO_PR: ButtonConfig = {
47+
label: "Push changes to PR",
48+
value: "Please push the latest changes to the existing pull request.",
49+
eventName: "push_to_pr_button_clicked",
50+
};
51+
52+
// Helper function to handle button clicks
53+
const handleButtonClick = (config: ButtonConfig) => {
54+
posthog.capture(config.eventName);
55+
onSuggestionsClick(config.value);
56+
if (config.callback) {
57+
config.callback();
58+
}
59+
};
60+
2261
return (
2362
<div className="flex flex-col gap-2 mb-2">
2463
{githubTokenIsSet && selectedRepository && (
@@ -27,48 +66,26 @@ export function ActionSuggestions({
2766
<>
2867
<SuggestionItem
2968
suggestion={{
30-
label: "Push to Branch",
31-
value:
32-
"Please push the changes to a remote branch on GitHub, but do NOT create a pull request. Please use the exact SAME branch name as the one you are currently on.",
33-
}}
34-
onClick={() => {
35-
posthog.capture("push_to_branch_button_clicked");
36-
// Ensure we're sending the correct value for this button
37-
onSuggestionsClick(
38-
"Please push the changes to a remote branch on GitHub, but do NOT create a pull request. Please use the exact SAME branch name as the one you are currently on.",
39-
);
69+
label: PUSH_TO_BRANCH.label,
70+
value: PUSH_TO_BRANCH.value,
4071
}}
72+
onClick={() => handleButtonClick(PUSH_TO_BRANCH)}
4173
/>
4274
<SuggestionItem
4375
suggestion={{
44-
label: "Push & Create PR",
45-
value:
46-
"Please push the changes to GitHub and open a pull request. Please create a meaningful branch name that describes the changes.",
47-
}}
48-
onClick={() => {
49-
posthog.capture("create_pr_button_clicked");
50-
// Ensure we're sending the correct value for this button
51-
onSuggestionsClick(
52-
"Please push the changes to GitHub and open a pull request. Please create a meaningful branch name that describes the changes.",
53-
);
54-
setHasPullRequest(true);
76+
label: PUSH_AND_CREATE_PR.label,
77+
value: PUSH_AND_CREATE_PR.value,
5578
}}
79+
onClick={() => handleButtonClick(PUSH_AND_CREATE_PR)}
5680
/>
5781
</>
5882
) : (
5983
<SuggestionItem
6084
suggestion={{
61-
label: "Push changes to PR",
62-
value:
63-
"Please push the latest changes to the existing pull request.",
64-
}}
65-
onClick={() => {
66-
posthog.capture("push_to_pr_button_clicked");
67-
// Ensure we're sending the correct value for this button
68-
onSuggestionsClick(
69-
"Please push the latest changes to the existing pull request.",
70-
);
85+
label: PUSH_TO_PR.label,
86+
value: PUSH_TO_PR.value,
7187
}}
88+
onClick={() => handleButtonClick(PUSH_TO_PR)}
7289
/>
7390
)}
7491
</div>

0 commit comments

Comments
 (0)