mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
refactor: improve virtualized text viewer with React Query infinite scroll and simplified buffering (#108)
* Add OAuth authentication support for MCP servers * Improve mobile text sizing with responsive text classes * refactor: improve virtualized text viewer with React Query infinite scroll and simplified buffering
This commit is contained in:
1 parent
c453235839
commit
f570758b74
24 files changed
+930
-471
No files matched your search
@@ -173,7 +173,7 @@ export function Combobox({
|
||||
placeholder={placeholder}
|
||||
disabled={disabled}
|
||||
className={cn(
|
||||
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors',
|
||||
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-[16px] md:text-sm shadow-sm transition-colors',
|
||||
'file:border-0 file:bg-transparent file:text-sm file:font-medium',
|
||||
'placeholder:text-muted-foreground',
|
||||
'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring',
|
||||
|
||||
@@ -111,7 +111,7 @@ const VirtualizedLine = memo(function VirtualizedLine({
|
||||
type="text"
|
||||
value={content}
|
||||
onChange={(e) => onLineChange(lineNum, e.target.value)}
|
||||
className={`flex-1 bg-transparent outline-none pl-2 ${
|
||||
className={`flex-1 bg-transparent outline-none pl-2 text-[16px] md:text-sm ${
|
||||
isEdited ? 'bg-yellow-500/10' : ''
|
||||
} ${
|
||||
lineWrap ? 'whitespace-pre-wrap break-all' : 'whitespace-pre'
|
||||
@@ -145,27 +145,19 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
onContentLoaded,
|
||||
}, ref) {
|
||||
const containerRef = useRef<HTMLDivElement>(null)
|
||||
const scrollTopRef = useRef(0)
|
||||
const [renderTrigger, setRenderTrigger] = useState(0)
|
||||
const [scrollTop, setScrollTop] = useState(0)
|
||||
const [viewportHeight, setViewportHeight] = useState(600)
|
||||
const [containerWidth, setContainerWidth] = useState(0)
|
||||
const [highlightedLine, setHighlightedLine] = useState<number | undefined>(initialLineNumber)
|
||||
const loadedBufferRef = useRef<{ start: number; end: number }>({ start: 0, end: 0 })
|
||||
const heightCacheRef = useRef<Map<number, number>>(new Map())
|
||||
const lineOffsetsRef = useRef<{ offsets: Map<number, { height: number; top: number }>; totalHeight: number } | null>(null)
|
||||
const lastContainerWidthRef = useRef(0)
|
||||
const lastTotalLinesRef = useRef(0)
|
||||
const lastVisibleRangeRef = useRef<{ start: number; end: number }>({ start: 0, end: 0 })
|
||||
const currentVisibleRangeRef = useRef<{ start: number; end: number }>({ start: 0, end: 200 })
|
||||
const scrollRafRef = useRef<number | null>(null)
|
||||
const isMobile = useMobile()
|
||||
|
||||
void renderTrigger
|
||||
|
||||
const chunkSize = isMobile && lineWrap ? 800 : 400
|
||||
const chunkSize = isMobile && lineWrap ? 1500 : 1000
|
||||
const overscan = isMobile && lineWrap ? 200 : 100
|
||||
const bufferMultiplier = 4
|
||||
const preRenderMultiplier = 3
|
||||
|
||||
const {
|
||||
lines,
|
||||
@@ -201,7 +193,6 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
}, [isFullyLoaded, fullContent, onContentLoaded])
|
||||
|
||||
useEffect(() => {
|
||||
loadedBufferRef.current = { start: 0, end: 0 }
|
||||
heightCacheRef.current.clear()
|
||||
lineOffsetsRef.current = null
|
||||
}, [filePath])
|
||||
@@ -275,14 +266,15 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
return result
|
||||
}, [lineWrap, containerWidth, totalLines, lines, editedLines, lineHeight])
|
||||
|
||||
const totalHeight = lineOffsets?.totalHeight ?? totalLines * lineHeight
|
||||
const displayLineCount = totalLines > 0 ? totalLines : chunkSize
|
||||
const totalHeight = lineOffsets?.totalHeight ?? displayLineCount * lineHeight
|
||||
|
||||
const lineOffsetsForCalcRef = useRef(lineOffsets)
|
||||
lineOffsetsForCalcRef.current = lineOffsets
|
||||
|
||||
const calculateVisibleRange = useCallback((scrollTop: number) => {
|
||||
const currentLineOffsets = lineOffsetsForCalcRef.current
|
||||
if (currentLineOffsets) {
|
||||
if (currentLineOffsets && totalLines > 0) {
|
||||
const searchTop = scrollTop - overscan * lineHeight
|
||||
const searchBottom = scrollTop + viewportHeight + overscan * lineHeight
|
||||
|
||||
@@ -327,66 +319,26 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
}, [viewportHeight, lineHeight, getVisibleRange, totalLines, overscan])
|
||||
|
||||
const visibleRange = useMemo(() => {
|
||||
const range = calculateVisibleRange(scrollTopRef.current)
|
||||
currentVisibleRangeRef.current = range
|
||||
return range
|
||||
}, [calculateVisibleRange])
|
||||
return calculateVisibleRange(scrollTop)
|
||||
}, [calculateVisibleRange, scrollTop])
|
||||
|
||||
useEffect(() => {
|
||||
const { start, end } = visibleRange
|
||||
const buffer = loadedBufferRef.current
|
||||
const visibleCount = end - start
|
||||
const desiredBuffer = visibleCount * bufferMultiplier
|
||||
|
||||
const needsLoadBefore = start < buffer.start + visibleCount
|
||||
const needsLoadAfter = end > buffer.end - visibleCount
|
||||
|
||||
if (needsLoadBefore || needsLoadAfter || buffer.start === 0 && buffer.end === 0) {
|
||||
const newStart = Math.max(0, start - desiredBuffer)
|
||||
const newEnd = Math.min(totalLines, end + desiredBuffer)
|
||||
|
||||
loadedBufferRef.current = { start: newStart, end: newEnd }
|
||||
loadRange(newStart, newEnd)
|
||||
}
|
||||
}, [visibleRange, loadRange, totalLines, bufferMultiplier])
|
||||
|
||||
const lastRenderTimeRef = useRef(0)
|
||||
const pendingRenderRef = useRef(false)
|
||||
const RENDER_THROTTLE_MS = 32
|
||||
if (start >= end) return
|
||||
loadRange(start, end)
|
||||
}, [visibleRange, loadRange])
|
||||
|
||||
const handleScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
|
||||
const newScrollTop = e.currentTarget.scrollTop
|
||||
scrollTopRef.current = newScrollTop
|
||||
|
||||
if (scrollRafRef.current) {
|
||||
cancelAnimationFrame(scrollRafRef.current)
|
||||
}
|
||||
|
||||
scrollRafRef.current = requestAnimationFrame(() => {
|
||||
const newRange = calculateVisibleRange(newScrollTop)
|
||||
const currentRange = currentVisibleRangeRef.current
|
||||
|
||||
const rangeChanged = newRange.start !== currentRange.start || newRange.end !== currentRange.end
|
||||
if (!rangeChanged) return
|
||||
|
||||
currentVisibleRangeRef.current = newRange
|
||||
|
||||
const now = performance.now()
|
||||
const timeSinceLastRender = now - lastRenderTimeRef.current
|
||||
|
||||
if (timeSinceLastRender >= RENDER_THROTTLE_MS) {
|
||||
lastRenderTimeRef.current = now
|
||||
setRenderTrigger(t => t + 1)
|
||||
} else if (!pendingRenderRef.current) {
|
||||
pendingRenderRef.current = true
|
||||
setTimeout(() => {
|
||||
pendingRenderRef.current = false
|
||||
lastRenderTimeRef.current = performance.now()
|
||||
setRenderTrigger(t => t + 1)
|
||||
}, RENDER_THROTTLE_MS - timeSinceLastRender)
|
||||
}
|
||||
setScrollTop(newScrollTop)
|
||||
})
|
||||
}, [calculateVisibleRange])
|
||||
}, [])
|
||||
|
||||
const handleLineChange = useCallback((lineNum: number, value: string) => {
|
||||
setLineContent(lineNum, value)
|
||||
@@ -396,8 +348,8 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
try {
|
||||
await saveEdits()
|
||||
onSave?.()
|
||||
} catch (err) {
|
||||
console.error('Failed to save:', err)
|
||||
} catch {
|
||||
void 0
|
||||
}
|
||||
}, [saveEdits, onSave])
|
||||
|
||||
@@ -433,25 +385,9 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
|
||||
const visibleLines = useMemo(() => {
|
||||
const result: Array<{ lineNum: number; content: string; isEdited: boolean; height: number; top: number; isLoaded: boolean }> = []
|
||||
const upperBound = totalLines > 0 ? totalLines : visibleRange.end
|
||||
|
||||
const lastRange = lastVisibleRangeRef.current
|
||||
const visibleCount = visibleRange.end - visibleRange.start
|
||||
const maxExpansion = visibleCount * preRenderMultiplier
|
||||
|
||||
const expandedStart = Math.max(
|
||||
0,
|
||||
visibleRange.start - maxExpansion,
|
||||
Math.min(visibleRange.start, lastRange.start)
|
||||
)
|
||||
const expandedEnd = Math.min(
|
||||
totalLines,
|
||||
visibleRange.end + maxExpansion,
|
||||
Math.max(visibleRange.end, lastRange.end)
|
||||
)
|
||||
|
||||
lastVisibleRangeRef.current = visibleRange
|
||||
|
||||
for (let i = expandedStart; i < expandedEnd; i++) {
|
||||
for (let i = visibleRange.start; i < Math.min(visibleRange.end, upperBound); i++) {
|
||||
const editedContent = editedLines.get(i)
|
||||
const lineData = lines.get(i)
|
||||
const isLoaded = lineData?.loaded === true || editedContent !== undefined
|
||||
@@ -472,7 +408,7 @@ export const VirtualizedTextView = forwardRef<VirtualizedTextViewHandle, Virtual
|
||||
}
|
||||
|
||||
return result
|
||||
}, [visibleRange, lines, editedLines, lineOffsets, lineHeight, totalLines, preRenderMultiplier])
|
||||
}, [visibleRange, lines, editedLines, lineOffsets, lineHeight, totalLines])
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user