-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathResourceTable.tsx
More file actions
149 lines (138 loc) · 4.19 KB
/
ResourceTable.tsx
File metadata and controls
149 lines (138 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as React from 'react';
import { useTranslation } from 'react-i18next';
import {
EmptyState,
EmptyStateBody,
Title,
Alert,
AlertVariant,
} from '@patternfly/react-core';
import { SearchIcon } from '@patternfly/react-icons';
interface Column {
title: string;
width?: number;
}
interface Row {
cells: React.ReactNode[];
}
interface ResourceTableProps {
columns: Column[];
rows: Row[];
loading?: boolean;
error?: string;
emptyStateTitle?: string;
emptyStateBody?: string;
/** When set, fallback empty state body is project-aware (e.g. "in project X" vs "in the demo project"). */
selectedProject?: string;
'data-test'?: string;
}
export const ResourceTable: React.FC<ResourceTableProps> = ({
columns,
rows,
loading = false,
error,
emptyStateTitle,
emptyStateBody,
selectedProject,
'data-test': dataTest,
}) => {
const { t } = useTranslation('plugin__ocp-secrets-management');
const defaultEmptyStateBody =
selectedProject && selectedProject !== 'all'
? t('No resources of this type are currently available in project {{project}}.', { project: selectedProject })
: t('No resources of this type are currently available in the demo project.');
if (loading) {
return (
<div className="co-m-loader co-an-fade-in-out" data-test={`${dataTest}-loading`}>
<div className="co-m-loader-dot__one"></div>
<div className="co-m-loader-dot__two"></div>
<div className="co-m-loader-dot__three"></div>
</div>
);
}
if (error) {
return (
<div className="co-m-pane__body" data-test={`${dataTest}-error`}>
<Alert
variant={AlertVariant.danger}
title={t('Error loading resources')}
isInline
>
{error}
</Alert>
</div>
);
}
if (rows.length === 0) {
return (
<div className="co-m-pane__body" data-test={`${dataTest}-empty`}>
<EmptyState>
<SearchIcon className="co-m-empty-state__icon" />
<Title size="lg" headingLevel="h4">
{emptyStateTitle || t('No resources found')}
</Title>
<EmptyStateBody>
{emptyStateBody ?? defaultEmptyStateBody}
</EmptyStateBody>
</EmptyState>
</div>
);
}
// Calculate column widths - distribute evenly if no widths specified
const totalSpecifiedWidth = columns.reduce((sum, col) => sum + (col.width || 0), 0);
const hasSpecifiedWidths = totalSpecifiedWidth > 0;
const defaultWidth = hasSpecifiedWidths ? undefined : 100 / columns.length;
return (
<div className="co-m-table-grid co-m-table-grid--bordered" data-test={dataTest}>
<div className="table-responsive">
<table className="table table-hover" style={{ tableLayout: 'fixed', width: '100%' }}>
<thead>
<tr>
{columns.map((column, index) => {
const width = hasSpecifiedWidths
? `${(column.width || 0)}%`
: `${defaultWidth}%`;
return (
<th
key={index}
role="columnheader"
style={{
width,
paddingLeft: '1rem',
paddingRight: '1rem',
textAlign: 'left',
verticalAlign: 'middle'
}}
>
{column.title}
</th>
);
})}
</tr>
</thead>
<tbody>
{rows.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.cells.map((cell, cellIndex) => (
<td
key={cellIndex}
style={{
paddingLeft: '1rem',
paddingRight: '1rem',
textAlign: 'left',
verticalAlign: 'middle',
wordWrap: 'break-word',
overflow: 'hidden'
}}
>
{cell}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
</div>
);
};