Skip to content

Commit 9f14c81

Browse files
committed
fix(file): use file-upload subblock
1 parent 7d4dd26 commit 9f14c81

File tree

2 files changed

+48
-43
lines changed
  • apps/sim
    • app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/file-upload
    • blocks/blocks

2 files changed

+48
-43
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/file-upload/file-upload.tsx

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
import { useEffect, useMemo, useRef, useState } from 'react'
44
import { createLogger } from '@sim/logger'
5+
import { useQueryClient } from '@tanstack/react-query'
56
import { X } from 'lucide-react'
67
import { useParams } from 'next/navigation'
78
import { Button, Combobox } from '@/components/emcn/components'
89
import { Progress } from '@/components/ui/progress'
910
import { cn } from '@/lib/core/utils/cn'
10-
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
1111
import { getExtensionFromMimeType } from '@/lib/uploads/utils/file-utils'
1212
import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-sub-block-value'
13+
import { useWorkspaceFiles, workspaceFilesKeys } from '@/hooks/queries/workspace-files'
1314
import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1415
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
1516

@@ -150,8 +151,6 @@ export function FileUpload({
150151
const [storeValue, setStoreValue] = useSubBlockValue(blockId, subBlockId)
151152
const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([])
152153
const [uploadProgress, setUploadProgress] = useState(0)
153-
const [workspaceFiles, setWorkspaceFiles] = useState<WorkspaceFileRecord[]>([])
154-
const [loadingWorkspaceFiles, setLoadingWorkspaceFiles] = useState(false)
155154
const [uploadError, setUploadError] = useState<string | null>(null)
156155
const [inputValue, setInputValue] = useState('')
157156

@@ -163,25 +162,14 @@ export function FileUpload({
163162
const params = useParams()
164163
const workspaceId = params?.workspaceId as string
165164

166-
const value = isPreview ? previewValue : storeValue
167-
168-
const loadWorkspaceFiles = async () => {
169-
if (!workspaceId || isPreview) return
170-
171-
try {
172-
setLoadingWorkspaceFiles(true)
173-
const response = await fetch(`/api/workspaces/${workspaceId}/files`)
174-
const data = await response.json()
165+
const queryClient = useQueryClient()
166+
const {
167+
data: workspaceFiles = [],
168+
isLoading: loadingWorkspaceFiles,
169+
refetch: refetchWorkspaceFiles,
170+
} = useWorkspaceFiles(workspaceId)
175171

176-
if (data.success) {
177-
setWorkspaceFiles(data.files || [])
178-
}
179-
} catch (error) {
180-
logger.error('Error loading workspace files:', error)
181-
} finally {
182-
setLoadingWorkspaceFiles(false)
183-
}
184-
}
172+
const value = isPreview ? previewValue : storeValue
185173

186174
/**
187175
* Checks if a file's MIME type matches the accepted types
@@ -226,10 +214,6 @@ export function FileUpload({
226214
return !isAlreadySelected
227215
})
228216

229-
useEffect(() => {
230-
void loadWorkspaceFiles()
231-
}, [workspaceId])
232-
233217
/**
234218
* Opens file dialog
235219
*/
@@ -394,7 +378,7 @@ export function FileUpload({
394378
setUploadError(null)
395379

396380
if (workspaceId) {
397-
void loadWorkspaceFiles()
381+
void queryClient.invalidateQueries({ queryKey: workspaceFilesKeys.lists() })
398382
}
399383

400384
if (uploadedFiles.length === 1) {
@@ -726,7 +710,7 @@ export function FileUpload({
726710
value={inputValue}
727711
onChange={handleComboboxChange}
728712
onOpenChange={(open) => {
729-
if (open) void loadWorkspaceFiles()
713+
if (open) void refetchWorkspaceFiles()
730714
}}
731715
placeholder={loadingWorkspaceFiles ? 'Loading files...' : '+ Add More'}
732716
disabled={disabled || loadingWorkspaceFiles}
@@ -746,7 +730,7 @@ export function FileUpload({
746730
onInputChange={handleComboboxChange}
747731
onClear={(e) => handleRemoveFile(filesArray[0], e)}
748732
onOpenChange={(open) => {
749-
if (open) void loadWorkspaceFiles()
733+
if (open) void refetchWorkspaceFiles()
750734
}}
751735
disabled={disabled}
752736
isLoading={loadingWorkspaceFiles}
@@ -763,7 +747,7 @@ export function FileUpload({
763747
value={inputValue}
764748
onChange={handleComboboxChange}
765749
onOpenChange={(open) => {
766-
if (open) void loadWorkspaceFiles()
750+
if (open) void refetchWorkspaceFiles()
767751
}}
768752
placeholder={loadingWorkspaceFiles ? 'Loading files...' : 'Select or upload file'}
769753
disabled={disabled || loadingWorkspaceFiles}

apps/sim/blocks/blocks/file.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -318,23 +318,26 @@ export const FileV3Block: BlockConfig<FileParserV3Output> = {
318318
condition: { field: 'operation', value: 'file_write' },
319319
mode: 'advanced',
320320
},
321+
{
322+
id: 'appendFile',
323+
title: 'File',
324+
type: 'file-upload' as SubBlockType,
325+
canonicalParamId: 'appendFileInput',
326+
acceptedTypes: '.txt,.md,.json,.csv,.xml,.html,.htm,.yaml,.yml,.log,.rtf',
327+
placeholder: 'Select or upload a workspace file',
328+
mode: 'basic',
329+
condition: { field: 'operation', value: 'file_append' },
330+
required: { field: 'operation', value: 'file_append' },
331+
},
321332
{
322333
id: 'appendFileName',
323334
title: 'File',
324-
type: 'dropdown' as SubBlockType,
325-
placeholder: 'Select a workspace file...',
335+
type: 'short-input' as SubBlockType,
336+
canonicalParamId: 'appendFileInput',
337+
placeholder: 'File name (e.g., notes.md)',
338+
mode: 'advanced',
326339
condition: { field: 'operation', value: 'file_append' },
327340
required: { field: 'operation', value: 'file_append' },
328-
options: [],
329-
fetchOptions: async () => {
330-
const { useWorkflowRegistry } = await import('@/stores/workflows/registry/store')
331-
const workspaceId = useWorkflowRegistry.getState().hydration.workspaceId
332-
if (!workspaceId) return []
333-
const response = await fetch(`/api/workspaces/${workspaceId}/files`)
334-
const data = await response.json()
335-
if (!data.success || !data.files) return []
336-
return data.files.map((f: { name: string }) => ({ label: f.name, id: f.name }))
337-
},
338341
},
339342
{
340343
id: 'appendContent',
@@ -362,8 +365,26 @@ export const FileV3Block: BlockConfig<FileParserV3Output> = {
362365
}
363366

364367
if (operation === 'file_append') {
368+
const appendInput = params.appendFileInput
369+
if (!appendInput) {
370+
throw new Error('File is required for append')
371+
}
372+
373+
let fileName: string
374+
if (typeof appendInput === 'string') {
375+
fileName = appendInput.trim()
376+
} else {
377+
const normalized = normalizeFileInput(appendInput, { single: true })
378+
const file = normalized as Record<string, unknown> | null
379+
fileName = (file?.name as string) ?? ''
380+
}
381+
382+
if (!fileName) {
383+
throw new Error('Could not determine file name')
384+
}
385+
365386
return {
366-
fileName: params.appendFileName,
387+
fileName,
367388
content: params.appendContent,
368389
workspaceId: params._context?.workspaceId,
369390
}
@@ -413,7 +434,7 @@ export const FileV3Block: BlockConfig<FileParserV3Output> = {
413434
fileName: { type: 'string', description: 'Name for a new file (write)' },
414435
content: { type: 'string', description: 'File content to write' },
415436
contentType: { type: 'string', description: 'MIME content type for write' },
416-
appendFileName: { type: 'string', description: 'Name of existing file to append to' },
437+
appendFileInput: { type: 'json', description: 'File to append to (canonical param)' },
417438
appendContent: { type: 'string', description: 'Content to append to file' },
418439
},
419440
outputs: {

0 commit comments

Comments
 (0)