1
+ // config/data-sources.config.tsx
2
+ import React from 'react' ;
3
+ import { Row , Col , Statistic , Tag , Space , Button , Tooltip } from 'antd' ;
4
+ import { DatabaseOutlined , CloudUploadOutlined } from '@ant-design/icons' ;
5
+ import { DeployableItemConfig } from '../types/deployable-item.types' ;
6
+ import { DataSource , DataSourceStats } from '../types/datasource.types' ;
7
+ import { Environment } from '../types/environment.types' ;
8
+ import { getMergedWorkspaceDataSources } from '../services/datasources.service' ;
9
+ import { connectManagedDataSource , unconnectManagedDataSource } from '../services/enterprise.service' ;
10
+
11
+
12
+
13
+ export const dataSourcesConfig : DeployableItemConfig < DataSource , DataSourceStats > = {
14
+ // Basic info
15
+ type : 'dataSources' ,
16
+ singularLabel : 'Data Source' ,
17
+ pluralLabel : 'Data Sources' ,
18
+ icon : < DatabaseOutlined /> ,
19
+ idField : 'id' ,
20
+
21
+ // Navigation
22
+ buildDetailRoute : ( params ) => "#" ,
23
+
24
+ // Configuration
25
+ requiredEnvProps : [ 'environmentApikey' , 'environmentApiServiceUrl' ] ,
26
+
27
+ // Stats rendering
28
+ renderStats : ( stats ) => (
29
+ < Row gutter = { 16 } >
30
+ < Col span = { 8 } >
31
+ < Statistic title = "Total Data Sources" value = { stats . total } prefix = { < DatabaseOutlined /> } />
32
+ </ Col >
33
+ < Col span = { 8 } >
34
+ < Statistic title = "Managed Data Sources" value = { stats . managed } prefix = { < DatabaseOutlined /> } />
35
+ </ Col >
36
+ < Col span = { 8 } >
37
+ < Statistic title = "Unmanaged Data Sources" value = { stats . unmanaged } prefix = { < DatabaseOutlined /> } />
38
+ </ Col >
39
+ </ Row >
40
+ ) ,
41
+
42
+ // Stats calculation
43
+ calculateStats : ( dataSources ) => {
44
+ const total = dataSources . length ;
45
+ const managed = dataSources . filter ( ds => ds . managed ) . length ;
46
+
47
+ // Calculate counts by type
48
+ const byType = dataSources . reduce ( ( acc , ds ) => {
49
+ const type = ds . type || 'Unknown' ;
50
+ acc [ type ] = ( acc [ type ] || 0 ) + 1 ;
51
+ return acc ;
52
+ } , { } as Record < string , number > ) ;
53
+
54
+ return {
55
+ total,
56
+ managed,
57
+ unmanaged : total - managed ,
58
+ byType
59
+ } ;
60
+ } ,
61
+
62
+ // Table configuration - Customize based on your existing UI
63
+ columns : [
64
+ {
65
+ title : 'Name' ,
66
+ dataIndex : 'name' ,
67
+ key : 'name' ,
68
+ } ,
69
+ {
70
+ title : 'Type' ,
71
+ dataIndex : 'type' ,
72
+ key : 'type' ,
73
+ render : ( type : string ) => (
74
+ < Tag color = "blue" > { type || 'Unknown' } </ Tag >
75
+ ) ,
76
+ } ,
77
+ {
78
+ title : 'Database' ,
79
+ key : 'database' ,
80
+ render : ( _ , record : DataSource ) => (
81
+ < span > { record . datasourceConfig ?. database || 'N/A' } </ span >
82
+ ) ,
83
+ } ,
84
+ {
85
+ title : 'Status' ,
86
+ dataIndex : 'datasourceStatus' ,
87
+ key : 'status' ,
88
+ render : ( status : string ) => (
89
+ < Tag color = { status === 'NORMAL' ? 'green' : 'red' } >
90
+ { status }
91
+ </ Tag >
92
+ ) ,
93
+ } ,
94
+ {
95
+ title : 'Actions' ,
96
+ key : 'actions' ,
97
+ render : ( _ , record : DataSource ) => (
98
+ < Space >
99
+ < Tooltip title = "Deploy to another environment" >
100
+ < Button
101
+ icon = { < CloudUploadOutlined /> }
102
+ onClick = { ( e ) => {
103
+ e . stopPropagation ( ) ; // Prevent row click navigation
104
+ } }
105
+ type = "primary"
106
+ ghost
107
+ >
108
+ Deploy
109
+ </ Button >
110
+ </ Tooltip >
111
+ </ Space >
112
+ ) ,
113
+ }
114
+ ] ,
115
+
116
+ // Deployment options
117
+ enableManaged : true ,
118
+
119
+ // Service functions
120
+ fetchItems : async ( { environment, workspaceId } ) => {
121
+ if ( ! workspaceId ) {
122
+ throw new Error ( "Workspace ID is required to fetch data sources" ) ;
123
+ }
124
+
125
+ const result = await getMergedWorkspaceDataSources (
126
+ workspaceId ,
127
+ environment . environmentId ,
128
+ environment . environmentApikey ,
129
+ environment . environmentApiServiceUrl !
130
+ ) ;
131
+
132
+ return result . dataSources ;
133
+ } ,
134
+
135
+ toggleManaged : async ( { item, checked, environment } ) => {
136
+ try {
137
+ if ( checked ) {
138
+ await connectManagedDataSource ( environment . environmentId , item . name , item . gid ) ;
139
+ } else {
140
+ await unconnectManagedDataSource ( item . gid ) ;
141
+ }
142
+ return true ;
143
+ } catch ( error ) {
144
+ console . error ( 'Error toggling managed status:' , error ) ;
145
+ return false ;
146
+ }
147
+ }
148
+ } ;
0 commit comments