Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat(app): better import function

dextmorgnef0f6fe10756
diff --git a/flowsint-app/src/api/sketch-service.ts b/flowsint-app/src/api/sketch-service.ts
index 496c4120..6cdc943e 100644
--- a/flowsint-app/src/api/sketch-service.ts
+++ b/flowsint-app/src/api/sketch-service.ts
@@ -90,11 +90,9 @@ export const sketchService = {
},
executeImport: async (
sketchId: string,
- file: File,
- entityMappings: Array<{ row_index: number; entity_type: string; include: boolean; label?: string }>,
+ entityMappings: Array<{ id: string; entity_type: string; include: boolean; label: string; data: Record<string, any> }>,
): Promise<any> => {
const formData = new FormData()
- formData.append('file', file)
formData.append('entity_mappings_json', JSON.stringify(entityMappings))
return fetchWithAuth(`/api/sketches/${sketchId}/import/execute`, {
diff --git a/flowsint-app/src/components/graphs/add-item-dialog.tsx b/flowsint-app/src/components/graphs/add-item-dialog.tsx
index 23964075..f38f0213 100644
--- a/flowsint-app/src/components/graphs/add-item-dialog.tsx
+++ b/flowsint-app/src/components/graphs/add-item-dialog.tsx
@@ -319,7 +319,7 @@ function ActionCard({ item, onSelect }: ActionCardProps) {
return (
<Card
className={cn(
- 'cursor-pointer transition-all hover:border-primary border border-transparent hover:shadow-md bg-muted',
+ 'cursor-pointer transition-all hover:border-primary border rounded border-transparent hover:shadow-md bg-muted',
item.disabled && 'opacity-50 cursor-not-allowed',
'h-full'
)}
diff --git a/flowsint-app/src/components/graphs/import-preview.tsx b/flowsint-app/src/components/graphs/import-preview.tsx
index 69991d0d..1fff718d 100644
--- a/flowsint-app/src/components/graphs/import-preview.tsx
+++ b/flowsint-app/src/components/graphs/import-preview.tsx
@@ -1,6 +1,7 @@
-import { useState, useMemo, useCallback, memo } from 'react'
+import { useState, useMemo, memo, useCallback, useEffect, useRef } from 'react'
import { Button } from '@/components/ui/button'
import { Label } from '@/components/ui/label'
+import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs'
import {
Select,
SelectContent,
@@ -13,32 +14,12 @@ import { Input } from '@/components/ui/input'
import { CheckCircle2, XCircle, Loader2, ChevronLeft, ChevronRight } from 'lucide-react'
import { sketchService } from '@/api/sketch-service'
import { useActionItems } from '@/hooks/use-action-items'
-import {
- useReactTable,
- getCoreRowModel,
- getPaginationRowModel,
- flexRender,
- createColumnHelper,
-} from '@tanstack/react-table'
import { toast } from 'sonner'
import { useGraphControls } from '@/stores/graph-controls-store'
-
-interface EntityPreview {
- row_index: number
- data: Record<string, any>
- detected_type: string
- primary_value: string
-}
-
-interface AnalysisResult {
- entities: EntityPreview[]
- total_entities: number
- type_distribution: Record<string, number>
- columns: string[]
-}
+import { v4 as uuidv4 } from 'uuid'
interface EntityMapping {
- row_index: number
+ id: string
entity_type: string
include: boolean
label: string
@@ -46,315 +27,542 @@ interface EntityMapping {
}
interface ImportPreviewProps {
- analysisResult: AnalysisResult
- file: File
+ analysisResult: any
sketchId: string
onSuccess: () => void
onCancel: () => void
}
-type TableRow = EntityPreview & {
- mapping: EntityMapping
-}
-
-// Memoized cell components to prevent unnecessary re-renders
-const IncludeCell = memo(({
- checked,
- rowIndex,
- onChange
-}: {
- checked: boolean
- rowIndex: number
- onChange: (rowIndex: number, include: boolean) => void
-}) => (
- <Checkbox
- checked={checked}
- onCheckedChange={(checked) => onChange(rowIndex, checked as boolean)}
- />
-))
-IncludeCell.displayName = 'IncludeCell'
-
-const EntityTypeCell = memo(({
- value,
- rowIndex,
- onChange,
- disabled,
- entityTypes
-}: {
+// ============================================================================
+// FIX 1: Debounced Input component to avoid re-renders on every keystroke
+// ============================================================================
+const DebouncedInput = memo(({
+ value,
+ onChange,
+ disabled,
+ placeholder,
+ className,
+ debounceMs = 150
+}: {
value: string
- rowIndex: number
- onChange: (rowIndex: number, entityType: string) => void
- disabled: boolean
+ onChange: (value: string) => void
+ disabled?: boolean
+ placeholder?: string
+ className?: string
+ debounceMs?: number
+}) => {
+ const [localValue, setLocalValue] = useState(value)
+ const timeoutRef = useRef<NodeJS.Timeout>(null)
+
+ // Sync from parent when value changes externally
+ useEffect(() => {
+ setLocalValue(value)
+ }, [value])
+
+ const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
+ const newValue = e.target.value
+ setLocalValue(newValue)
+
+ // Clear previous timeout
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current)
+ }
+
+ // Debounce the parent update
+ timeoutRef.current = setTimeout(() => {
+ onChange(newValue)
+ }, debounceMs)
+ }, [onChange, debounceMs])
+
+ // Cleanup on unmount
+ useEffect(() => {
+ return () => {
+ if (timeoutRef.current) {
+ clearTimeout(timeoutRef.current)
+ }
+ }
+ }, [])
+
+ return (
+ <Input
+ className={className}
+ value={localValue}
+ onChange={handleChange}
+ disabled={disabled}
+ placeholder={placeholder}
+ />
+ )
+})
+DebouncedInput.displayName = 'DebouncedInput'
+
+const EntityRow = memo(({
+ mapping,
+ fields,
+ entityTypes,
+ isDisabled,
+ onIncludeChange,
+ onTypeChange,
+ onLabelChange,
+ onFieldChange
+}: {
+ mapping: EntityMapping
+ fields: string[]
entityTypes: string[]
-}) => (
- <Select
- value={value}
- onValueChange={(value) => onChange(rowIndex, value)}
- disabled={disabled}
- >
- <SelectTrigger className="h-8 w-full text-xs">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {entityTypes.map((type) => (
- <SelectItem key={type} value={type}>
- {type}
- </SelectItem>
+ isDisabled: boolean
+ onIncludeChange: (id: string, include: boolean) => void
+ onTypeChange: (id: string, type: string) => void
+ onLabelChange: (id: string, label: string) => void
+ onFieldChange: (id: string, field: string, value: string) => void
+}) => {
+ // Create stable callbacks for this specific row
+ const handleInclude = useCallback((checked: boolean) => {
+ onIncludeChange(mapping.id, checked)
+ }, [mapping.id, onIncludeChange])
+
+ const handleType = useCallback((value: string) => {
+ onTypeChange(mapping.id, value)
+ }, [mapping.id, onTypeChange])
+
+ const handleLabel = useCallback((value: string) => {
+ onLabelChange(mapping.id, value)
+ }, [mapping.id, onLabelChange])
+
+ return (
+ <div className={`flex border-b ${!mapping.include ? 'opacity-50' : ''}`}>
+ <div className="px-3 py-2 border-r w-[60px] shrink-0 flex items-center">
+ <Checkbox
+ checked={mapping.include}
+ onCheckedChange={handleInclude}
+ />
+ </div>
+ <div className="px-3 py-2 border-r w-[160px] shrink-0">
+ <Select
+ value={mapping.entity_type}
+ onValueChange={handleType}
+ disabled={!mapping.include || isDisabled}
+ >
+ <SelectTrigger className="h-8 w-full text-xs">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {entityTypes.map((type) => (
+ <SelectItem key={type} value={type}>{type}</SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ <div className="px-3 py-2 border-r w-[200px] shrink-0">
+ <DebouncedInput
+ className="h-8 w-full text-xs"
+ value={mapping.label}
+ onChange={handleLabel}
+ disabled={!mapping.include}
+ placeholder="Label..."
+ />
+ </div>
+ {fields.map((field) => (
+ <div key={field} className="px-3 py-2 border-r w-[200px] shrink-0">
+ <DebouncedInput
+ className="h-8 w-full text-xs"
+ value={mapping.data[field] || ''}
+ onChange={(value) => onFieldChange(mapping.id, field, value)}
+ disabled={!mapping.include}
+ placeholder="-"
+ />
+ </div>
))}
- </SelectContent>
- </Select>
-))
-EntityTypeCell.displayName = 'EntityTypeCell'
-
-const LabelCell = memo(({
- value,
- rowIndex,
- onChange,
- disabled
-}: {
- value: string
- rowIndex: number
- onChange: (rowIndex: number, label: string) => void
- disabled: boolean
-}) => (
- <Input
- className="h-8 w-full text-xs"
- value={value}
- onChange={(e) => onChange(rowIndex, e.target.value)}
- disabled={disabled}
- placeholder="Enter label..."
- />
-))
-LabelCell.displayName = 'LabelCell'
-
-const DataCell = memo(({
- value,
- rowIndex,
- dataKey,
- onChange,
- disabled
-}: {
- value: string
- rowIndex: number
- dataKey: string
- onChange: (rowIndex: number, key: string, value: string) => void
- disabled: boolean
-}) => (
- <Input
- className="h-8 w-full text-xs"
- value={value || ''}
- onChange={(e) => onChange(rowIndex, dataKey, e.target.value)}
- disabled={disabled}
- placeholder="-"
- />
-))
-DataCell.displayName = 'DataCell'
+ </div>
+ )
+}, (prevProps, nextProps) => {
+ // Custom comparison for better memoization
+ return (
+ prevProps.mapping === nextProps.mapping &&
+ prevProps.fields === nextProps.fields &&
+ prevProps.entityTypes === nextProps.entityTypes &&
+ prevProps.isDisabled === nextProps.isDisabled
+ )
+})
+EntityRow.displayName = 'EntityRow'
+
+interface TypeTableProps {
+ mappings: EntityMapping[]
+ fields: string[]
+ entityTypes: string[]
+ isDisabled: boolean
+ currentPage: number
+ itemsPerPage: number
+ onPageChange: (page: number) => void
+ onItemsPerPageChange: (count: number) => void
+ onIncludeChange: (id: string, include: boolean) => void
+ onTypeChange: (id: string, type: string) => void
+ onLabelChange: (id: string, label: string) => void
+ onFieldChange: (id: string, field: string, value: string) => void
+}
+
+const SimpleTypeTable = memo(({
+ mappings,
+ fields,
+ entityTypes,
+ isDisabled,
+ currentPage,
+ itemsPerPage,
+ onPageChange,
+ onItemsPerPageChange,
+ onIncludeChange,
+ onTypeChange,
+ onLabelChange,
+ onFieldChange
+}: TypeTableProps) => {
+ const totalPages = Math.ceil(mappings.length / itemsPerPage)
+ const startIndex = (currentPage - 1) * itemsPerPage
+ const endIndex = startIndex + itemsPerPage
+
+ const paginatedMappings = useMemo(
+ () => mappings.slice(startIndex, endIndex),
+ [mappings, startIndex, endIndex]
+ )
+
+ const handleItemsPerPageChange = useCallback((value: string) => {
+ onItemsPerPageChange(Number(value))
+ }, [onItemsPerPageChange])
+
+ const handlePrevPage = useCallback(() => {
+ onPageChange(currentPage - 1)
+ }, [currentPage, onPageChange])
+
+ const handleNextPage = useCallback(() => {
+ onPageChange(currentPage + 1)
+ }, [currentPage, onPageChange])
+
+ const totalWidth = 60 + 160 + 200 + (fields.length * 200)
+
+ return (
+ <div className="h-full flex flex-col">
+ <div className="border rounded-lg overflow-auto flex-1 min-h-0">
+ <div style={{ minWidth: totalWidth }}>
+ {/* Sticky header */}
+ <div className="flex bg-muted sticky top-0 z-10 border-b">
+ <div className="px-3 py-2 text-left text-xs font-medium border-r w-[60px] shrink-0">
+ Include
+ </div>
+ <div className="px-3 py-2 text-left text-xs font-medium border-r w-[160px] shrink-0">
+ Type
+ </div>
+ <div className="px-3 py-2 text-left text-xs font-medium border-r w-[200px] shrink-0">
+ Label
+ </div>
+ {fields.map((field) => (
+ <div key={field} className="px-3 py-2 text-left text-xs font-medium border-r w-[200px] shrink-0">
+ {field}
+ </div>
+ ))}
+ </div>
+
+ {/* Simple body - no virtualization */}
+ <div>
+ {paginatedMappings.map((mapping) => (
+ <EntityRow
+ key={mapping.id}
+ mapping={mapping}
+ fields={fields}
+ entityTypes={entityTypes}
+ isDisabled={isDisabled}
+ onIncludeChange={onIncludeChange}
+ onTypeChange={onTypeChange}
+ onLabelChange={onLabelChange}
+ onFieldChange={onFieldChange}
+ />
+ ))}
+ </div>
+ </div>
+ </div>
+
+ {/* Pagination controls */}
+ <div className="flex items-center justify-between pt-3 gap-4">
+ <div className="flex items-center gap-2">
+ <span className="text-xs text-muted-foreground">Rows per page:</span>
+ <Select value={String(itemsPerPage)} onValueChange={handleItemsPerPageChange}>
+ <SelectTrigger className="h-8 w-[70px]">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="10">10</SelectItem>
+ <SelectItem value="20">20</SelectItem>
+ <SelectItem value="50">50</SelectItem>
+ </SelectContent>
+ </Select>
+ </div>
+
+ <div className="flex items-center gap-4">
+ <span className="text-xs text-muted-foreground">
+ {startIndex + 1}-{Math.min(endIndex, mappings.length)} of {mappings.length}
+ </span>
+ <div className="flex items-center gap-1">
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handlePrevPage}
+ disabled={currentPage === 1}
+ className="h-8 w-8 p-0"
+ >
+ <ChevronLeft className="h-4 w-4" />
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ onClick={handleNextPage}
+ disabled={currentPage === totalPages}
+ className="h-8 w-8 p-0"
+ >
+ <ChevronRight className="h-4 w-4" />
+ </Button>
+ </div>
+ </div>
+ </div>
+ </div>
+ )
+})
+SimpleTypeTable.displayName = 'SimpleTypeTable'
export function ImportPreview({
analysisResult,
- file,
sketchId,
onSuccess,
onCancel
}: ImportPreviewProps) {
const { actionItems, isLoading: isLoadingActionItems } = useActionItems()
const refetchGraph = useGraphControls((s) => s.refetchGraph)
-
- const [entityMappings, setEntityMappings] = useState<EntityMapping[]>(() => {
- return analysisResult.entities.map((entity) => ({
- row_index: entity.row_index,
- entity_type: entity.detected_type,
- include: true,
- label: entity.primary_value,
- data: { ...entity.data }
- }))
- })
- const allDataKeys = useMemo(() => {
- const keysSet = new Set<string>()
- analysisResult.entities.forEach(entity => {
- Object.keys(entity.data).forEach(key => keysSet.add(key))
+ const fieldsByType = useMemo(() => {
+ if (!actionItems) return {}
+ const fields: Record<string, string[]> = {}
+
+ actionItems.forEach(item => {
+ if (item.children) {
+ item.children.forEach(child => {
+ fields[child.label] = child.fields
+ .filter((f: any) => f.name !== 'label')
+ .map((f: any) => f.name)
+ })
+ } else if (item.fields) {
+ fields[item.label] = item.fields
+ .filter((f: any) => f.name !== 'label')
+ .map((f: any) => f.name)
+ }
})
- return Array.from(keysSet)
- }, [analysisResult.entities])
- const [isImporting, setIsImporting] = useState(false)
- const [importResult, setImportResult] = useState<{
- status: string
- nodes_created: number
- nodes_skipped: number
- errors: string[]
- } | null>(null)
-
- // Memoized callbacks to prevent recreation on every render
- const handleIncludeChange = useCallback((rowIndex: number, include: boolean) => {
- setEntityMappings((prev) =>
- prev.map((mapping) =>
- mapping.row_index === rowIndex ? { ...mapping, include } : mapping
- )
- )
+ return fields
+ }, [actionItems])
+
+ const [mappingsById, setMappingsById] = useState<Map<string, EntityMapping>>(new Map())
+ const [mappingIdsByType, setMappingIdsByType] = useState<Record<string, string[]>>({})
+ const [isInitialized, setIsInitialized] = useState(false)
+
+ useEffect(() => {
+ if (!actionItems || isInitialized) return
+
+ const newMappingsById = new Map<string, EntityMapping>()
+ const newMappingIdsByType: Record<string, string[]> = {}
+
+ Object.entries(analysisResult.entities).forEach(([typeName, group]: [string, any]) => {
+ const typeFields = fieldsByType[typeName] || []
+ const ids: string[] = []
+
+ group.results.forEach((entity: any) => {
+ const data: Record<string, any> = {}
+ typeFields.forEach(field => {
+ data[field] = entity.obj[field] ?? ''
+ })
+ data.label = entity.obj.label
+
+ const id = uuidv4()
+ ids.push(id)
+
+ newMappingsById.set(id, {
+ id,
+ entity_type: entity.detected_type,
+ include: true,
+ label: entity.obj.label,
+ data
+ })
+ })
+
+ newMappingIdsByType[typeName] = ids
+ })
+
+ setMappingsById(newMappingsById)
+ setMappingIdsByType(newMappingIdsByType)
+ setIsInitialized(true)
+ }, [actionItems, analysisResult.entities, fieldsByType, isInitialized])
+
+ const [paginationByType, setPaginationByType] = useState<Record<string, { page: number; perPage: number }>>({})
+
+ const getPagination = useCallback((typeName: string) => {
+ return paginationByType[typeName] || { page: 1, perPage: 20 }
+ }, [paginationByType])
+
+ const setPageForType = useCallback((typeName: string, page: number) => {
+ setPaginationByType(prev => ({
+ ...prev,
+ [typeName]: { ...getPagination(typeName), page }
+ }))
+ }, [getPagination])
+
+ const setPerPageForType = useCallback((typeName: string, perPage: number) => {
+ setPaginationByType(prev => ({
+ ...prev,
+ [typeName]: { page: 1, perPage } // Reset to page 1 when changing items per page
+ }))
}, [])
- const handleTypeChange = useCallback((rowIndex: number, entityType: string) => {
- setEntityMappings((prev) =>
- prev.map((mapping) =>
- mapping.row_index === rowIndex
- ? { ...mapping, entity_type: entityType }
- : mapping
- )
- )
+ const [isImporting, setIsImporting] = useState(false)
+ const [importResult, setImportResult] = useState<any>(null)
+
+ // ============================================================================
+ // FIX 7: Memoize entity types with stable reference
+ // ============================================================================
+ const entityTypes = useMemo(() => {
+ if (!actionItems) return []
+ const types: string[] = []
+ actionItems.forEach(item => {
+ if (item.children) {
+ item.children.forEach((c: any) => c.label && types.push(c.label))
+ } else if (item.label) {
+ types.push(item.label)
+ }
+ })
+ return [...new Set(types)]
+ }, [actionItems])
+
+ // ============================================================================
+ // FIX 8: Granular update handlers - only update the specific mapping
+ // ============================================================================
+ const handleIncludeChange = useCallback((id: string, include: boolean) => {
+ setMappingsById(prev => {
+ const mapping = prev.get(id)
+ if (!mapping) return prev
+ const newMap = new Map(prev)
+ newMap.set(id, { ...mapping, include })
+ return newMap
+ })
}, [])
- const handleLabelChange = useCallback((rowIndex: number, label: string) => {
- setEntityMappings((prev) =>
- prev.map((mapping) =>
- mapping.row_index === rowIndex ? { ...mapping, label } : mapping
- )
- )
+ const handleTypeChange = useCallback((id: string, newType: string) => {
+ setMappingsById(prev => {
+ const mapping = prev.get(id)
+ if (!mapping || mapping.entity_type === newType) return prev
+
+ const newFields = fieldsByType[newType] || []
+ const newData: Record<string, any> = {}
+ newFields.forEach(field => {
+ newData[field] = mapping.data[field] ?? ''
+ })
+ newData.label = mapping.label
+
+ const newMap = new Map(prev)
+ newMap.set(id, { ...mapping, entity_type: newType, data: newData })
+ return newMap
+ })
+ }, [fieldsByType])
+
+ const handleLabelChange = useCallback((id: string, label: string) => {
+ setMappingsById(prev => {
+ const mapping = prev.get(id)
+ if (!mapping) return prev
+ const newMap = new Map(prev)
+ newMap.set(id, { ...mapping, label })
+ return newMap
+ })
}, [])
- const handleDataChange = useCallback((rowIndex: number, key: string, value: string) => {
- setEntityMappings((prev) =>
- prev.map((mapping) =>
- mapping.row_index === rowIndex
- ? { ...mapping, data: { ...mapping.data, [key]: value } }
- : mapping
- )
- )
+ const handleFieldChange = useCallback((id: string, field: string, value: string) => {
+ setMappingsById(prev => {
+ const mapping = prev.get(id)
+ if (!mapping) return prev
+ const newMap = new Map(prev)
+ newMap.set(id, {
+ ...mapping,
+ data: { ...mapping.data, [field]: value }
+ })
+ return newMap
+ })
}, [])
- const handleImport = async () => {
+ // ============================================================================
+ // FIX 9: Memoize mappings arrays per type to avoid recreating on every render
+ // ============================================================================
+ const getMappingsForType = useCallback((typeName: string): EntityMapping[] => {
+ const ids = mappingIdsByType[typeName] || []
+ return ids.map(id => mappingsById.get(id)!).filter(Boolean)
+ }, [mappingIdsByType, mappingsById])
+
+ // Pre-compute mappings for all types to ensure stable references
+ const mappingsByType = useMemo(() => {
+ const result: Record<string, EntityMapping[]> = {}
+ Object.keys(mappingIdsByType).forEach(typeName => {
+ result[typeName] = getMappingsForType(typeName)
+ })
+ return result
+ }, [mappingIdsByType, getMappingsForType])
+
+ const handleImport = useCallback(async () => {
setIsImporting(true)
try {
- const result = await sketchService.executeImport(
- sketchId,
- file,
- entityMappings
- )
+ const mappingsArray = Array.from(mappingsById.values())
+ const result = await sketchService.executeImport(sketchId, mappingsArray)
setImportResult(result)
if (result.status === 'completed') {
- setTimeout(() => {
- onSuccess()
- }, 2000)
+ setTimeout(onSuccess, 2000)
}
} catch (error) {
- toast.error('Failed to import entities. Please try again.')
+ toast.error('Failed to import')
} finally {
refetchGraph()
setIsImporting(false)
- toast.success("Import successfull !")
+ toast.success('Import successful!')
}
- }
+ }, [mappingsById, sketchId, onSuccess, refetchGraph])
- const entityTypes = useMemo<string[]>(() => {
- if (!actionItems) return []
- const keys: string[] = []
- for (const item of actionItems) {
- if (item.children && item.children.length > 0) {
- for (const child of item.children) {
- if (child.label) keys.push(child.label)
- }
- } else if (item.label) {
- keys.push(item.label)
- }
+ const typeNames = useMemo(() => Object.keys(mappingIdsByType), [mappingIdsByType])
+ const [activeTab, setActiveTab] = useState('')
+
+ // Set initial active tab when types are loaded
+ useEffect(() => {
+ if (typeNames.length > 0 && !activeTab) {
+ setActiveTab(typeNames[0])
}
- return Array.from(new Set(keys))
- }, [actionItems])
+ }, [typeNames, activeTab])
+
+ const includedCount = useMemo(() => {
+ let count = 0
+ mappingsById.forEach(m => {
+ if (m.include) count++
+ })
+ return count
+ }, [mappingsById])
- const tableData: TableRow[] = useMemo(() => {
- return analysisResult.entities.map((entity) => ({
- ...entity,
- mapping: entityMappings.find(m => m.row_index === entity.row_index)!
- }))
- }, [analysisResult.entities, entityMappings])
-
- // Define columns ONCE with stable references
- const columns = useMemo(() => {
- const columnHelper: any = createColumnHelper() as any
-
- const baseColumns = [
- columnHelper.display({
- id: 'include',
- header: 'Include',
- size: 60,
- cell: ({ row }: { row: any }) => (
- <IncludeCell
- checked={row.original.mapping.include}
- rowIndex={row.original.row_index}
- onChange={handleIncludeChange}
- />
- ),
- }),
- columnHelper.display({
- id: 'entity_type',
- header: 'Entity Type',
- size: 160,
- cell: ({ row }: { row: any }) => (
- <EntityTypeCell
- value={row.original.mapping.entity_type}
- rowIndex={row.original.row_index}
- onChange={handleTypeChange}
- disabled={!row.original.mapping.include || isLoadingActionItems}
- entityTypes={entityTypes}
- />
- ),
- }),
- columnHelper.display({
- id: 'label',
- header: 'Label *',
- size: 200,
- cell: ({ row }: { row: any }) => (
- <LabelCell
- value={row.original.mapping.label}
- rowIndex={row.original.row_index}
- onChange={handleLabelChange}
- disabled={!row.original.mapping.include}
- />
- ),
- }),
- ]
-
- const dataColumns = allDataKeys.map((key) =>
- columnHelper.display({
- id: `data_${key}`,
- header: key,
- size: 200,
- cell: ({ row }: { row: any }) => (
- <DataCell
- value={row.original.mapping.data[key]}
- rowIndex={row.original.row_index}
- dataKey={key}
- onChange={handleDataChange}
- disabled={!row.original.mapping.include}
- />
- ),
- })
- )
- return [...baseColumns, ...dataColumns]
- }, [
- allDataKeys,
- entityTypes,
- isLoadingActionItems,
- handleIncludeChange,
- handleTypeChange,
- handleLabelChange,
- handleDataChange
- ])
-
- const table = useReactTable({
- data: tableData,
- columns,
- getCoreRowModel: getCoreRowModel(),
- getPaginationRowModel: getPaginationRowModel(),
- autoResetPageIndex: false, // Prevent pagination reset on data changes
- initialState: {
- pagination: {
- pageSize: 20,
- },
- },
- })
+ const getFieldsForType = useCallback((typeName: string): string[] => {
+ return fieldsByType[typeName] || []
+ }, [fieldsByType])
+
+ const fieldsPerType = useMemo(() => {
+ const result: Record<string, string[]> = {}
+ typeNames.forEach(name => {
+ result[name] = getFieldsForType(name)
+ })
+ return result
+ }, [typeNames, getFieldsForType])
+
+ // Show loading state while initializing
+ if (!isInitialized) {
+ return (
+ <div className="flex items-center justify-center h-full">
+ <Loader2 className="h-8 w-8 animate-spin" />
+ </div>
+ )
+ }
if (importResult) {
return (
@@ -368,11 +576,6 @@ export function ImportPreview({
<p className="text-sm text-muted-foreground mt-2">
{importResult.nodes_created} entities created
</p>
- {importResult.nodes_skipped > 0 && (
- <p className="text-sm text-muted-foreground">
- {importResult.nodes_skipped} entities skipped
- </p>
- )}
</div>
</>
) : (
@@ -381,29 +584,22 @@ export function ImportPreview({
<div className="text-center">
<h3 className="text-lg font-semibold">Import Completed with Errors</h3>
<p className="text-sm text-muted-foreground mt-2">
- {importResult.nodes_created} entities created
- </p>
- <p className="text-sm text-muted-foreground">
- {importResult.errors.length} errors encountered
+ {importResult.nodes_created} created, {importResult.errors.length} errors
</p>
</div>
{importResult.errors.length > 0 && (
<div className="w-full mt-4">
<Label>Errors:</Label>
<div className="h-32 w-full rounded-md border p-2 mt-2 overflow-auto">
- {importResult.errors.map((error, idx) => (
- <p key={idx} className="text-xs text-red-500 mb-1">
- {error}
- </p>
+ {importResult.errors.map((error: string, idx: number) => (
+ <p key={idx} className="text-xs text-red-500 mb-1">{error}</p>
))}
</div>
</div>
)}
</>
)}
- <Button onClick={onSuccess} className="mt-4">
- Close
- </Button>
+ <Button onClick={onSuccess} className="mt-4">Close</Button>
</div>
</div>
)
@@ -411,92 +607,47 @@ export function ImportPreview({
return (
<div className="flex flex-col h-full overflow-hidden">
- <div className="flex flex-col flex-grow overflow-hidden">
- <div className="flex-grow overflow-auto border rounded-lg">
- <table
- className="w-full"
- style={{ borderCollapse: 'separate', borderSpacing: 0 }}
- >
- <thead className="bg-muted sticky top-0 z-10">
- {table.getHeaderGroups().map((headerGroup: any) => (
- <tr key={headerGroup.id}>
- {headerGroup.headers.map((header: any) => (
- <th
- key={header.id}
- className="px-3 py-2 text-left text-xs font-medium border-b border-r bg-muted"
- style={{
- width: `${header.getSize()}px`,
- minWidth: `${header.getSize()}px`,
- maxWidth: `${header.getSize()}px`,
- boxSizing: 'border-box',
- }}
- >
- {flexRender(header.column.columnDef.header, header.getContext())}
- </th>
- ))}
- </tr>
- ))}
- </thead>
- <tbody>
- {table.getRowModel().rows.map((row: any) => (
- <tr
- key={row.id}
- className={`border-b ${!row.original.mapping.include ? 'opacity-50' : ''}`}
- >
- {row.getVisibleCells().map((cell: any) => (
- <td
- key={cell.id}
- className="px-3 py-2 border-r"
- style={{
- width: `${cell.column.getSize()}px`,
- minWidth: `${cell.column.getSize()}px`,
- maxWidth: `${cell.column.getSize()}px`,
- boxSizing: 'border-box',
- }}
- >
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
- </td>
- ))}
- </tr>
- ))}
- </tbody>
- </table>
+ <Tabs value={activeTab} onValueChange={setActiveTab} className="flex-1 flex flex-col overflow-hidden">
+ <div className="px-4 pt-4">
+ <TabsList className="w-full justify-start">
+ {typeNames.map((name) => (
+ <TabsTrigger key={name} value={name} className="flex-1">
+ {name} ({mappingsByType[name]?.length || 0})
+ </TabsTrigger>
+ ))}
+ </TabsList>
</div>
- </div>
- <div className="flex items-center justify-between px-4 py-3 shrink-0 bg-background">
- <div className="text-sm text-muted-foreground">
- Showing {table.getState().pagination.pageIndex * table.getState().pagination.pageSize + 1} to{' '}
- {Math.min(
- (table.getState().pagination.pageIndex + 1) * table.getState().pagination.pageSize,
- table.getFilteredRowModel().rows.length
- )}{' '}
- of {table.getFilteredRowModel().rows.length} entities
+ <div className="flex-1 overflow-hidden px-4 py-4">
+ {typeNames.map((name) => {
+ const pagination = getPagination(name)
+ return (
+ <TabsContent
+ key={name}
+ value={name}
+ className="h-full mt-0"
+ forceMount // FIX 11: Keep mounted to preserve scroll position
+ hidden={activeTab !== name}
+ >
+ <SimpleTypeTable
+ mappings={mappingsByType[name]}
+ fields={fieldsPerType[name]}
+ entityTypes={entityTypes}
+ isDisabled={isLoadingActionItems}
+ currentPage={pagination.page}
+ itemsPerPage={pagination.perPage}
+ onPageChange={(page) => setPageForType(name, page)}
+ onItemsPerPageChange={(perPage) => setPerPageForType(name, perPage)}
+ onIncludeChange={handleIncludeChange}
+ onTypeChange={handleTypeChange}
+ onLabelChange={handleLabelChange}
+ onFieldChange={handleFieldChange}
+ />
+ </TabsContent>
+ )
+ })}
</div>
- <div className="flex items-center gap-2">
- <Button
- variant="outline"
- size="sm"
- onClick={() => table.previousPage()}
- disabled={!table.getCanPreviousPage()}
- >
- <ChevronLeft className="h-4 w-4" />
- Previous
- </Button>
- <span className="text-sm">
- Page {table.getState().pagination.pageIndex + 1} of {table.getPageCount()}
- </span>
- <Button
- variant="outline"
- size="sm"
- onClick={() => table.nextPage()}
- disabled={!table.getCanNextPage()}
- >
- Next
- <ChevronRight className="h-4 w-4" />
- </Button>
- </div>
- </div>
+ </Tabs>
<div className="flex justify-end gap-2 px-4 py-3 border-t shrink-0 bg-background">
<Button variant="outline" onClick={onCancel} disabled={isImporting}>
@@ -504,7 +655,7 @@ export function ImportPreview({
</Button>
<Button onClick={handleImport} disabled={isImporting}>
{isImporting && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
- {isImporting ? 'Importing...' : `Import ${entityMappings.filter(m => m.include).length} entities`}
+ {isImporting ? 'Importing...' : `Import ${includedCount} entities`}
</Button>
</div>
</div>
diff --git a/flowsint-app/src/components/graphs/import-sheet.tsx b/flowsint-app/src/components/graphs/import-sheet.tsx
index 79cccb7b..8372d187 100644
--- a/flowsint-app/src/components/graphs/import-sheet.tsx
+++ b/flowsint-app/src/components/graphs/import-sheet.tsx
@@ -7,7 +7,7 @@ import {
SheetTitle
} from '@/components/ui/sheet'
import { Button } from '@/components/ui/button'
-import { Upload, FileText, FileSpreadsheet } from 'lucide-react'
+import { Upload, FileText } from 'lucide-react'
import { cn } from '@/lib/utils'
import { ImportPreview } from './import-preview'
import { sketchService } from '@/api/sketch-service'
@@ -39,6 +39,26 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
setIsDragging(false)
}, [])
+ const handleFileSelect = useCallback(async (selectedFile: File) => {
+ // Validate file type - only TXT files are supported
+ const fileExtension = selectedFile.name.toLowerCase().substring(selectedFile.name.lastIndexOf('.'))
+ if (fileExtension !== '.txt') {
+ toast.error('Only .txt files are supported. Please upload a text file with one value per line.')
+ return
+ }
+ setFile(selectedFile)
+ setIsAnalyzing(true)
+ try {
+ const result = await sketchService.analyzeImportFile(sketchId, selectedFile)
+ setAnalysisResult(result)
+ } catch (error) {
+ toast.error('Failed to analyze file. Please try again.')
+ setFile(null)
+ } finally {
+ setIsAnalyzing(false)
+ }
+ }, [sketchId])
+
const handleDrop = useCallback((e: React.DragEvent) => {
e.preventDefault()
e.stopPropagation()
@@ -48,74 +68,49 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
if (droppedFile) {
handleFileSelect(droppedFile)
}
- }, [])
+ }, [handleFileSelect])
const handleFileInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const selectedFile = e.target.files?.[0]
if (selectedFile) {
handleFileSelect(selectedFile)
}
- }, [])
-
- const handleFileSelect = async (selectedFile: File) => {
- // Validate file type
- const validExtensions = ['.csv', '.txt', '.xlsx', '.xls']
- const fileExtension = selectedFile.name.toLowerCase().substring(selectedFile.name.lastIndexOf('.'))
- if (!validExtensions.includes(fileExtension)) {
- alert('Please upload a CSV, TXT, or XLSX file')
- return
- }
- setFile(selectedFile)
- setIsAnalyzing(true)
- try {
- const result = await sketchService.analyzeImportFile(sketchId, selectedFile)
- setAnalysisResult(result)
- } catch (error) {
- toast.error('Failed to analyze file. Please try again.')
- setFile(null)
- } finally {
- setIsAnalyzing(false)
- }
- }
+ }, [handleFileSelect])
- const handleReset = () => {
+ const handleReset = useCallback(() => {
setFile(null)
setAnalysisResult(null)
setIsAnalyzing(false)
- }
+ }, [])
- const handleClose = () => {
+ const handleClose = useCallback(() => {
handleReset()
onOpenChange(false)
- }
+ }, [handleReset, onOpenChange])
- const getFileIcon = (fileName: string) => {
- const extension = fileName.toLowerCase().substring(fileName.lastIndexOf('.'))
- if (extension === '.csv' || extension === '.txt') {
- return <FileText className="h-8 w-8" />
- }
- return <FileSpreadsheet className="h-8 w-8" />
- }
+ const getFileIcon = useCallback(() => {
+ return <FileText className="h-8 w-8" />
+ }, [])
return (
<Sheet open={open} onOpenChange={handleClose}>
<SheetContent
side="right"
className={cn(
- "flex flex-col h-full overflow-hidden", // full height, vertical layout
+ "flex flex-col h-full overflow-hidden py-2", // full height, vertical layout
analysisResult ? "sm:max-w-[85vw]" : "sm:max-w-2xl"
)}
>
{/* Header stays fixed */}
- <SheetHeader className="shrink-0 border-b bg-background px-6 py-4">
+ <SheetHeader className="shrink-0 border-b bg-background p-0 h-19 px-6 justify-center flex-col flex items-start">
<SheetTitle>Import entities</SheetTitle>
<SheetDescription>
- Upload a CSV, TXT, or XLSX file to import entities into your sketch
+ Upload a TXT file with one value per line to import entities into your sketch
</SheetDescription>
</SheetHeader>
{/* Optional beta banner */}
- <div className="px-6 shrink-0">
+ {/* <div className="px-6 shrink-0">
<div className="mt-3 rounded-md border border-primary bg-primary/20 px-3 py-2 text-xs text-primary">
This import feature is in beta. There may be minor side effects. If you see any issue, please{" "}
<a
@@ -127,7 +122,7 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
</a>{" "}
to help out the community.
</div>
- </div>
+ </div> */}
{/* Main scrollable zone */}
<div className="flex flex-col flex-grow overflow-hidden p-6">
@@ -153,7 +148,7 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
type="file"
id="file-upload"
className="hidden"
- accept=".csv,.txt,.xlsx,.xls"
+ accept=".txt"
onChange={handleFileInputChange}
/>
<Button asChild variant="outline">
@@ -162,7 +157,7 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
</label>
</Button>
<p className="text-xs text-muted-foreground">
- Supported formats: CSV, TXT, XLSX
+ Supported format: TXT (one value per line)
</p>
</div>
</div>
@@ -177,7 +172,7 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
{file && !isAnalyzing && !analysisResult && (
<div className="flex items-center gap-4 p-4 border rounded-lg">
- {getFileIcon(file.name)}
+ {getFileIcon()}
<div className="flex-1">
<p className="font-medium">{file.name}</p>
<p className="text-sm text-muted-foreground">
@@ -192,10 +187,9 @@ export function ImportSheet({ sketchId }: ImportSheetProps) {
{/* Scrollable Import Preview */}
{analysisResult && file && (
- <div className="flex flex-col flex-grow overflow-hidden">
+ <div className="flex flex-col flex-grow overflow-hidden">
<ImportPreview
analysisResult={analysisResult}
- file={file}
sketchId={sketchId}
onSuccess={handleClose}
onCancel={handleReset}
diff --git a/flowsint-app/src/components/table/relationships-view.tsx b/flowsint-app/src/components/table/relationships-view.tsx
index 597eb978..c909b8b2 100644
--- a/flowsint-app/src/components/table/relationships-view.tsx
+++ b/flowsint-app/src/components/table/relationships-view.tsx
@@ -56,7 +56,7 @@ function RelationshipItem({ relationship, style, onNodeClick, isSelected, onSele
return (
<div style={style} className="px-3 pb-2">
- <Card className="h-[55px] p-0">
+ <Card className="h-[55px] p-0 rounded-md">
<CardContent className="p-3 h-[55px] flex items-center gap-3 min-w-0">
{/* Checkbox */}
<div className="flex items-center flex-shrink-0">
@@ -398,7 +398,7 @@ export default function RelationshipsTable() {
)}
{/* Virtualized List */}
- <div ref={parentRef} className="grow overflow-auto py-4 rounded-lg border">
+ <div ref={parentRef} className="grow overflow-auto py-4 rounded border">
<div
style={{
height: `${virtualizer.getTotalSize()}px`,