-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathpublish.js
226 lines (210 loc) · 6.06 KB
/
publish.js
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
/* 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/. */
// @flow
import { combineReducers } from 'redux';
import { getShouldSanitizeByDefault } from '../profile-logic/sanitize';
import type {
CheckedSharingOptions,
PublishState,
UploadState,
UploadPhase,
Reducer,
State,
} from 'firefox-profiler/types';
function _getSanitizingSharingOptions(): CheckedSharingOptions {
return {
includeHiddenThreads: false,
includeAllTabs: false,
includeFullTimeRange: false,
includeScreenshots: false,
includeUrls: false,
includeExtension: false,
includePreferenceValues: false,
includePrivateBrowsingData: false,
};
}
function _getMostlyNonSanitizingSharingOptions(): CheckedSharingOptions {
return {
includeHiddenThreads: true,
includeAllTabs: true,
includeFullTimeRange: true,
includeScreenshots: true,
includeUrls: true,
includeExtension: true,
includePreferenceValues: true,
// We always want to sanitize the private browsing data by default
includePrivateBrowsingData: false,
};
}
const checkedSharingOptions: Reducer<CheckedSharingOptions> = (
state = _getSanitizingSharingOptions(),
action
) => {
switch (action.type) {
case 'PROFILE_LOADED': {
const newState = getShouldSanitizeByDefault(action.profile)
? _getSanitizingSharingOptions()
: _getMostlyNonSanitizingSharingOptions();
return newState;
}
case 'TOGGLE_CHECKED_SHARING_OPTION':
return {
...state,
[action.slug]: !state[action.slug],
};
default:
return state;
}
};
// This is a diagram explaining the ordering of actions for uploading
//
// UPLOAD_COMPRESSION_STARTED
// |
// v
// UPLOAD_STARTED ---> UPDATE_UPLOAD_PROGRESS
// / \ (fired many times)
// v v
// [SANITIZED_]PROFILE_PUBLISHED UPLOAD_ABORTED
// |
// v
// UPLOAD_RESET
//
const phase: Reducer<UploadPhase> = (state = 'local', action) => {
switch (action.type) {
case 'UPLOAD_COMPRESSION_STARTED':
return 'compressing';
case 'UPLOAD_STARTED':
return 'uploading';
case 'PROFILE_PUBLISHED':
case 'SANITIZED_PROFILE_PUBLISHED':
return 'uploaded';
case 'UPLOAD_FAILED':
return 'error';
case 'UPLOAD_ABORTED':
case 'UPLOAD_RESET':
return 'local';
default:
return state;
}
};
const uploadProgress: Reducer<number> = (state = 0, action) => {
switch (action.type) {
case 'UPDATE_UPLOAD_PROGRESS':
return action.uploadProgress;
// Not all of these upload actions really need to be here, but it's nice to
// explicitly list them all here.
case 'UPLOAD_STARTED':
case 'UPLOAD_ABORTED':
case 'UPLOAD_RESET':
case 'PROFILE_PUBLISHED':
case 'SANITIZED_PROFILE_PUBLISHED':
case 'UPLOAD_COMPRESSION_STARTED':
case 'UPLOAD_FAILED':
return 0;
default:
return state;
}
};
const error: Reducer<Error | mixed> = (state = null, action) => {
switch (action.type) {
case 'UPLOAD_FAILED':
return action.error;
case 'UPLOAD_COMPRESSION_STARTED':
// When starting out a new upload, clear out any old errors.
return null;
default:
return state;
}
};
const noop = () => {};
const abortFunction: Reducer<() => void> = (state = noop, action) => {
switch (action.type) {
case 'UPLOAD_ABORTED':
case 'PROFILE_PUBLISHED':
case 'SANITIZED_PROFILE_PUBLISHED':
case 'UPLOAD_FAILED':
return noop;
case 'UPLOAD_COMPRESSION_STARTED':
return action.abortFunction;
default:
return state;
}
};
/**
* Update the generation value for every upload attempt.
*/
const generation: Reducer<number> = (state = 0, action) => {
switch (action.type) {
case 'PROFILE_PUBLISHED':
case 'SANITIZED_PROFILE_PUBLISHED':
case 'UPLOAD_ABORTED':
case 'UPLOAD_FAILED':
case 'HIDE_STALE_PROFILE':
// Increment the generation value when exiting out of the profile uploading.
return state + 1;
default:
return state;
}
};
const upload: Reducer<UploadState> = combineReducers({
phase,
uploadProgress,
abortFunction,
error,
generation,
});
/**
* When sanitizing profiles, it is nice to have the option to revert to the original
* profile and view, which is stored in this reducer.
*/
const prePublishedState: Reducer<null | State> = (state = null, action) => {
switch (action.type) {
case 'SANITIZED_PROFILE_PUBLISHED':
case 'PROFILE_PUBLISHED':
return action.prePublishedState;
case 'REVERT_TO_PRE_PUBLISHED_STATE':
return null;
default:
return state;
}
};
/**
* This piece of state controls the animation of hiding the profile when it's stale.
* Stale profiles are ones that have been mutated in some way. This happens when
* the user goes from an original profile to a sanitized profile, or when they revert
* to the original. We want to display a helpful animation when this happens, and so
* this piece of state controls it.
*/
const isHidingStaleProfile: Reducer<boolean> = (state = false, action) => {
switch (action.type) {
case 'HIDE_STALE_PROFILE':
return true;
case 'VIEW_FULL_PROFILE':
case 'VIEW_ORIGINS_PROFILE':
case 'VIEW_ACTIVE_TAB_PROFILE':
return false;
default:
return state;
}
};
/**
* This piece of state lets components know that a profile has been sanitized.
* This changes the behavior of how the <ProfileViewer> component animates in.
*/
const hasSanitizedProfile: Reducer<boolean> = (state = false, action) => {
switch (action.type) {
case 'HIDE_STALE_PROFILE':
return true;
default:
return state;
}
};
const publishReducer: Reducer<PublishState> = combineReducers({
checkedSharingOptions,
upload,
isHidingStaleProfile,
hasSanitizedProfile,
prePublishedState,
});
export default publishReducer;