SuccessTable.tsx 8.24 KB
Newer Older
Lijiao's avatar
Lijiao committed
1
import * as React from 'react';
Lijiaoa's avatar
Lijiaoa committed
2
3
4
5
import {
    DetailsList,
    IDetailsListProps,
    IColumn,
6
7
    Icon,
    DetailsRow,
Lijiaoa's avatar
Lijiaoa committed
8
9
10
11
12
13
14
    IRenderFunction,
    IDetailsHeaderProps,
    Sticky,
    StickyPositionType,
    ScrollablePane,
    ScrollbarVisibility
} from '@fluentui/react';
15
import DefaultMetric from '../../public-child/DefaultMetric';
16
17
import OpenRow from '../../public-child/OpenRow';
import { convertDuration, copyAndSort } from '../../../static/function';
18
import { TRIALS } from '../../../static/datamodel';
19
import { SortInfo } from '../../../static/interface';
20
21
22
23
import { DETAILTABS } from '../../stateless-component/NNItabs';
import '../../../static/style/succTable.scss';
import '../../../static/style/tableStatus.css';
import '../../../static/style/openRow.scss';
Lijiao's avatar
Lijiao committed
24
25

interface SuccessTableProps {
26
    trialIds: string[];
27
28
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    updateOverviewPage: () => void;
Lijiao's avatar
Lijiao committed
29
30
}

31
32
33
interface SuccessTableState {
    columns: IColumn[];
    source: Array<any>;
34
35
    expandRowIdList: Set<string>;
    sortInfo: SortInfo;
36
}
Lijiao's avatar
Lijiao committed
37

