11import { faDownload , faPlus , faUpload } from '@fortawesome/free-solid-svg-icons'
22import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
33import { getRandomString , joinObjectPathFragments , literal , objectPathGet } from '@sofie-automation/corelib/dist/lib'
4- import React , { useCallback , useMemo , useState } from 'react'
4+ import React , { useCallback , useMemo } from 'react'
55import { useTranslation } from 'react-i18next'
66import {
77 WrappedOverridableItemNormal ,
@@ -30,6 +30,8 @@ import { ObjectTableDeletedRow } from './ObjectTableDeletedRow'
3030import { SchemaFormSectionHeader } from '../SchemaFormSectionHeader'
3131import { UploadButton } from '../../uploadButton'
3232import Tooltip from 'rc-tooltip'
33+ import { NoticeLevel , Notification , NotificationCenter } from '../../notifications/notifications'
34+ import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
3335
3436interface SchemaFormObjectTableProps {
3537 /** Schema for each row in the table */
@@ -293,8 +295,6 @@ interface ImportExportButtonsProps {
293295function ImportExportButtons ( { schema, overrideHelper, wrappedRows } : Readonly < ImportExportButtonsProps > ) {
294296 const { t } = useTranslation ( )
295297
296- const [ uploadFileKey , setUploadFileKey ] = useState ( 0 )
297-
298298 const exportTable = ( ) => {
299299 const exportObject : Record < string , any > = { }
300300 for ( const obj of wrappedRows ) {
@@ -319,73 +319,73 @@ function ImportExportButtons({ schema, overrideHelper, wrappedRows }: Readonly<I
319319 URL . revokeObjectURL ( url )
320320 }
321321
322- const importTable = ( e : React . ChangeEvent < HTMLInputElement > ) => {
323- const file = e . target . files ?. [ 0 ]
324- if ( ! file ) return
325-
326- const reader = new FileReader ( )
327- reader . onload = ( e2 ) => {
328- // On file upload
329- setUploadFileKey ( Date . now ( ) )
330-
331- const uploadFileContents = e2 . target ?. result
332- if ( ! uploadFileContents ) return
333-
334- const newRows = JSON . parse ( uploadFileContents as string )
335- if ( ! newRows || typeof newRows !== 'object' ) return
322+ const importTable = ( uploadFileContents : string , file : File ) => {
323+ const newRows = JSON . parse ( uploadFileContents )
324+ if ( ! newRows || typeof newRows !== 'object' ) return
325+
326+ doModalDialog ( {
327+ title : t ( 'Import file?' ) ,
328+ yes : t ( 'Replace rows' ) ,
329+ no : t ( 'Cancel' ) ,
330+ message : (
331+ < p >
332+ { t ( 'Are you sure you want to import the contents of the file "{{fileName}}"?' , {
333+ fileName : file . name ,
334+ } ) }
335+ </ p >
336+ ) ,
337+ onAccept : ( ) => {
338+ const batch = overrideHelper ( )
339+
340+ for ( const row of wrappedRows ) {
341+ batch . deleteRow ( row . id )
342+ }
336343
337- doModalDialog ( {
338- title : t ( 'Import file?' ) ,
339- yes : t ( 'Replace rows' ) ,
340- no : t ( 'Cancel' ) ,
341- message : (
342- < p >
343- { t ( 'Are you sure you want to import the contents of the file "{{fileName}}"?' , {
344- fileName : file . name ,
345- } ) }
346- </ p >
347- ) ,
348- onAccept : ( ) => {
349- const batch = overrideHelper ( )
344+ for ( const [ rowId , row ] of Object . entries < unknown > ( newRows ) ) {
345+ batch . insertRow ( rowId , row )
346+ }
350347
351- for ( const row of wrappedRows ) {
352- batch . deleteRow ( row . id )
353- }
348+ batch . commit ( )
349+ } ,
350+ actions : [
351+ {
352+ label : t ( 'Append rows' ) ,
353+ on : ( ) => {
354+ const batch = overrideHelper ( )
354355
355- for ( const [ rowId , row ] of Object . entries < unknown > ( newRows ) ) {
356- batch . insertRow ( rowId , row )
357- }
356+ for ( const [ rowId , row ] of Object . entries < unknown > ( newRows ) ) {
357+ batch . insertRow ( rowId , row )
358+ }
358359
359- batch . commit ( )
360- } ,
361- actions : [
362- {
363- label : t ( 'Append rows' ) ,
364- on : ( ) => {
365- const batch = overrideHelper ( )
366-
367- for ( const [ rowId , row ] of Object . entries < unknown > ( newRows ) ) {
368- batch . insertRow ( rowId , row )
369- }
370-
371- batch . commit ( )
372- } ,
373- classNames : 'btn-secondary' ,
360+ batch . commit ( )
374361 } ,
375- ] ,
376- } )
377- }
378- reader . readAsText ( file )
362+ classNames : 'btn-secondary' ,
363+ } ,
364+ ] ,
365+ } )
379366 }
367+ const importError = useCallback (
368+ ( err : Error ) => {
369+ NotificationCenter . push (
370+ new Notification (
371+ undefined ,
372+ NoticeLevel . WARNING ,
373+ t ( 'Import error: {{errorMessage}}' , { errorMessage : stringifyError ( err ) } ) ,
374+ 'ConfigManifestSettings'
375+ )
376+ )
377+ } ,
378+ [ t ]
379+ )
380380
381381 return (
382382 < >
383383 < Tooltip overlay = { t ( 'Import' ) } placement = "top" >
384384 < span className = "inline-block" >
385385 < UploadButton
386- key = { uploadFileKey }
387386 className = "btn btn-secondary"
388- onChange = { importTable }
387+ onUploadContents = { importTable }
388+ onUploadError = { importError }
389389 accept = "application/json,.json"
390390 >
391391 < FontAwesomeIcon icon = { faUpload } />
0 commit comments