-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathapp.tsx
More file actions
273 lines (250 loc) · 7.81 KB
/
app.tsx
File metadata and controls
273 lines (250 loc) · 7.81 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { isRetryableStatusCode, getErrorStatusCode } from '@codebuff/sdk'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useShallow } from 'zustand/react/shallow'
import { Chat } from './chat'
import { LoginModal } from './components/login-modal'
import { ProjectPickerScreen } from './components/project-picker-screen'
import { TerminalLink } from './components/terminal-link'
import { useAuthQuery } from './hooks/use-auth-query'
import { useAuthState } from './hooks/use-auth-state'
import { useLogo } from './hooks/use-logo'
import { useSheenAnimation } from './hooks/use-sheen-animation'
import { useTerminalDimensions } from './hooks/use-terminal-dimensions'
import { useTerminalFocus } from './hooks/use-terminal-focus'
import { useTheme } from './hooks/use-theme'
import { getProjectRoot } from './project-files'
import { useChatStore, type TopBannerType } from './state/chat-store'
import { openFileAtPath } from './utils/open-file'
import { formatCwd } from './utils/path-helpers'
import { findGitRoot } from './utils/git'
import { getLogoBlockColor, getLogoAccentColor } from './utils/theme-system'
import type { MultilineInputHandle } from './components/multiline-input'
import type { AgentMode } from './utils/constants'
import type { AuthStatus } from './utils/status-indicator-state'
import type { FileTreeNode } from '@codebuff/common/util/file'
interface AppProps {
initialPrompt: string | null
agentId?: string
requireAuth: boolean | null
hasInvalidCredentials: boolean
fileTree: FileTreeNode[]
continueChat: boolean
continueChatId?: string
initialMode?: AgentMode
showProjectPicker: boolean
onProjectChange: (projectPath: string) => void
}
export const App = ({
initialPrompt,
agentId,
requireAuth,
hasInvalidCredentials,
fileTree,
continueChat,
continueChatId,
initialMode,
showProjectPicker,
onProjectChange,
}: AppProps) => {
const { contentMaxWidth, terminalWidth } = useTerminalDimensions()
const theme = useTheme()
// Sheen animation state for the logo
const [sheenPosition, setSheenPosition] = useState(0)
const blockColor = getLogoBlockColor(theme.name)
const accentColor = getLogoAccentColor(theme.name)
const { applySheenToChar } = useSheenAnimation({
logoColor: theme.foreground,
accentColor,
blockColor,
terminalWidth,
sheenPosition,
setSheenPosition,
})
const { component: logoComponent } = useLogo({
availableWidth: contentMaxWidth,
accentColor,
blockColor,
applySheenToChar,
})
const inputRef = useRef<MultilineInputHandle | null>(null)
const {
setInputFocused,
setIsFocusSupported,
resetChatStore,
activeTopBanner,
setActiveTopBanner,
closeTopBanner,
} = useChatStore(
useShallow((store) => ({
setInputFocused: store.setInputFocused,
setIsFocusSupported: store.setIsFocusSupported,
resetChatStore: store.reset,
activeTopBanner: store.activeTopBanner,
setActiveTopBanner: store.setActiveTopBanner,
closeTopBanner: store.closeTopBanner,
})),
)
// Wrap in useCallback to prevent re-subscribing on every render
const handleSupportDetected = useCallback(() => {
setIsFocusSupported(true)
}, [setIsFocusSupported])
// Enable terminal focus detection to stop cursor blinking when window loses focus
// Cursor starts visible but not blinking; blinking enabled once terminal support confirmed
useTerminalFocus({
onFocusChange: setInputFocused,
onSupportDetected: handleSupportDetected,
})
// Get auth query for network status tracking
const authQuery = useAuthQuery()
const {
isAuthenticated,
setIsAuthenticated,
setUser,
handleLoginSuccess,
logoutMutation,
} = useAuthState({
requireAuth,
inputRef,
setInputFocused,
resetChatStore,
})
const projectRoot = getProjectRoot()
const gitRoot = useMemo(
() => findGitRoot({ cwd: projectRoot }),
[projectRoot],
)
const showGitRootBanner = Boolean(gitRoot && gitRoot !== projectRoot)
const [gitRootBannerDismissed, setGitRootBannerDismissed] = useState(false)
const prevTopBannerRef = useRef<TopBannerType | null>(null)
useEffect(() => {
setGitRootBannerDismissed(false)
}, [projectRoot])
useEffect(() => {
const prevBanner = prevTopBannerRef.current
if (
prevBanner === 'gitRoot' &&
activeTopBanner === null &&
showGitRootBanner
) {
setGitRootBannerDismissed(true)
}
prevTopBannerRef.current = activeTopBanner
}, [activeTopBanner, showGitRootBanner])
useEffect(() => {
if (!showGitRootBanner) {
if (activeTopBanner === 'gitRoot') {
closeTopBanner()
}
return
}
if (!gitRootBannerDismissed && activeTopBanner === null) {
setActiveTopBanner('gitRoot')
}
}, [
activeTopBanner,
closeTopBanner,
gitRootBannerDismissed,
setActiveTopBanner,
showGitRootBanner,
])
const handleSwitchToGitRoot = useCallback(() => {
if (gitRoot) {
onProjectChange(gitRoot)
}
}, [gitRoot, onProjectChange])
const headerContent = useMemo(() => {
const displayPath = formatCwd(projectRoot)
return (
<box
style={{
flexDirection: 'column',
gap: 0,
paddingLeft: 1,
paddingRight: 1,
}}
>
<box
style={{
flexDirection: 'column',
marginBottom: 1,
marginTop: 2,
}}
>
{logoComponent}
</box>
<text
style={{ wrapMode: 'word', marginBottom: 1, fg: theme.foreground }}
>
Codebuff will run commands on your behalf to help you build.
</text>
<text
style={{ wrapMode: 'word', marginBottom: 1, fg: theme.foreground }}
>
Directory{' '}
<TerminalLink
text={displayPath}
color={theme.muted}
inline={true}
underlineOnHover={true}
onActivate={() => openFileAtPath(projectRoot)}
/>
</text>
</box>
)
}, [logoComponent, projectRoot, theme])
// Derive auth reachability + retrying state from authQuery error
const authError = authQuery.error
const authErrorStatusCode = authError ? getErrorStatusCode(authError) : undefined
let authStatus: AuthStatus = 'ok'
if (authQuery.isError && authErrorStatusCode !== undefined) {
if (isRetryableStatusCode(authErrorStatusCode)) {
// Retryable errors (408 timeout, 429 rate limit, 5xx server errors)
authStatus = 'retrying'
} else if (authErrorStatusCode >= 500) {
// Non-retryable server errors (unlikely but possible future codes)
authStatus = 'unreachable'
}
// 4xx client errors (401, 403, etc.) keep 'ok' - network is fine, just auth failed
}
// Render login modal when not authenticated AND auth service is reachable
// Don't show login modal during network outages OR while retrying
if (
requireAuth !== null &&
isAuthenticated === false &&
authStatus === 'ok'
) {
return (
<LoginModal
onLoginSuccess={handleLoginSuccess}
hasInvalidCredentials={hasInvalidCredentials}
/>
)
}
// Render project picker when at home directory or outside a project
if (showProjectPicker) {
return (
<ProjectPickerScreen
onSelectProject={onProjectChange}
initialPath={projectRoot}
/>
)
}
return (
<Chat
headerContent={headerContent}
initialPrompt={initialPrompt}
agentId={agentId}
fileTree={fileTree}
inputRef={inputRef}
setIsAuthenticated={setIsAuthenticated}
setUser={setUser}
logoutMutation={logoutMutation}
continueChat={continueChat}
continueChatId={continueChatId}
authStatus={authStatus}
initialMode={initialMode}
gitRoot={gitRoot}
onSwitchToGitRoot={handleSwitchToGitRoot}
/>
)
}