-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathApp.jsx
More file actions
240 lines (224 loc) · 7.99 KB
/
App.jsx
File metadata and controls
240 lines (224 loc) · 7.99 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import {
createSession,
resetSessions,
getSessions,
updateSession,
getSession,
deleteSession,
} from '../../services/local-session.mjs'
import { useEffect, useRef, useState } from 'react'
import './styles.scss'
import { useConfig } from '../../hooks/use-config.mjs'
import { useTranslation } from 'react-i18next'
import ConfirmButton from '../../components/ConfirmButton'
import ConversationCard from '../../components/ConversationCard'
import DeleteButton from '../../components/DeleteButton'
import { openUrl } from '../../utils/index.mjs'
import Browser from 'webextension-polyfill'
import FileSaver from 'file-saver'
const logo = Browser.runtime.getURL('logo.png')
function App() {
const { t } = useTranslation()
const hasUserToggledRef = useRef(false)
const [collapsed, setCollapsed] = useState(true)
const config = useConfig(null, false)
const [sessions, setSessions] = useState([])
const [sessionId, setSessionId] = useState(null)
const [currentSession, setCurrentSession] = useState(null)
const [renderContent, setRenderContent] = useState(false)
const currentPort = useRef(null)
const setSessionIdSafe = async (sessionId) => {
if (currentPort.current) {
try {
currentPort.current.postMessage({ stop: true })
currentPort.current.disconnect()
} catch (e) {
/* empty */
}
currentPort.current = null
}
const { session, currentSessions } = await getSession(sessionId)
if (session) setSessionId(sessionId)
else if (currentSessions.length > 0) setSessionId(currentSessions[0].sessionId)
}
useEffect(() => {
document.documentElement.dataset.theme = config.themeMode
}, [config.themeMode])
useEffect(() => {
// eslint-disable-next-line
;(async () => {
const urlFrom = new URLSearchParams(window.location.search).get('from')
const sessions = await getSessions()
if (
urlFrom !== 'store' &&
sessions[0].conversationRecords &&
sessions[0].conversationRecords.length > 0
) {
await createNewChat()
} else {
setSessions(sessions)
await setSessionIdSafe(sessions[0].sessionId)
}
})()
}, [])
useEffect(() => {
// Default open only on the standalone IndependentPanel (tab/window) when wide enough.
// Keep the existing side-panel behavior (closed by default).
void (async () => {
if (hasUserToggledRef.current) return
if (!window.matchMedia('(min-width: 900px)').matches) return
try {
// In a regular tab, this returns the current tab object; in the side panel it returns null.
const tab = await Browser.tabs.getCurrent()
if (!hasUserToggledRef.current && tab) setCollapsed(false)
} catch (e) {
// If we can't tell, keep current (closed) default.
}
})()
}, [])
useEffect(() => {
if ('sessions' in config && config['sessions']) setSessions(config['sessions'])
}, [config])
useEffect(() => {
// eslint-disable-next-line
;(async () => {
if (sessions.length > 0) {
setCurrentSession((await getSession(sessionId)).session)
setRenderContent(false)
setTimeout(() => {
setRenderContent(true)
})
}
})()
}, [sessionId])
const toggleSidebar = () => {
hasUserToggledRef.current = true
setCollapsed(!collapsed)
}
const closeSidebar = () => {
hasUserToggledRef.current = true
setCollapsed(true)
}
const closeSidebarIfOverlay = () => {
if (window.matchMedia('(max-width: 600px)').matches) {
closeSidebar()
}
}
const createNewChat = async () => {
const { session, currentSessions } = await createSession()
setSessions(currentSessions)
await setSessionIdSafe(session.sessionId)
}
const exportConversations = async () => {
const sessions = await getSessions()
const blob = new Blob([JSON.stringify(sessions, null, 2)], { type: 'text/json;charset=utf-8' })
FileSaver.saveAs(blob, 'conversations.json')
}
const clearConversations = async () => {
const sessions = await resetSessions()
setSessions(sessions)
await setSessionIdSafe(sessions[0].sessionId)
}
return (
<div className="IndependentPanel">
<div className={`chat-container ${collapsed ? 'sidebar-collapsed' : 'sidebar-open'}`}>
{!collapsed && (
<div className="chat-sidebar-backdrop" role="presentation" onClick={closeSidebar} />
)}
<div className={`chat-sidebar ${collapsed ? 'collapsed' : ''}`}>
<div className="chat-sidebar-content">
<div className="chat-sidebar-topbar">
<div className="chat-sidebar-brand-group">
<img className="chat-sidebar-logo" src={logo} alt="ChatGPTBox" />
<div className="chat-sidebar-brand">ChatGPTBox</div>
</div>
<button
type="button"
className="gpt-util-icon gpt-menu-toggle chat-sidebar-close"
aria-label="Close sidebar"
onClick={closeSidebar}
>
✕
</button>
</div>
<div className="chat-sidebar-button-group">
<button className="normal-button" onClick={createNewChat}>
{t('New Chat')}
</button>
<button className="normal-button" onClick={exportConversations}>
{t('Export')}
</button>
</div>
<hr />
<div className="chat-list">
{sessions.map(
(
session,
index, // TODO editable session name
) => (
<button
key={index}
className={`normal-button ${sessionId === session.sessionId ? 'active' : ''}`}
style="display: flex; align-items: center; justify-content: space-between;"
onClick={() => {
setSessionIdSafe(session.sessionId)
closeSidebarIfOverlay()
}}
>
{session.sessionName}
<span className="gpt-util-group">
<DeleteButton
size={14}
text={t('Delete Conversation')}
onConfirm={() =>
deleteSession(session.sessionId).then((sessions) => {
setSessions(sessions)
setSessionIdSafe(sessions[0].sessionId)
})
}
/>
</span>
</button>
),
)}
</div>
<hr />
<div className="chat-sidebar-button-group">
<ConfirmButton text={t('Clear conversations')} onConfirm={clearConversations} />
<button
className="normal-button"
onClick={() => {
openUrl(Browser.runtime.getURL('popup.html'))
closeSidebarIfOverlay()
}}
>
{t('Settings')}
</button>
</div>
</div>
</div>
<div className="chat-content">
{renderContent && currentSession && currentSession.conversationRecords && (
<div className="chatgptbox-container" style="height:100%;">
<ConversationCard
session={currentSession}
notClampSize={true}
pageMode={true}
sidebarOpen={!collapsed}
onToggleSidebar={toggleSidebar}
onUpdate={(port, session, cData) => {
currentPort.current = port
if (cData.length > 0 && cData[cData.length - 1].done) {
updateSession(session).then(setSessions)
setCurrentSession(session)
}
}}
/>
</div>
)}
</div>
</div>
</div>
)
}
export default App