/
posthighlight.tsx
136 lines (119 loc) · 5.3 KB
/
posthighlight.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
import { useEffect, useState } from "react";
import { FaChevronLeft } from "react-icons/fa";
import OpenSaucedLogo from "../assets/opensauced-logo.svg";
import { useAuth } from "../hooks/useAuth";
import toast, { Toaster } from "react-hot-toast";
import { cerateHighlight } from "../utils/fetchOpenSaucedApiData";
import { goBack } from "react-chrome-extension-router";
import { OPEN_SAUCED_INSIGHTS_DOMAIN } from "../constants";
const PostOnHighlight = () => {
const { authToken, user } = useAuth();
const [pageURL, setPageURL] = useState("");
const [highlightTitle, setHighlightTitle] = useState("");
const [highlightContent, setHighlightContent] = useState("");
const [isSendButtonEnabled, enableSendButton] = useState(true);
const generateAiDescription = () => {
const toastId = toast.loading("Generating summary...");
enableSendButton(false);
chrome.tabs.query({ currentWindow: true, active: true }, tabs => {
chrome.tabs.sendMessage(tabs[0].id ?? 0, { type: "get_ai_description" }, response => {
toast.dismiss(toastId);
setHighlightContent(response);
enableSendButton(true);
});
});
};
// post highlight function
const postHighlight = () => {
enableSendButton(false);
const postHighlightAPI = cerateHighlight((authToken ?? ""), pageURL, highlightTitle, highlightContent);
toast.promise(postHighlightAPI, {
loading: "Loading ...",
success: data => {
enableSendButton(true);
if (!data.ok) {
throw new Error(`Statues code ${data.status}`);
}
return (
<span>
<a
href={`https://${OPEN_SAUCED_INSIGHTS_DOMAIN}/user/${user?.user_name}/highlights`}
rel="noreferrer"
target="_blank"
>
See the highlight live
</a>
</span>
);
},
error: e => {
enableSendButton(true);
return `Uh oh, there was an error! ${e.message}`;
},
}).catch(console.error);
};
useEffect(() => {
chrome.tabs.query({ currentWindow: true, active: true }, tabs => {
setPageURL(tabs[0]?.url ?? "");
chrome.tabs.sendMessage(tabs[0].id ?? 0, { type: "get_highlight" }, setHighlightTitle);
});
}, []);
return (
<div className="p-4 bg-slate-800">
<Toaster />
<div className="grid grid-cols-1 divide-y divider-y-center-2 min-w-[320px]">
<header className="flex justify-between">
<div className="flex items-center gap-2">
<button
className="rounded-full p-2 bg-slate-700 hover:bg-slate-700/50"
onClick={() => {
goBack();
}}
>
<FaChevronLeft className="text-osOrange text-white" />
</button>
<img
alt="OpenSauced logo"
className="w-[100%]"
src={OpenSaucedLogo}
/>
</div>
</header>
<main className="text-white">
<input
className="p-1.5 rounded-md mb-2 w-full text-black"
maxLength={50}
placeholder="An amazing title here"
type="text"
value={highlightTitle}
onChange={e => setHighlightTitle(e.target.value)}
/>
<textarea
className="p-1.5 rounded-md mb-2 w-full text-black"
placeholder="Summarize this pull request"
rows={5}
value={highlightContent}
onChange={e => setHighlightContent(e.target.value)}
/>
<div className="flex justify-evenly">
<button
className="inline-block disabled:bg-gray-500 text-black bg-gh-white rounded-md p-2 text-sm font-semibold text-center select-none w-5/12 border hover:shadow-button hover:no-underline"
disabled={!isSendButtonEnabled}
onClick={() => generateAiDescription()}
>
Summarize
</button>
<button
className="inline-block disabled:bg-gray-500 text-black bg-gh-white rounded-md p-2 text-sm font-semibold text-center select-none w-5/12 border hover:shadow-button hover:no-underline"
disabled={!isSendButtonEnabled}
onClick={postHighlight}
>
Post
</button>
</div>
</main>
</div>
</div>
);
};
export default PostOnHighlight;