-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathListOfPublishedProfiles.tsx
More file actions
273 lines (241 loc) · 8.43 KB
/
ListOfPublishedProfiles.tsx
File metadata and controls
273 lines (241 loc) · 8.43 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import { Localized } from '@fluent/react';
import { InnerNavigationLink } from 'firefox-profiler/components/shared/InnerNavigationLink';
import { ProfileMetaInfoSummary } from 'firefox-profiler/components/shared/ProfileMetaInfoSummary';
import { ProfileDeleteButton } from './ProfileDeleteButton';
import {
listAllUploadedProfileInformationFromDb,
type UploadedProfileInformation,
} from 'firefox-profiler/app-logic/uploaded-profiles-db';
import { formatSeconds } from 'firefox-profiler/utils/format-numbers';
import type { StartEndRange } from 'firefox-profiler/types/units';
import './ListOfPublishedProfiles.css';
// This component displays all published profile, and makes it possible to load
// them by clicking on them, or delete them.
function _formatRange(range: StartEndRange): string {
if (!Number.isFinite(range.start) || !Number.isFinite(range.end)) {
// Do not attempt to show if the range is NaN or Infinity, which happens
// if a profile doesn't have any samples or markers.
return '';
}
return formatSeconds(range.end - range.start, 3, 1);
}
type PublishedProfileProps = {
readonly onProfileDelete: () => void;
readonly uploadedProfileInformation: UploadedProfileInformation;
readonly withActionButtons: boolean;
};
type PublishedProfileState = {
readonly confirmDialogIsOpen: boolean;
};
/**
* This implements one line in the list of published profiles.
*/
class PublishedProfile extends React.PureComponent<
PublishedProfileProps,
PublishedProfileState
> {
override state: PublishedProfileState = {
confirmDialogIsOpen: false,
};
onOpenConfirmDialog = () => {
this.setState({ confirmDialogIsOpen: true });
};
onCloseConfirmDialog = () => {
this.setState({ confirmDialogIsOpen: false });
};
onCloseSuccessMessage = () => {
this.props.onProfileDelete();
};
override render() {
const { uploadedProfileInformation, withActionButtons } = this.props;
const { confirmDialogIsOpen } = this.state;
let { urlPath } = uploadedProfileInformation;
if (!urlPath.startsWith('/')) {
urlPath = '/' + urlPath;
}
const slicedProfileToken = uploadedProfileInformation.profileToken.slice(
0,
6
);
const profileName = uploadedProfileInformation.name
? uploadedProfileInformation.name
: `Profile #${slicedProfileToken}`;
const smallProfileName = uploadedProfileInformation.name
? uploadedProfileInformation.name
: '#' + slicedProfileToken;
return (
<li
className={classNames('publishedProfilesListItem', {
publishedProfilesListItem_ConfirmDialogIsOpen: confirmDialogIsOpen,
})}
>
<Localized
id="ListOfPublishedProfiles--published-profiles-link"
attrs={{ title: true }}
vars={{ smallProfileName: smallProfileName }}
>
<a
className="publishedProfilesLink"
href={urlPath}
title={`Click here to load profile ${smallProfileName}`}
>
<div className="publishedProfilesDate">
<Localized
id="NumberFormat--short-date"
vars={{ date: uploadedProfileInformation.publishedDate }}
/>
</div>
<div className="publishedProfilesInfo">
<div className="publishedProfilesName">
<strong>{profileName}</strong> (
{_formatRange(uploadedProfileInformation.publishedRange)})
</div>
<ProfileMetaInfoSummary meta={uploadedProfileInformation.meta} />
</div>
</a>
</Localized>
{withActionButtons ? (
<div className="publishedProfilesActionButtons">
{uploadedProfileInformation.jwtToken ? (
<ProfileDeleteButton
buttonClassName="publishedProfilesDeleteButton"
profileName={profileName}
smallProfileName={smallProfileName}
jwtToken={uploadedProfileInformation.jwtToken}
profileToken={uploadedProfileInformation.profileToken}
onOpenConfirmDialog={this.onOpenConfirmDialog}
onCloseConfirmDialog={this.onCloseConfirmDialog}
onCloseSuccessMessage={this.onCloseSuccessMessage}
/>
) : (
<Localized
id="ListOfPublishedProfiles--published-profiles-delete-button-disabled"
attrs={{ title: true }}
>
<button
className="publishedProfilesDeleteButton photon-button photon-button-default"
type="button"
title="This profile cannot be deleted because we lack the authorization information."
disabled
>
Delete
</button>
</Localized>
)}
</div>
) : null}
</li>
);
}
}
type Props = {
withActionButtons: boolean;
limit?: number;
};
type State = {
uploadedProfileInformationList: null | UploadedProfileInformation[];
};
export class ListOfPublishedProfiles extends PureComponent<Props, State> {
_isMounted = false;
override state: State = {
uploadedProfileInformationList: null,
};
_refreshList = async () => {
const uploadedProfileInformationList =
await listAllUploadedProfileInformationFromDb();
if (this._isMounted) {
// It isn't ideal to use a setState here, but this is the only way.
this.setState({
// We want to display the list with the most recent uploaded profile first.
uploadedProfileInformationList:
uploadedProfileInformationList.reverse(),
});
}
};
override async componentDidMount() {
this._isMounted = true;
this._refreshList();
window.addEventListener('focus', this._refreshList);
}
override componentWillUnmount() {
this._isMounted = false;
window.removeEventListener('focus', this._refreshList);
}
onProfileDelete = () => {
this._refreshList();
};
override render() {
const { limit, withActionButtons } = this.props;
const { uploadedProfileInformationList } = this.state;
if (!uploadedProfileInformationList) {
return null;
}
// TypeScript type narrowing help
const profileList: UploadedProfileInformation[] =
uploadedProfileInformationList;
if (!profileList.length) {
return (
<p className="photon-body-30">
<Localized id="ListOfPublishedProfiles--uploaded-profile-information-list-empty">
No profile has been uploaded yet!
</Localized>
</p>
);
}
const reducedUploadedProfileInformationList = limit
? profileList.slice(0, limit)
: profileList;
const profilesRestCount =
profileList.length - reducedUploadedProfileInformationList.length;
let profileRestLabel;
if (profilesRestCount > 0) {
profileRestLabel = (
<Localized
id="ListOfPublishedProfiles--uploaded-profile-information-label"
vars={{ profilesRestCount: profilesRestCount }}
>
<>See and manage all your recordings ({profilesRestCount} more)</>
</Localized>
);
} else {
profileRestLabel = (
<Localized
id="ListOfPublishedProfiles--uploaded-profile-information-list"
vars={{
uploadedProfileCount: profileList.length,
}}
>
<>Manage this recording</>
</Localized>
);
}
return (
<>
<ul className="publishedProfilesList">
{reducedUploadedProfileInformationList.map(
(uploadedProfileInformation: UploadedProfileInformation) => (
<PublishedProfile
onProfileDelete={this.onProfileDelete}
key={uploadedProfileInformation.profileToken}
uploadedProfileInformation={uploadedProfileInformation}
withActionButtons={withActionButtons}
/>
)
)}
</ul>
{withActionButtons ? null : (
<p>
<InnerNavigationLink dataSource="uploaded-recordings">
{profileRestLabel}
</InnerNavigationLink>
</p>
)}
</>
);
}
}