1
1
import React , { useMemo , useState } from 'react' ;
2
- import { ACTION } from '@/utils' ;
3
- import buildCard from './build-card' ;
4
2
import styled from 'styled-components' ;
3
+ import { safeJsonParse } from '@/utils' ;
5
4
import { useDrawerStore } from '@/store' ;
6
5
import { CardDetails } from '@/components' ;
7
- import buildDrawerItem from './build-drawer-item' ;
8
- import { ConditionDetails } from '@/reuseable-components' ;
6
+ import type { ActualDestination } from '@/types' ;
9
7
import OverviewDrawer from '../../overview/overview-drawer' ;
10
8
import { DestinationFormBody } from '../destination-form-body' ;
11
- import { OVERVIEW_ENTITY_TYPES , type ActualDestination } from '@/types' ;
12
9
import { useDestinationCRUD , useDestinationFormData , useDestinationTypes } from '@/hooks' ;
13
10
14
11
interface Props { }
@@ -21,94 +18,96 @@ const FormContainer = styled.div`
21
18
overflow-y: auto;
22
19
` ;
23
20
24
- const DataContainer = styled . div `
25
- display: flex;
26
- flex-direction: column;
27
- gap: 12px;
28
- ` ;
29
-
30
21
export const DestinationDrawer : React . FC < Props > = ( ) => {
31
- const { selectedItem, setSelectedItem } = useDrawerStore ( ) ;
32
- const { destinations : destinationTypes } = useDestinationTypes ( ) ;
22
+ const selectedItem = useDrawerStore ( ( { selectedItem } ) => selectedItem ) ;
23
+ const [ isEditing , setIsEditing ] = useState ( false ) ;
24
+ const [ isFormDirty , setIsFormDirty ] = useState ( false ) ;
33
25
26
+ const { updateDestination, deleteDestination } = useDestinationCRUD ( ) ;
34
27
const { formData, handleFormChange, resetFormData, validateForm, loadFormWithDrawerItem, destinationTypeDetails, dynamicFields, setDynamicFields } = useDestinationFormData ( {
35
28
destinationType : ( selectedItem ?. item as ActualDestination ) ?. destinationType ?. type ,
36
29
preLoadedFields : ( selectedItem ?. item as ActualDestination ) ?. fields ,
37
30
// TODO: supportedSignals: thisDestination?.supportedSignals,
38
31
// currently, the real "supportedSignals" is being used by "destination" passed as prop to "DestinationFormBody"
39
32
} ) ;
40
33
41
- const { updateDestination, deleteDestination } = useDestinationCRUD ( {
42
- onSuccess : ( type ) => {
43
- setIsEditing ( false ) ;
44
- setIsFormDirty ( false ) ;
45
-
46
- if ( type === ACTION . DELETE ) {
47
- setSelectedItem ( null ) ;
48
- } else {
49
- const { item } = selectedItem as { item : ActualDestination } ;
50
- const { id } = item ;
51
- setSelectedItem ( { id, type : OVERVIEW_ENTITY_TYPES . DESTINATION , item : buildDrawerItem ( id , formData , item ) } ) ;
52
- }
53
- } ,
54
- } ) ;
34
+ const cardData = useMemo ( ( ) => {
35
+ if ( ! selectedItem ) return [ ] ;
55
36
56
- const [ isEditing , setIsEditing ] = useState ( false ) ;
57
- const [ isFormDirty , setIsFormDirty ] = useState ( false ) ;
37
+ const buildMonitorsList = ( exportedSignals : ActualDestination [ 'exportedSignals' ] ) : string =>
38
+ Object . keys ( exportedSignals )
39
+ . filter ( ( key ) => exportedSignals [ key ] )
40
+ . join ( ', ' ) || 'N/A' ;
58
41
59
- const cardData = useMemo ( ( ) => {
60
- if ( ! selectedItem || ! destinationTypeDetails ) return [ ] ;
42
+ const buildDestinationFieldData = ( parsedFields : Record < string , string > ) =>
43
+ Object . entries ( parsedFields ) . map ( ( [ key , value ] ) => {
44
+ const found = destinationTypeDetails ?. fields ?. find ( ( field ) => field . name === key ) ;
61
45
62
- const { item } = selectedItem as { item : ActualDestination } ;
63
- const arr = buildCard ( item , destinationTypeDetails ) ;
46
+ const { type } = safeJsonParse ( found ?. componentProperties , { type : '' } ) ;
47
+ const secret = type === 'password' ? new Array ( value . length ) . fill ( '•' ) . join ( '' ) : '' ;
48
+
49
+ return {
50
+ title : found ?. displayName || key ,
51
+ value : secret || value || 'N/A' ,
52
+ } ;
53
+ } ) ;
64
54
65
- return arr ;
55
+ const { exportedSignals, destinationType, fields } = selectedItem . item as ActualDestination ;
56
+ const parsedFields = safeJsonParse < Record < string , string > > ( fields , { } ) ;
57
+ const fieldsData = buildDestinationFieldData ( parsedFields ) ;
58
+
59
+ return [ { title : 'Destination' , value : destinationType . displayName || 'N/A' } , { title : 'Monitors' , value : buildMonitorsList ( exportedSignals ) } , ...fieldsData ] ;
66
60
} , [ selectedItem , destinationTypeDetails ] ) ;
67
61
62
+ const { destinations } = useDestinationTypes ( ) ;
68
63
const thisDestination = useMemo ( ( ) => {
69
- if ( ! destinationTypes . length || ! selectedItem || ! isEditing ) {
64
+ if ( ! destinations . length || ! selectedItem || ! isEditing ) {
70
65
resetFormData ( ) ;
71
66
return undefined ;
72
67
}
73
68
74
69
const { item } = selectedItem as { item : ActualDestination } ;
75
- const found = destinationTypes . map ( ( { items } ) => items . filter ( ( { type } ) => type === item . destinationType . type ) ) . filter ( ( arr ) => ! ! arr . length ) [ 0 ] [ 0 ] ;
70
+ const found = destinations . map ( ( { items } ) => items . filter ( ( { type } ) => type === item . destinationType . type ) ) . filter ( ( arr ) => ! ! arr . length ) [ 0 ] [ 0 ] ;
76
71
77
72
if ( ! found ) return undefined ;
78
73
79
74
loadFormWithDrawerItem ( selectedItem ) ;
80
75
81
76
return found ;
82
- } , [ destinationTypes , selectedItem , isEditing ] ) ;
77
+ } , [ destinations , selectedItem , isEditing ] ) ;
83
78
84
79
if ( ! selectedItem ?. item ) return null ;
85
- const { id, item } = selectedItem as { id : string ; item : ActualDestination } ;
80
+ const { id, item } = selectedItem ;
86
81
87
82
const handleEdit = ( bool ?: boolean ) => {
88
- setIsEditing ( typeof bool === 'boolean' ? bool : true ) ;
83
+ if ( typeof bool === 'boolean' ) {
84
+ setIsEditing ( bool ) ;
85
+ } else {
86
+ setIsEditing ( true ) ;
87
+ }
89
88
} ;
90
89
91
90
const handleCancel = ( ) => {
91
+ resetFormData ( ) ;
92
92
setIsEditing ( false ) ;
93
- setIsFormDirty ( false ) ;
94
93
} ;
95
94
96
95
const handleDelete = async ( ) => {
97
- await deleteDestination ( id ) ;
96
+ await deleteDestination ( id as string ) ;
98
97
} ;
99
98
100
99
const handleSave = async ( newTitle : string ) => {
101
100
if ( validateForm ( { withAlert : true } ) ) {
102
- const title = newTitle !== item . destinationType . displayName ? newTitle : '' ;
103
- handleFormChange ( 'name' , title ) ;
104
- await updateDestination ( id , { ...formData , name : title } ) ;
101
+ const title = newTitle !== ( item as ActualDestination ) . destinationType . displayName ? newTitle : '' ;
102
+
103
+ await updateDestination ( id as string , { ...formData , name : title } ) ;
105
104
}
106
105
} ;
107
106
108
107
return (
109
108
< OverviewDrawer
110
- title = { item . name || item . destinationType . displayName }
111
- imageUri = { item . destinationType . imageUrl }
109
+ title = { ( item as ActualDestination ) . name || ( item as ActualDestination ) . destinationType . displayName }
110
+ imageUri = { ( item as ActualDestination ) . destinationType . imageUrl }
112
111
isEdit = { isEditing }
113
112
isFormDirty = { isFormDirty }
114
113
onEdit = { handleEdit }
@@ -135,10 +134,7 @@ export const DestinationDrawer: React.FC<Props> = () => {
135
134
/>
136
135
</ FormContainer >
137
136
) : (
138
- < DataContainer >
139
- < ConditionDetails conditions = { item . conditions } />
140
- < CardDetails data = { cardData } />
141
- </ DataContainer >
137
+ < CardDetails title = 'Destination Details' data = { cardData } />
142
138
) }
143
139
</ OverviewDrawer >
144
140
) ;
0 commit comments