Skip to content

Commit f1d837d

Browse files
committed
refactor(files): four stellar-quality improvements to file-viewer split
- Extract useBlobUrl hook shared by AudioPreview and VideoPreview, eliminating ~30 lines of duplicated state/effect logic - Stabilize markSavedContent with useCallback (matches setDraftContent) - Stabilize handleEditorChange with useCallback([setDraftContent]) - Fix pptx static render effect deps: drop redundant dataUpdatedAt (already encoded in cacheKey) and unused workspaceId
1 parent deec713 commit f1d837d

3 files changed

Lines changed: 34 additions & 43 deletions

File tree

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -351,28 +351,16 @@ const IframePreview = memo(function IframePreview({
351351
return <PdfViewerCore source={staticSource} filename={file.name} />
352352
})
353353

354-
const AudioPreview = memo(function AudioPreview({
355-
file,
356-
workspaceId,
357-
}: {
358-
file: WorkspaceFileRecord
359-
workspaceId: string
360-
}) {
361-
const {
362-
data: fileData,
363-
isLoading,
364-
error: fetchError,
365-
} = useWorkspaceFileBinary(workspaceId, file.id, file.key)
354+
function useBlobUrl(workspaceId: string, fileId: string, fileKey: string) {
355+
const { data: fileData, isLoading, error } = useWorkspaceFileBinary(workspaceId, fileId, fileKey)
366356
const [blobUrl, setBlobUrl] = useState<string | null>(null)
367357
const blobUrlRef = useRef<string | null>(null)
368358

369359
const replaceBlobUrl = useCallback((nextUrl: string | null) => {
370360
const previousUrl = blobUrlRef.current
371361
blobUrlRef.current = nextUrl
372362
setBlobUrl(nextUrl)
373-
if (previousUrl && previousUrl !== nextUrl) {
374-
URL.revokeObjectURL(previousUrl)
375-
}
363+
if (previousUrl && previousUrl !== nextUrl) URL.revokeObjectURL(previousUrl)
376364
}, [])
377365

378366
useEffect(() => {
@@ -384,6 +372,24 @@ const AudioPreview = memo(function AudioPreview({
384372
}
385373
}, [])
386374

375+
return { fileData, isLoading, error, blobUrl, replaceBlobUrl }
376+
}
377+
378+
const AudioPreview = memo(function AudioPreview({
379+
file,
380+
workspaceId,
381+
}: {
382+
file: WorkspaceFileRecord
383+
workspaceId: string
384+
}) {
385+
const {
386+
fileData,
387+
isLoading,
388+
error: fetchError,
389+
blobUrl,
390+
replaceBlobUrl,
391+
} = useBlobUrl(workspaceId, file.id, file.key)
392+
387393
useEffect(() => {
388394
if (!fileData) return
389395
replaceBlobUrl(URL.createObjectURL(new Blob([fileData], { type: file.type || 'audio/mpeg' })))
@@ -424,30 +430,12 @@ const VideoPreview = memo(function VideoPreview({
424430
workspaceId: string
425431
}) {
426432
const {
427-
data: fileData,
433+
fileData,
428434
isLoading,
429435
error: fetchError,
430-
} = useWorkspaceFileBinary(workspaceId, file.id, file.key)
431-
const [blobUrl, setBlobUrl] = useState<string | null>(null)
432-
const blobUrlRef = useRef<string | null>(null)
433-
434-
const replaceBlobUrl = useCallback((nextUrl: string | null) => {
435-
const previousUrl = blobUrlRef.current
436-
blobUrlRef.current = nextUrl
437-
setBlobUrl(nextUrl)
438-
if (previousUrl && previousUrl !== nextUrl) {
439-
URL.revokeObjectURL(previousUrl)
440-
}
441-
}, [])
442-
443-
useEffect(() => {
444-
return () => {
445-
if (blobUrlRef.current) {
446-
URL.revokeObjectURL(blobUrlRef.current)
447-
blobUrlRef.current = null
448-
}
449-
}
450-
}, [])
436+
blobUrl,
437+
replaceBlobUrl,
438+
} = useBlobUrl(workspaceId, file.id, file.key)
451439

452440
useEffect(() => {
453441
if (!fileData) return

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/pptx-preview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export function PptxPreview({
262262
return () => {
263263
cancelled = true
264264
}
265-
}, [fileData, dataUpdatedAt, streamingContent, cacheKey, workspaceId])
265+
}, [fileData, streamingContent, cacheKey])
266266

267267
const error = resolvePreviewError(fetchError, renderError)
268268
const loading = isFetching || rendering

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ function useTextEditorContentState(options: SyncTextEditorContentStateOptions) {
498498
dispatch({ type: 'edit', content })
499499
}, [])
500500

501-
const markSavedContent = (content: string) => {
501+
const markSavedContent = useCallback((content: string) => {
502502
dispatch({ type: 'save-success', content })
503-
}
503+
}, [])
504504

505505
return {
506506
content: state.content,
@@ -786,9 +786,12 @@ export const TextEditor = memo(function TextEditor({
786786
}
787787
}
788788

789-
const handleEditorChange = (value: string | undefined) => {
790-
setDraftContent(value ?? '')
791-
}
789+
const handleEditorChange = useCallback(
790+
(value: string | undefined) => {
791+
setDraftContent(value ?? '')
792+
},
793+
[setDraftContent]
794+
)
792795

793796
const isStreaming = isStreamInteractionLocked
794797
const isEditorReadOnly = isStreamInteractionLocked || !canEdit

0 commit comments

Comments
 (0)