-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathApp.tsx
executable file
·190 lines (180 loc) · 5.32 KB
/
App.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
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
import React, { useEffect, useRef, useState, useLayoutEffect } from 'react'
import { Image, message } from 'antd'
import common from './electron/constants/common'
import PubSub from 'pubsub-js'
import { MoreOutlined } from '@ant-design/icons'
import { GlobalStyle } from './styles/GlobalStyle'
import { ChatPanel } from './components/ChatPanel'
import { Setting } from './components/Setting'
import { Preset } from './components/Preset'
import { Logo } from './components/Logo'
import Search from './components/Search'
import { Prompt as PromptModal } from './components/Modal/prompt'
import { init as initI18n } from './i18n'
import { useAppDispatch, useAppSelector } from './app/hooks'
import { StoreKey } from './app/constants'
import { draggableStyle } from './utils'
import { setVisible as setChatVisible } from './features/chat/chatSlice'
import {
setListVisible as setPresetListVisible,
setPreset,
} from './features/preset/presetSlice'
import {
setVisible as setSettingVisible,
setMinimal,
setLng,
setContexual,
setStore as setStoreSet,
defaultVals,
} from './features/setting/settingSlice'
import { setUrl, setSelection } from './features/clipboard/clipboardSlice'
import {
selection_change,
url_change,
setting_show,
} from './electron/constants/event'
import { PresetType, PanelVisible } from './@types'
interface Tips {
type: 'success' | 'error' | 'warning'
message: string
}
export function App() {
const [show, setShow] = useState<boolean>(false)
const presetState = useAppSelector(state => state.preset)
const [messageApi, contextHolder] = message.useMessage()
const dispatch = useAppDispatch()
useRef<HTMLTextAreaElement>(null)
const preset = presetState.builtInPlugins.filter(
p => p.title === presetState.currentPreset
)
const presetIcon = preset.length > 0 ? preset[0].logo : null
// TODO: need to perf
const getSettings = async () => {
const getter = window.Main.getSettings
const lng = await getter(StoreKey.Set_Lng)
dispatch(setLng(lng || defaultVals.lng))
initI18n(lng)
setShow(true)
const storeSet = await getter(StoreKey.Set_StoreChat)
dispatch(setStoreSet(storeSet || defaultVals.store))
const contextual = await getter(StoreKey.Set_Contexual)
dispatch(setContexual(contextual || defaultVals.contexual))
const simpleMode = await getter(StoreKey.Set_SimpleMode)
dispatch(setMinimal(simpleMode || false))
}
useLayoutEffect(() => {
getSettings()
}, [])
useEffect(() => {
if (common.production()) {
window.addEventListener('mousemove', event => {
const flag = event.target === document.documentElement
window.Main.ignoreWinMouse(flag)
})
}
window.Main.on(
selection_change,
(selection: { txt: string; app: string }) => {
const { txt, app } = selection
dispatch(setSelection({ txt, app }))
dispatch(setChatVisible(!!txt && !!app))
}
)
window.Main.on(url_change, (selection: { url: string }) => {
const { url } = selection
dispatch(setUrl({ url }))
dispatch(setChatVisible(true))
})
window.Main.on(setting_show, () =>
showPanel({
setting: true,
})
)
PubSub.subscribe('tips', (name: string, data: Tips) => {
const { type, message } = data
messageApi.open({
type,
content: message,
})
})
PubSub.subscribe('showPanel', (name: string, data: PanelVisible) => {
showPanel(data)
})
}, [])
const showPanel = (options: PanelVisible) => {
const { plugin, setting, chatPanel } = options
dispatch(setPresetListVisible(!!plugin && !setting && !chatPanel))
dispatch(setSettingVisible(!plugin && !!setting && !chatPanel))
dispatch(setChatVisible(!plugin && !setting && !!chatPanel))
}
const onPresetChange = (preset: PresetType) => {
dispatch(setPreset(preset))
window.Main.setUsePreset(preset)
}
return show ? (
<>
<GlobalStyle />
{contextHolder}
<div style={styles.container}>
<div style={styles.inputWrap}>
{presetIcon ? (
<Image
width={30}
style={styles.nonDragable}
preview={false}
src={presetIcon}
onClick={() =>
showPanel({
plugin: true,
})
}
/>
) : null}
<Search />
<MoreOutlined
style={styles.moreIcon}
onClick={() => {
PubSub.publish('showPromptModal')
}}
/>
<Logo />
</div>
<ChatPanel />
<Preset onPresetChange={onPresetChange} />
<Setting />
<PromptModal />
</div>
</>
) : null
}
const padding = 15
const styles = {
container: {
backgroundColor: '#FFF',
border: 'none',
borderRadius: 15,
borderWidth: 1,
borderColor: '#FFF',
overflow: 'hidden',
},
inputWrap: {
position: 'relative',
display: 'flex',
flexDirection: 'row',
border: 'none',
borderWidth: 0,
borderColor: '#FFF',
justifyContent: 'space-between',
alignItems: 'center',
padding,
...draggableStyle(true),
} as React.CSSProperties,
nonDragable: {
...draggableStyle(false),
} as React.CSSProperties,
moreIcon: {
fontSize: 20,
margin: '0 10px',
...draggableStyle(false),
},
}