-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathfeedbackBoard.tsx
474 lines (410 loc) · 18.1 KB
/
feedbackBoard.tsx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import React from 'react';
import { WebApiTeam } from 'azure-devops-extension-api/Core';
import { WorkItem, WorkItemType } from 'azure-devops-extension-api/WorkItemTracking/WorkItemTracking';
import { workService } from '../dal/azureDevOpsWorkService';
import { workItemService } from '../dal/azureDevOpsWorkItemService';
import BoardDataService from '../dal/boardDataService';
import { itemDataService } from '../dal/itemDataService';
import { reflectBackendService } from '../dal/reflectBackendService';
import FeedbackColumn from './feedbackColumn';
import { IFeedbackBoardDocument, IFeedbackColumn, IFeedbackItemDocument } from '../interfaces/feedback';
import { ExceptionCode } from '../interfaces/retrospectiveState';
import { WorkflowPhase } from '../interfaces/workItem';
import FeedbackItemCarousel from './feedbackCarousel';
import { Dialog, DialogType } from 'office-ui-fabric-react/lib/Dialog';
import { withAITracking } from '@microsoft/applicationinsights-react-js';
import { reactPlugin } from '../utilities/telemetryClient';
export interface FeedbackBoardProps {
displayBoard: boolean;
exceptionCode?: ExceptionCode;
board: IFeedbackBoardDocument;
team: WebApiTeam;
workflowPhase: WorkflowPhase;
nonHiddenWorkItemTypes: WorkItemType[];
allWorkItemTypes: WorkItemType[];
isAnonymous: boolean;
hideFeedbackItems: boolean;
isCarouselDialogHidden: boolean;
hideCarouselDialog: () => void;
userId: string;
}
export interface IColumn {
columnProperties: IFeedbackColumn;
columnItems: IColumnItem[];
shouldFocusOnCreateFeedback?: boolean;
}
export interface IColumnItem {
feedbackItem: IFeedbackItemDocument;
actionItems: WorkItem[];
newlyCreated?: boolean;
showAddedAnimation?: boolean;
shouldHaveFocus?: boolean;
hideFeedbackItems?: boolean;
}
export interface FeedbackBoardState {
isDataLoaded: boolean;
columns: { [id: string]: IColumn };
columnIds: string[];
hasItems: boolean;
defaultActionItemIteration: string;
defaultActionItemAreaPath: string;
currentVoteCount: string;
}
class FeedbackBoard extends React.Component<FeedbackBoardProps, FeedbackBoardState> {
constructor(props: FeedbackBoardProps) {
super(props);
this.state = {
columnIds: [],
columns: {},
defaultActionItemAreaPath: '',
defaultActionItemIteration: '',
hasItems: false,
isDataLoaded: false,
currentVoteCount: props.board.boardVoteCollection?.[this.props.userId]?.toString() ?? "0",
};
}
public async componentDidMount() {
this.initColumns();
await this.getAllBoardFeedbackItems();
this.setDefaultIterationAndAreaPath(this.props.team.id);
// listen for signals for work item updates.
reflectBackendService.onReceiveNewItem(this.receiveNewItemHandler);
reflectBackendService.onReceiveUpdatedItem(this.receiveUpdatedItemHandler);
}
public async componentDidUpdate(prevProps: FeedbackBoardProps) {
if (prevProps.board.id !== this.props.board.id) {
this.setState({
isDataLoaded: false,
columns: {},
columnIds: [],
hasItems: false,
});
this.initColumns();
await this.getAllBoardFeedbackItems();
}
if (prevProps.board.modifiedDate !== this.props.board.modifiedDate) {
this.initColumns();
await this.getAllBoardFeedbackItems();
}
if (prevProps.team.id !== this.props.team.id) {
await this.setDefaultIterationAndAreaPath(this.props.team.id);
}
}
public async componentWillUnmount() {
// Remove event listeners.
reflectBackendService.removeOnReceiveNewItem(this.receiveNewItemHandler);
reflectBackendService.removeOnReceiveUpdatedItem(this.receiveUpdatedItemHandler);
}
private receiveNewItemHandler = async (columnId: string, feedbackItemId: string) => {
const newItem = await itemDataService.getFeedbackItem(this.props.board.id, feedbackItemId);
this.addFeedbackItems(
columnId,
[newItem],
/*shouldBroadcast*/ false,
/*newlyCreated*/ false,
/*showAddedAnimation*/ true,
/*shouldHaveFocus*/ false,
this.props.hideFeedbackItems);
}
private receiveUpdatedItemHandler = async (columnId: string, feedbackItemId: string) => {
const updatedItem = await itemDataService.getFeedbackItem(this.props.board.id, feedbackItemId);
this.refreshFeedbackItems([updatedItem], false);
}
private initColumns = () => {
const columnProperties = this.props.board.columns;
const stateColumns: { [id: string]: IColumn } = {};
const columnIds: string[] = new Array<string>();
columnProperties.map((col) => {
// If icon class is not present in the document, assign the smile and frown
// based on column id.
col.iconClass = col.iconClass
? col.iconClass
: col.id === BoardDataService.legacyPositiveColumnId
? 'far fa-smile'
: col.id === BoardDataService.legacyNegativeColumnId
? 'far fa-frown'
// Default icon to a chalkboard if there's any issue.
: 'fas fa-chalkboard';
col.accentColor = col.accentColor
? col.accentColor
: col.id === BoardDataService.legacyPositiveColumnId
? 'green'
: col.id === BoardDataService.legacyNegativeColumnId
? '#cc293d'
// Default accent color to DevOps blue if there's any issue.
: '#0078d4';
const column: IColumn = {
columnProperties: col,
columnItems: [],
shouldFocusOnCreateFeedback: false,
};
stateColumns[col.id] = column;
columnIds.push(col.id);
});
this.setState({ columns: stateColumns, columnIds: columnIds });
}
// TODO: This throws a 404 if board does not have any items. This case and any such cases should be explicitly handled.
private getAllBoardFeedbackItems = async () => {
const feedbackItems = await itemDataService.getFeedbackItemsForBoard(this.props.board.id);
if (!feedbackItems) {
this.setState({ isDataLoaded: true });
return;
}
const columnItemPromises: Promise<IColumnItem>[] = feedbackItems.map(async (feedbackItem) => {
const actionItems = feedbackItem.associatedActionItemIds && feedbackItem.associatedActionItemIds.length ? await workItemService.getWorkItemsByIds(feedbackItem.associatedActionItemIds) : [];
return {
actionItems,
feedbackItem,
};
});
const columnItems = await Promise.all(columnItemPromises);
this.setState((prevState) => {
columnItems.forEach((columnItem) => {
// Some columns might have been deleted. Only add items to columns that still exist.
if (this.state.columnIds.indexOf(columnItem.feedbackItem.columnId) >= 0) {
prevState.columns[columnItem.feedbackItem.columnId].columnItems.push(columnItem);
}
});
return {
columns: prevState.columns,
hasItems: true,
isDataLoaded: true,
};
});
}
private setDefaultIterationAndAreaPath = async (teamId: string): Promise<void> => {
let currentIterations = await workService.getIterations(teamId, 'current');
if (!currentIterations || !currentIterations.length) {
// If no iterations cover the present timeframe, we simply choose an arbitrary iteration as default.
currentIterations = await workService.getIterations(teamId);
}
const defaultIteration = (currentIterations && currentIterations.length) ? currentIterations[0].path : '';
const teamFieldValues = await workService.getTeamFieldValues(teamId);
const defaultAreaPath = (teamFieldValues && teamFieldValues.values && teamFieldValues.values.length) ? teamFieldValues.values[0].value : '';
this.setState({ defaultActionItemAreaPath: defaultAreaPath, defaultActionItemIteration: defaultIteration });
}
private getColumnsWithReleasedFocus = (currentFeedbackBoardState: FeedbackBoardState) => {
const resetFocusForStateColumns = { ...currentFeedbackBoardState.columns };
for (const columnIdKey in currentFeedbackBoardState.columns) {
if (resetFocusForStateColumns[columnIdKey].shouldFocusOnCreateFeedback) {
resetFocusForStateColumns[columnIdKey].shouldFocusOnCreateFeedback = false;
}
const resetColumnItems = currentFeedbackBoardState.columns[columnIdKey].columnItems.map(columnItem => {
return { ...columnItem, shouldHaveFocus: false };
});
resetFocusForStateColumns[columnIdKey].columnItems = resetColumnItems;
}
return resetFocusForStateColumns;
}
private addFeedbackItems = (
columnId: string, feedbackItems: IFeedbackItemDocument[],
shouldBroadcast: boolean, newlyCreated: boolean, showAddedAnimation: boolean,
shouldHaveFocus: boolean, hideFeedbackItems: boolean) => {
this.setState((previousState) => {
const firstAddedItemId = feedbackItems.length && feedbackItems[0].id;
const resetFocusForStateColumns = this.getColumnsWithReleasedFocus(previousState);
const updatedColumnItems = feedbackItems.map(
(feedbackItem): IColumnItem => {
if (feedbackItem.id === firstAddedItemId) {
return {
actionItems: [],
feedbackItem,
hideFeedbackItems,
newlyCreated,
shouldHaveFocus,
showAddedAnimation,
};
}
return {
actionItems: [],
feedbackItem,
hideFeedbackItems,
newlyCreated,
showAddedAnimation,
};
},
).concat(resetFocusForStateColumns[columnId].columnItems);
const newColumns = { ...resetFocusForStateColumns };
newColumns[columnId].columnItems = updatedColumnItems;
return {
columns: newColumns,
isDataLoaded: true,
};
});
if (shouldBroadcast) {
feedbackItems.forEach((columnItem) => {
reflectBackendService.broadcastNewItem(
columnId,
columnItem.id,
);
});
}
}
private removeFeedbackItemFromColumn = (columnId: string, feedbackItemId: string, shouldSetFocusOnFirstAvailableItem: boolean) => {
this.setState((previousState: FeedbackBoardState) => {
const removedItemIndex: number = previousState.columns[columnId].columnItems.findIndex((columnItem) => columnItem.feedbackItem.id === feedbackItemId);
const updatedColumnItems = previousState.columns[columnId].columnItems.filter((columnItem) => {
return columnItem.feedbackItem.id !== feedbackItemId;
});
let updatedColumnItemsWithActiveFocus = updatedColumnItems;
let shouldFocusOnCreateFeedback: boolean = false;
if (shouldSetFocusOnFirstAvailableItem) {
if (updatedColumnItems.length > 0 && updatedColumnItems[0]) {
updatedColumnItemsWithActiveFocus = updatedColumnItems.map((columnItem): IColumnItem => {
return { ...columnItem, shouldHaveFocus: false };
});
const nextAvailableItemIndex = removedItemIndex >= updatedColumnItemsWithActiveFocus.length ? 0 : removedItemIndex;
updatedColumnItemsWithActiveFocus[nextAvailableItemIndex] = { ...updatedColumnItemsWithActiveFocus[nextAvailableItemIndex], shouldHaveFocus: true };
}
else {
// If no items in colummn, set focus to column's create feedback button
shouldFocusOnCreateFeedback = true;
}
}
const resetFocusForStateColumns = this.getColumnsWithReleasedFocus(previousState);
return {
columns: {
...resetFocusForStateColumns,
[columnId]: {
...resetFocusForStateColumns[columnId],
columnItems: updatedColumnItemsWithActiveFocus,
shouldFocusOnCreateFeedback,
}
}
};
});
}
private refreshFeedbackItems = async (updatedFeedbackItems: IFeedbackItemDocument[], shouldBroadcast: boolean): Promise<void> => {
if (updatedFeedbackItems.length) {
const updatedColumnItems: IColumnItem[] = await Promise.all(updatedFeedbackItems.map(async (feedbackItem) => {
// TODO: Optimize performance by only updating work items in action-item-related update scenario.
const actionItems = feedbackItem.associatedActionItemIds && feedbackItem.associatedActionItemIds.length ?
await workItemService.getWorkItemsByIds(feedbackItem.associatedActionItemIds) : [];
return {
feedbackItem,
actionItems,
};
}));
this.setState((previousState) => {
const newColumnsAsList = previousState.columnIds.map((columnId) => {
return {
key: columnId,
value: {
columnProperties: previousState.columns[columnId].columnProperties,
// Update the new column items to contain
// 1) The existing items that have not been moved. (filter)
// - Note that we use the updated version of these items if they are present in updatedFeedbackItems. (map)
// 2) The new items for this column. (concat)
columnItems: previousState.columns[columnId].columnItems
.filter((columnItem) => {
return !updatedColumnItems.some((item) => (item.feedbackItem.id === columnItem.feedbackItem.id && item.feedbackItem.columnId !== columnItem.feedbackItem.columnId));
})
.map((columnItem) => {
const updatedItem = updatedColumnItems.find((item) => (item.feedbackItem.id === columnItem.feedbackItem.id && item.feedbackItem.columnId === columnItem.feedbackItem.columnId));
return updatedItem || columnItem;
})
.concat(updatedColumnItems.filter((columnItem) => {
return columnItem.feedbackItem.columnId === columnId &&
!previousState.columns[columnId].columnItems.some((existingColumnItem) => columnItem.feedbackItem.id === existingColumnItem.feedbackItem.id);
})),
}
};
});
const emptyColumns: { [id: string]: IColumn } = {};
const newColumns = newColumnsAsList.reduce(
(columns, columnsAsList) => {
columns[columnsAsList.key] = columnsAsList.value;
return columns;
},
emptyColumns);
return {
columns: newColumns
};
});
}
if (shouldBroadcast) {
updatedFeedbackItems.forEach(updatedFeedbackItem => {
reflectBackendService.broadcastUpdatedItem('dummyColumn', updatedFeedbackItem.id);
});
}
}
public render() {
if (!this.props.displayBoard) {
return (<div> An unexpected exception occurred. </div>);
}
const feedbackColumnPropsList = this.state.columnIds.map((columnId) => {
return {
key: columnId,
columns: this.state.columns,
columnIds: this.state.columnIds,
columnName: this.state.columns[columnId].columnProperties.title,
columnId: columnId,
columnItems: this.state.columns[columnId].columnItems,
accentColor: this.state.columns[columnId].columnProperties.accentColor,
team: this.props.team,
boardId: this.props.board.id,
boardTitle: this.props.board.title,
isDataLoaded: this.state.isDataLoaded,
iconClass: this.state.columns[columnId].columnProperties.iconClass,
workflowPhase: this.props.workflowPhase,
addFeedbackItems: this.addFeedbackItems,
removeFeedbackItemFromColumn: this.removeFeedbackItemFromColumn,
refreshFeedbackItems: this.refreshFeedbackItems,
defaultActionItemAreaPath: this.state.defaultActionItemAreaPath,
defaultActionItemIteration: this.state.defaultActionItemIteration,
nonHiddenWorkItemTypes: this.props.nonHiddenWorkItemTypes,
allWorkItemTypes: this.props.allWorkItemTypes,
isBoardAnonymous: this.props.isAnonymous,
shouldFocusOnCreateFeedback: this.state.columns[columnId].shouldFocusOnCreateFeedback ? true : false,
hideFeedbackItems: this.props.hideFeedbackItems,
isFocusModalHidden: true,
onVoteCasted: () => {
itemDataService.getBoardItem(this.props.team.id, this.props.board.id).then((boardItem: IFeedbackBoardDocument) => {
const voteCollection = boardItem.boardVoteCollection;
this.setState({ currentVoteCount: voteCollection?.[this.props.userId]?.toString() ?? "0"});
});
},
groupTitles: []
};
});
const columnItems = this.state.columnIds
.flatMap((columnId) => this.state.columns[columnId].columnItems);
const participants = new Set(columnItems.flatMap((columnItem) => Object.keys(columnItem.feedbackItem.voteCollection))).size;
const teamVotes = columnItems
.flatMap((columnItem) => Object.values(columnItem.feedbackItem.voteCollection))
.reduce((a, b) => a + b, 0)
.toString();
const maxTeamVotes = (participants * this.props.board?.maxVotesPerUser).toString();
return (
<div className="feedback-board">
{this.props.workflowPhase === WorkflowPhase.Vote &&
<div className="feedback-maxvotes-per-user">
<label>Votes Used: {this.state.currentVoteCount} / {this.props.board?.maxVotesPerUser?.toString()} (me), {teamVotes} / {maxTeamVotes} (team)</label>
</div>
}
<div className="feedback-columns-container">
{this.state.isDataLoaded && feedbackColumnPropsList.map((columnProps) => { return (<FeedbackColumn {...columnProps} />); })}
</div>
<Dialog
hidden={this.props.isCarouselDialogHidden}
onDismiss={this.props.hideCarouselDialog}
minWidth={900}
dialogContentProps={{
type: DialogType.close,
title: 'Focus Mode',
subText: 'Now is the time to focus! Discuss one feedback item at a time and create actionable work items',
}}
modalProps={{
containerClassName: 'retrospectives-carousel-dialog',
className: 'retrospectives-carousel-dialog-modal hide-mobile',
isBlocking: true
}}>
<FeedbackItemCarousel
feedbackColumnPropsList={feedbackColumnPropsList} isFeedbackAnonymous={this.props.isAnonymous}
isFocusModalHidden={this.props.isCarouselDialogHidden}
/>
</Dialog>
</div>);
}
}
export default withAITracking(reactPlugin, FeedbackBoard);