38
class SuccessTable extends React.Component<SuccessTableProps, SuccessTableState> {
Lijiao's avatar
Lijiao committed
39
40
    constructor(props: SuccessTableProps) {
        super(props);
41
42
43
        this.state = {
            columns: this.columns,
            source: TRIALS.table(this.props.trialIds),
44
45
            sortInfo: { field: '', isDescend: false },
            expandRowIdList: new Set() // store expanded row's trial id
46
        };
Lijiao's avatar
Lijiao committed
47
48
    }

49
50
51
52
    componentDidUpdate(prevProps: SuccessTableProps): void {
        if (this.props.trialIds !== prevProps.trialIds) {
            const { trialIds } = this.props;
            this.setState(() => ({ source: TRIALS.table(trialIds) }));
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
    render(): React.ReactNode {
        const { columns, source, sortInfo } = this.state;
        const keepSortedSource = copyAndSort(source, sortInfo.field, sortInfo.isDescend);
        const isNoneData = source.length === 0 ? true : false;
        return (
            <div id='succTable'>
                <ScrollablePane className='scrollPanel' scrollbarVisibility={ScrollbarVisibility.auto}>
                    <DetailsList
                        columns={columns}
                        items={keepSortedSource}
                        setKey='set'
                        compact={true}
                        onRenderRow={this.onRenderRow}
                        onRenderDetailsHeader={this.onRenderDetailsHeader}
                        selectionMode={0} // close selector function
                        className='succTable'
                    />
                </ScrollablePane>
                {isNoneData && <div className='succTable-tooltip'>{this.tooltipStr}</div>}
            </div>
        );
    }

    private onColumnClick = (_ev: React.MouseEvent<HTMLElement>, getColumn: IColumn): void => {
80
81
82
83
84
85
86
87
88
89
        const { columns, source } = this.state;
        const newColumns: IColumn[] = columns.slice();
        const currColumn: IColumn = newColumns.filter(item => getColumn.key === item.key)[0];
        newColumns.forEach((newCol: IColumn) => {
            if (newCol === currColumn) {
                currColumn.isSortedDescending = !currColumn.isSortedDescending;
                currColumn.isSorted = true;
            } else {
                newCol.isSorted = false;
                newCol.isSortedDescending = true;
Lijiao's avatar
Lijiao committed
90
            }
91
92
        });
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
93
        const newItems = copyAndSort(source, currColumn.fieldName!, currColumn.isSortedDescending);
94
95
        this.setState({
            columns: newColumns,
96
97
98
            source: newItems,
            // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
            sortInfo: { field: currColumn.fieldName!, isDescend: currColumn.isSortedDescending }
99
100
101
        });
    };

102
    private tooltipStr = (
103
104
105
106
        <React.Fragment>
            The experiment is running, please wait for the final metric patiently. You could also find status of trial
            job with <span>{DETAILTABS}</span> button.
        </React.Fragment>
107
    );
108

109
    private columns: IColumn[] = [
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        {
            key: '_expand',
            name: '',
            onRender: (item: any): any => (
                <Icon
                    aria-hidden={true}
                    iconName='ChevronRight'
                    styles={{
                        root: {
                            transition: 'all 0.2s',
                            transform: `rotate(${this.state.expandRowIdList.has(item.id) ? 90 : 0}deg)`
                        }
                    }}
                    className='cursor'
                    onClick={this.expandTrialId.bind(this, Event, item.id)}
                />
            ),
            fieldName: 'expand',
            isResizable: false,
            minWidth: 20,
            maxWidth: 20
        },
132
133
134
135
        {
            name: 'Trial No.',
            key: 'sequenceId',
            fieldName: 'sequenceId', // required!
136
137
            minWidth: 60,
            maxWidth: 100,
138
            isResizable: true,
139
            data: 'number',
140
141
            onColumnClick: this.onColumnClick,
            onRender: (item: any): React.ReactNode => <div className='succeed-padding'>{item.sequenceId}</div>
142
143
        },
        {
144
145
146
            name: 'ID',
            key: 'id',
            fieldName: 'id',
147
            minWidth: 60,
148
            maxWidth: 90,
149
            isResizable: true,
150
151
            className: 'tableHead leftTitle',
            data: 'string',
152
153
            onColumnClick: this.onColumnClick,
            onRender: (item: any): React.ReactNode => <div className='succeed-padding'>{item.id}</div>
154
155
        },
        {
156
157
            name: 'Duration',
            key: 'duration',
158
159
            minWidth: 80,
            maxWidth: 120,
160
            isResizable: true,
161
162
163
            fieldName: 'duration',
            data: 'number',
            onColumnClick: this.onColumnClick,
164
            onRender: (item: any): React.ReactNode => (
165
                <div className='durationsty succeed-padding'>
166
167
168
169
170
                    <div>{convertDuration(item.duration)}</div>
                </div>
            )
        },
        {
171
172
            name: 'Status',
            key: 'status',
173
174
            minWidth: 88,
            maxWidth: 120,
175
            isResizable: true,
176
            fieldName: 'status',
177
178
179
            onRender: (item: any): React.ReactNode => (
                <div className={`${item.status} commonStyle succeed-padding`}>{item.status}</div>
            )
180
181
        },
        {
182
183
184
            name: 'Default metric',
            key: 'accuracy',
            fieldName: 'accuracy',
185
            minWidth: 100,
Lijiaoa's avatar
Lijiaoa committed
186
            maxWidth: 166,
187
            isResizable: true,
188
189
            data: 'number',
            onColumnClick: this.onColumnClick,
190
            onRender: (item: any): React.ReactNode => <DefaultMetric trialId={item.id} />
191
192
193
        }
    ];

194
    private onRenderDetailsHeader: IRenderFunction<IDetailsHeaderProps> = (props, defaultRender) => {
Lijiaoa's avatar
Lijiaoa committed
195
196
197
198
199
200
201
202
203
204
205
206
207
        if (!props) {
            return null;
        }
        return (
            <Sticky stickyPosition={StickyPositionType.Header} isScrollSynced>
                {// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                defaultRender!({
                    ...props
                })}
            </Sticky>
        );
    };

208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    private onRenderRow: IDetailsListProps['onRenderRow'] = props => {
        const { expandRowIdList } = this.state;
        if (props) {
            return (
                <div>
                    <div>
                        <DetailsRow {...props} />
                    </div>
                    {Array.from(expandRowIdList).map(
                        item => item === props.item.id && <OpenRow key={item} trialId={item} />
                    )}
                </div>
            );
        }
        return null;
223
224
    };

225
226
227
228
229
230
231
232
    private expandTrialId = (_event: any, id: string): void => {
        const { expandRowIdList } = this.state;
        const { updateOverviewPage } = this.props;
        const copyExpandList = expandRowIdList;
        if (copyExpandList.has(id)) {
            copyExpandList.delete(id);
        } else {
            copyExpandList.add(id);
233
        }
234
235
236
        this.setState(() => ({ expandRowIdList: copyExpandList }));
        updateOverviewPage();
    };
Lijiao's avatar
Lijiao committed
237
}
238
239

export default SuccessTable;