add typecheck script and fix TypeScript errors (#48)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-01-04 17:11:53 -05:00
1 parent e853660ad3
commit ed4bc17987
14 files changed
+371 -137

No files matched your search

@@ -1,6 +1,7 @@
import { useFileDiff } from '@/api/git'
import { Loader2, FileText, FilePlus, FileX, FileEdit, File, Plus, Minus, ArrowLeft, ExternalLink } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { CopyButton } from '@/components/ui/copy-button'
import { cn } from '@/lib/utils'
import type { GitFileStatusType } from '@/types/git'
@@ -60,6 +61,8 @@ const statusConfig: Record<GitFileStatusType, { icon: typeof FileText; color: st
copied: { icon: FileText, color: 'text-purple-500', bgColor: 'bg-purple-500/10', label: 'Copied' },
}
function DiffLineComponent({ line, showLineNumbers, onLineClick }: { line: DiffLine; showLineNumbers: boolean; onLineClick?: (lineNumber: number) => void }) {
if (line.type === 'header') {
return (
@@ -198,6 +201,7 @@ export function FileDiffView({ repoId, filePath, onBack, onOpenFile, isMobile =
<span className="text-red-500">-{diffData.deletions}</span>
</>
)}
{diffData.diff && <CopyButton content={diffData.diff} title="Copy diff" iconSize="sm" variant="ghost" />}
</div>
</div>
@@ -1,6 +1,6 @@
import { useState } from 'react'
import { useState, useMemo } from 'react'
import { useGitStatus } from '@/api/git'
import { Loader2, FileText, FilePlus, FileX, FileEdit, File, ChevronRight, ArrowUp, ArrowDown } from 'lucide-react'
import { Loader2, FileText, FilePlus, FileX, FileEdit, File, ChevronRight, ChevronDown, ArrowUp, ArrowDown, Folder, FolderOpen } from 'lucide-react'
import { cn } from '@/lib/utils'
import type { GitFileStatus, GitFileStatusType } from '@/types/git'
@@ -10,6 +10,16 @@ interface GitChangesPanelProps {
selectedFile?: string
}
interface GitTreeNode {
name: string
path: string
isDirectory: boolean
children: GitTreeNode[]
file?: GitFileStatus
fileCount: number
statusCounts: Partial<Record<GitFileStatusType, number>>
}
const statusConfig: Record<GitFileStatusType, { icon: typeof FileText; color: string; label: string }> = {
modified: { icon: FileEdit, color: 'text-yellow-500', label: 'Modified' },
added: { icon: FilePlus, color: 'text-green-500', label: 'Added' },
@@ -19,31 +29,201 @@ const statusConfig: Record<GitFileStatusType, { icon: typeof FileText; color: st
copied: { icon: FileText, color: 'text-purple-500', label: 'Copied' },
}
function FileStatusItem({ file, isSelected, onClick }: { file: GitFileStatus; isSelected: boolean; onClick: () => void }) {
function buildTree(files: GitFileStatus[]): GitTreeNode[] {
const root: GitTreeNode[] = []
const nodeMap = new Map<string, GitTreeNode>()
for (const file of files) {
const isDirectoryPath = file.path.endsWith('/')
const cleanPath = isDirectoryPath ? file.path.slice(0, -1) : file.path
const parts = cleanPath.split('/').filter(p => p)
if (parts.length === 0) continue
let currentPath = ''
let currentLevel = root
for (let i = 0; i < parts.length; i++) {
const part = parts[i]
const isLast = i === parts.length - 1
currentPath = currentPath ? `${currentPath}/${part}` : part
let node = nodeMap.get(currentPath)
if (!node) {
const isDir = !isLast || isDirectoryPath
node = {
name: part,
path: currentPath,
isDirectory: isDir,
children: [],
file: (isLast && !isDirectoryPath) ? file : undefined,
fileCount: 0,
statusCounts: isDirectoryPath && isLast ? { [file.status]: 1 } : {},
}
nodeMap.set(currentPath, node)
currentLevel.push(node)
} else if (isLast && isDirectoryPath) {
node.statusCounts[file.status] = (node.statusCounts[file.status] || 0) + 1
}
if (isLast && !isDirectoryPath) {
node.file = file
node.isDirectory = false
}
currentLevel = node.children
}
}
function aggregateCounts(node: GitTreeNode): void {
if (node.isDirectory) {
const existingCounts = { ...node.statusCounts }
node.fileCount = 0
node.statusCounts = {}
if (node.children.length === 0) {
node.statusCounts = existingCounts
node.fileCount = Object.values(existingCounts).reduce((a, b) => a + (b || 0), 0) || 1
} else {
for (const child of node.children) {
aggregateCounts(child)
node.fileCount += child.isDirectory ? child.fileCount : 1
for (const [status, count] of Object.entries(child.statusCounts)) {
node.statusCounts[status as GitFileStatusType] =
(node.statusCounts[status as GitFileStatusType] || 0) + (count as number)
}
if (child.file) {
node.statusCounts[child.file.status] =
(node.statusCounts[child.file.status] || 0) + 1
}
}
}
}
}
function sortNodes(nodes: GitTreeNode[]): GitTreeNode[] {
return nodes.sort((a, b) => {
if (a.isDirectory && !b.isDirectory) return -1
if (!a.isDirectory && b.isDirectory) return 1
return a.name.localeCompare(b.name)
}).map(node => ({
...node,
children: sortNodes(node.children)
}))
}
for (const node of root) {
aggregateCounts(node)
}
return sortNodes(root)
}
function filterTree(nodes: GitTreeNode[], filter: GitFileStatusType | 'all'): GitTreeNode[] {
if (filter === 'all') return nodes
return nodes
.map(node => {
if (node.isDirectory) {
const filteredChildren = filterTree(node.children, filter)
if (filteredChildren.length === 0) return null
return { ...node, children: filteredChildren }
}
return node.file?.status === filter ? node : null
})
.filter((node): node is GitTreeNode => node !== null)
}
interface GitTreeNodeItemProps {
node: GitTreeNode
level: number
selectedFile?: string
expandedPaths: Set<string>
onToggle: (path: string) => void
onSelect: (path: string) => void
}
function GitTreeNodeItem({ node, level, selectedFile, expandedPaths, onToggle, onSelect }: GitTreeNodeItemProps) {
const isExpanded = expandedPaths.has(node.path)
const isSelected = selectedFile === node.path
if (node.isDirectory) {
const statusEntries = Object.entries(node.statusCounts) as [GitFileStatusType, number][]
const dominantStatus = statusEntries.length > 0
? statusEntries.reduce((a, b) => a[1] > b[1] ? a : b)[0]
: null
return (
<div>
<button
onClick={() => onToggle(node.path)}
className={cn(
'w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-accent/50 transition-colors rounded-md'
)}
style={{ paddingLeft: `${level * 12 + 12}px` }}
>
<span className="w-4 h-4 flex items-center justify-center flex-shrink-0">
{isExpanded ? (
<ChevronDown className="w-3 h-3 text-muted-foreground" />
) : (
<ChevronRight className="w-3 h-3 text-muted-foreground" />
)}
</span>
{isExpanded ? (
<FolderOpen className="w-4 h-4 text-muted-foreground flex-shrink-0" />
) : (
<Folder className="w-4 h-4 text-muted-foreground flex-shrink-0" />
)}
<span className="text-sm text-foreground truncate flex-1">{node.name}</span>
<div className="flex items-center gap-1 flex-shrink-0">
{dominantStatus && (
<span className={cn('text-[10px] px-1 py-0.5 rounded', statusConfig[dominantStatus].color)}>
{node.fileCount}
</span>
)}
</div>
</button>
{isExpanded && (
<div>
{node.children.map(child => (
<GitTreeNodeItem
key={child.path}
node={child}
level={level + 1}
selectedFile={selectedFile}
expandedPaths={expandedPaths}
onToggle={onToggle}
onSelect={onSelect}
/>
))}
</div>
)}
</div>
)
}
const file = node.file!
const config = statusConfig[file.status]
const Icon = config.icon
const fileName = file.path.split('/').pop() || file.path
const dirPath = file.path.includes('/') ? file.path.substring(0, file.path.lastIndexOf('/')) : ''
return (
<button
onClick={onClick}
onClick={() => onSelect(node.path)}
className={cn(
'w-full flex items-center gap-2 px-3 py-2 text-left hover:bg-accent/50 transition-colors rounded-md',
isSelected && 'bg-accent'
)}
style={{ paddingLeft: `${level * 12 + 12}px` }}
>
<span className="w-4 h-4 flex-shrink-0" />
<Icon className={cn('w-4 h-4 flex-shrink-0', config.color)} />
<div className="flex-1 min-w-0">
<div className="flex items-center gap-1">
<span className="text-sm text-foreground truncate">{fileName}</span>
<span className="text-sm text-foreground truncate">{node.name}</span>
{file.staged && (
<span className="text-[10px] px-1 py-0.5 bg-green-500/20 text-green-500 rounded">staged</span>
)}
</div>
{dirPath && (
<span className="text-xs text-muted-foreground truncate block">{dirPath}</span>
)}
{file.oldPath && (
<span className="text-xs text-muted-foreground truncate block">← {file.oldPath}</span>
)}
@@ -56,6 +236,28 @@ function FileStatusItem({ file, isSelected, onClick }: { file: GitFileStatus; is
export function GitChangesPanel({ repoId, onFileSelect, selectedFile }: GitChangesPanelProps) {
const { data: status, isLoading, error } = useGitStatus(repoId)
const [filter, setFilter] = useState<GitFileStatusType | 'all'>('all')
const [expandedPaths, setExpandedPaths] = useState<Set<string>>(new Set())
const tree = useMemo(() => {
if (!status?.files) return []
return buildTree(status.files)
}, [status?.files])
const filteredTree = useMemo(() => {
return filterTree(tree, filter)
}, [tree, filter])
const handleToggle = (path: string) => {
setExpandedPaths(prev => {
const next = new Set(prev)
if (next.has(path)) {
next.delete(path)
} else {
next.add(path)
}
return next
})
}
if (isLoading) {
return (
@@ -83,10 +285,6 @@ export function GitChangesPanel({ repoId, onFileSelect, selectedFile }: GitChang
)
}
const filteredFiles = filter === 'all'
? status.files
: status.files.filter(f => f.status === filter)
const statusCounts = status.files.reduce((acc, file) => {
acc[file.status] = (acc[file.status] || 0) + 1
return acc
@@ -140,13 +338,16 @@ export function GitChangesPanel({ repoId, onFileSelect, selectedFile }: GitChang
</div>
</div>
<div className="flex-1 overflow-y-auto p-2">
<div className="space-y-1 pb-8">
{filteredFiles.map((file) => (
<FileStatusItem
key={`${file.path}-${file.staged}`}
file={file}
isSelected={selectedFile === file.path}
onClick={() => onFileSelect(file.path)}
<div className="space-y-0.5 pb-8">
{filteredTree.map((node) => (
<GitTreeNodeItem
key={node.path}
node={node}
level={0}
selectedFile={selectedFile}
expandedPaths={expandedPaths}
onToggle={handleToggle}
onSelect={onFileSelect}
/>
))}
</div>
@@ -1,5 +1,5 @@
import React from 'react'
import { Copy } from 'lucide-react'
import { CopyButton } from '@/components/ui/copy-button'
import { useMobile } from '@/hooks/useMobile'
import { cn } from '@/lib/utils'
@@ -59,7 +59,6 @@ function detectLanguage(fileName: string): string {
export function CodePreview({ fileName, content }: CodePreviewProps) {
const isMobile = useMobile()
const [copied, setCopied] = React.useState(false)
const [showMore, setShowMore] = React.useState(false)
const language = detectLanguage(fileName)
@@ -73,27 +72,11 @@ export function CodePreview({ fileName, content }: CodePreviewProps) {
? lines.slice(0, INITIAL_CODE_LINES).join('\n')
: content
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(content)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch (err) {
console.error('Failed to copy:', err)
}
}
return (
<div className="flex flex-col bg-background">
<div className="px-3 py-2 bg-muted/30 border-b border-border/50 flex items-center justify-between text-xs">
<span className="font-medium truncate flex-1">{extractFileFromPath(fileName)}</span>
<button
onClick={handleCopy}
className="p-1 hover:bg-accent rounded flex-shrink-0"
title={copied ? 'Copied!' : 'Copy'}
>
<Copy className={cn('w-3.5 h-3.5', copied && 'text-green-500')} />
</button>
<CopyButton content={content} title="Copy" iconSize="sm" variant="ghost" className="flex-shrink-0" />
</div>
<div className={cn('overflow-y-auto', isMobile ? 'max-h-64' : 'max-h-96')}>
@@ -145,7 +145,8 @@ export const MessagePart = memo(function MessagePart({ part, role, allParts, par
)
case 'step-finish': {
const isFree = part.cost === 0
const costText = isMobile && isFree ? null : <span>${part.cost.toFixed(4)} • {part.tokens.input + part.tokens.output} tokens</span>
const totalTokens = part.tokens.input + part.tokens.output + (part.tokens.cache?.read || 0)
const costText = isMobile && isFree ? null : <span>${part.cost.toFixed(4)} • {totalTokens} tokens</span>
return (
<div className="text-xs text-muted-foreground my-1 flex items-center gap-2">
{costText}
+5 -45
View File
@@ -4,7 +4,8 @@ import remarkGfm from 'remark-gfm'
import rehypeHighlight from 'rehype-highlight'
import rehypeRaw from 'rehype-raw'
import mermaid from 'mermaid'
import { Copy, Check, Maximize2, X, AlertCircle } from 'lucide-react'
import { Maximize2, X, AlertCircle } from 'lucide-react'
import { CopyButton } from '@/components/ui/copy-button'
import type { components } from '@/api/opencode-types'
import { useTheme } from '@/hooks/useTheme'
import 'highlight.js/styles/github-dark.css'
@@ -23,7 +24,6 @@ function MermaidBlock({ code }: MermaidBlockProps) {
const [svg, setSvg] = useState<string | null>(null)
const [error, setError] = useState<string | null>(null)
const [isExpanded, setIsExpanded] = useState(false)
const [copied, setCopied] = useState(false)
const theme = useTheme()
const uniqueId = useId().replace(/:/g, '-')
const renderAttempt = React.useRef(0)
@@ -71,16 +71,6 @@ function MermaidBlock({ code }: MermaidBlockProps) {
renderDiagram()
}, [renderDiagram, theme])
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(code)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch (err) {
console.error('Failed to copy:', err)
}
}
if (error) {
return (
<div className="relative my-4">
@@ -94,13 +84,7 @@ function MermaidBlock({ code }: MermaidBlockProps) {
<code>{code}</code>
</pre>
</div>
<button
onClick={handleCopy}
className="absolute top-2 right-2 p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground"
title="Copy code"
>
{copied ? <Check className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
</button>
<CopyButton content={code} title="Copy code" className="absolute top-2 right-2" />
</div>
)
}
@@ -128,13 +112,7 @@ function MermaidBlock({ code }: MermaidBlockProps) {
>
<Maximize2 className="w-4 h-4" />
</button>
<button
onClick={handleCopy}
className="p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground"
title="Copy code"
>
{copied ? <Check className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
</button>
<CopyButton content={code} title="Copy code" />
</div>
</div>
@@ -172,8 +150,6 @@ interface CodeBlockProps {
}
function CodeBlock({ children, className, ...props }: CodeBlockProps) {
const [copied, setCopied] = React.useState(false)
const extractTextContent = (node: React.ReactNode): string => {
if (typeof node === 'string') return node
if (typeof node === 'number') return node.toString()
@@ -188,29 +164,13 @@ function CodeBlock({ children, className, ...props }: CodeBlockProps) {
}
const codeContent = extractTextContent(children)
const handleCopyCode = async () => {
try {
await navigator.clipboard.writeText(codeContent)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch (error) {
console.error('Failed to copy code:', error)
}
}
return (
<div className="relative">
<pre className={`bg-accent p-1 rounded-lg overflow-x-auto whitespace-pre-wrap break-words border border-border my-4 ${className || ''}`} {...props}>
{children}
</pre>
<button
onClick={handleCopyCode}
className="absolute top-2 right-2 p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground"
title="Copy code"
>
{copied ? <Check className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
</button>
<CopyButton content={codeContent} title="Copy code" className="absolute top-2 right-2" />
</div>
)
}
@@ -247,11 +247,12 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
</span>
)}
</div>
<pre className="bg-accent p-3 rounded text-xs overflow-x-auto whitespace-pre-wrap cursor-pointer hover:bg-accent/80 transition-colors"
onClick={() => navigator.clipboard.writeText(output)}
title="Click to copy output">
{output}
</pre>
<div className="relative">
<pre className="bg-accent p-3 rounded text-xs overflow-x-auto whitespace-pre-wrap">
{output}
</pre>
<CopyButton content={output} title="Copy output" className="absolute top-2 right-2" />
</div>
</div>
)
}
@@ -387,11 +388,14 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
)}
<div className="text-sm">
<div className="text-muted-foreground mb-1">Output:</div>
<pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-all cursor-pointer hover:bg-accent/80 transition-colors"
onClick={() => part.state.status === 'completed' && navigator.clipboard.writeText(part.state.output)}
title="Click to copy output">
{part.state.status === 'completed' ? part.state.output : ''}
</pre>
<div className="relative">
<pre className="bg-accent p-2 rounded text-xs overflow-x-auto whitespace-pre-wrap break-all">
{part.state.status === 'completed' ? part.state.output : ''}
</pre>
{part.state.status === 'completed' && part.state.output && (
<CopyButton content={part.state.output} title="Copy output" className="absolute top-1 right-1" iconSize="sm" />
)}
</div>
</div>
{part.state.time && (
<div className="text-xs text-muted-foreground">
+32 -8
View File
@@ -1,17 +1,32 @@
import { Copy } from 'lucide-react'
import { useState } from 'react'
import { Copy, Check } from 'lucide-react'
import { cn } from '@/lib/utils'
interface CopyButtonProps {
content: string
title: string
title?: string
className?: string
iconSize?: 'sm' | 'md'
variant?: 'default' | 'ghost'
}
export function CopyButton({ content, title, className = "" }: CopyButtonProps) {
export function CopyButton({
content,
title = 'Copy',
className = '',
iconSize = 'md',
variant = 'default'
}: CopyButtonProps) {
const [copied, setCopied] = useState(false)
const handleCopy = async () => {
if (!content.trim()) return
try {
await navigator.clipboard.writeText(content)
} catch (error) {
console.error('Failed to copy content:', error)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch {
// Clipboard access denied or not supported
}
}
@@ -19,13 +34,22 @@ export function CopyButton({ content, title, className = "" }: CopyButtonProps)
return null
}
const sizeClasses = iconSize === 'sm' ? 'w-3.5 h-3.5' : 'w-4 h-4'
const variantClasses = variant === 'ghost'
? 'p-1 hover:bg-accent rounded'
: 'p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground'
return (
<button
onClick={handleCopy}
className={`p-1.5 rounded bg-card hover:bg-card-hover text-muted-foreground hover:text-foreground ${className}`}
title={title}
className={cn(variantClasses, className)}
title={copied ? 'Copied!' : title}
>
<Copy className="w-4 h-4" />
{copied ? (
<Check className={cn(sizeClasses, 'text-green-500')} />
) : (
<Copy className={cn(sizeClasses, copied && 'text-green-500')} />
)}
</button>
)
}