-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathHeader.tsx
415 lines (395 loc) · 15 KB
/
Header.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
import * as remote from '@electron/remote';
import { openContextMenu } from '@renderer/components/app';
import { WithTagCategoriesProps } from '@renderer/containers/withTagCategories';
import { FpfssUser } from '@shared/back/types';
import { getLibraryItemTitle } from '@shared/library/util';
import { MenuItemConstructorOptions } from 'electron';
import * as React from 'react';
import { Link, RouteComponentProps, useLocation } from 'react-router-dom';
import { WithPreferencesProps } from '../containers/withPreferences';
import { Paths } from '@shared/Paths';
import { joinLibraryRoute } from '../Util';
import { LangContext } from '../util/lang';
import { OpenIcon } from './OpenIcon';
import { WithSearchProps } from '@renderer/containers/withSearch';
import { GENERAL_VIEW_ID } from '@renderer/store/search/slice';
import { updatePreferencesData } from '@shared/preferences/util';
import { WithViewProps } from '@renderer/containers/withView';
import { WithMainStateProps } from '@renderer/containers/withMainState';
import { DialogField, DialogState } from 'flashpoint-launcher';
import { uuid } from '@shared/utils/uuid';
import { WithConfirmDialogProps } from '@renderer/containers/withConfirmDialog';
const viewDragType = 'text/plain';
type OwnProps = {
/** Array of library routes */
libraries: string[];
/** Called when the left sidebar toggle button is clicked. */
onToggleLeftSidebarClick?: () => void;
/** Called when the right sidebar toggle button is clicked. */
onToggleRightSidebarClick?: () => void;
user: FpfssUser | null;
logoutUser: () => void;
};
export type HeaderProps = OwnProps & RouteComponentProps & WithMainStateProps & WithConfirmDialogProps & WithPreferencesProps & WithTagCategoriesProps & WithSearchProps & WithViewProps;
type HeaderState = {};
/** The header that is always visible at the top of the main window (just below the title bar). */
export class Header extends React.Component<HeaderProps, HeaderState> {
static contextType = LangContext;
declare context: React.ContextType<typeof LangContext>;
constructor(props: HeaderProps) {
super(props);
}
onRenameView = async (view: string) => {
let warning: string | undefined;
while (true) {
const name = await this.getUserInput('Enter View Name', warning);
if (name !== '') {
const views = Object.keys(this.props.search.views);
if (views.includes(name)) {
warning = 'Name already in use';
continue;
} else {
// Change prefs
const customViews = [...this.props.preferencesData.customViews];
const customViewsIdx = this.props.preferencesData.customViews.findIndex(v => v === view);
if (customViewsIdx > -1) {
customViews[customViewsIdx] = name;
} else {
customViews.push(name);
}
const storedViews = [...this.props.preferencesData.storedViews.filter(s => s.view !== view)];
const existingStoredView = this.props.preferencesData.storedViews.find(s => s.view === view);
if (existingStoredView) {
storedViews.push({
...existingStoredView,
view: name
});
}
if (this.props.preferencesData.defaultOpeningPage === joinLibraryRoute(view)) {
updatePreferencesData({
defaultOpeningPage: joinLibraryRoute(name)
});
}
updatePreferencesData({
customViews,
storedViews,
}, true);
if (this.props.currentView.id === view) {
// Move to LOADING page during change over
this.props.history.push(Paths.LOADING);
// Let the search action do the data swap
this.props.searchActions.renameView({
old: view,
new: name,
});
setTimeout(() => {
this.props.history.push(joinLibraryRoute(name));
}, 200);
} else {
this.props.searchActions.renameView({
old: view,
new: name,
});
}
}
}
break;
}
};
onCreateNewView = async () => {
let warning: string | undefined;
while (true) {
const name = await this.getUserInput('Enter View Name', warning);
if (name !== '') {
const views = Object.keys(this.props.search.views);
if (views.includes(name)) {
warning = 'Name already in use';
continue;
} else {
// Add new view
const customViews = [...this.props.preferencesData.customViews, name];
updatePreferencesData({
customViews,
}, true);
this.props.searchActions.addViews({
views: [name],
areLibraries: false,
});
setTimeout(() => {
this.props.history.push(joinLibraryRoute(name));
}, 200);
}
}
break;
}
};
onDragStart = (event: React.DragEvent<HTMLLIElement>, view: string) => {
console.log(`dragged: ${view}`);
event.dataTransfer.dropEffect = 'move';
event.dataTransfer.setData(viewDragType, view);
};
onDrop = (event: React.DragEvent<HTMLLIElement>, newView: string) => {
const view = event.dataTransfer.getData(viewDragType);
if (view) {
console.log(`dropped: ${view}`);
// Swap views
const customViews = [...this.props.preferencesData.customViews];
const oldIdx = customViews.findIndex(v => v === view);
const newIdx = customViews.findIndex(v => v === newView);
if (oldIdx > -1 && newIdx > -1) {
customViews[oldIdx] = newView;
customViews[newIdx] = view;
}
updatePreferencesData({
customViews,
});
}
};
onDeleteView = async (view: string) => {
const strings = this.context;
// Confirm first
const confirmation = await this.props.openConfirmDialog(
strings.dialog.areYouSure,
[strings.dialog.cancel, strings.misc.yes],
0,
1,
);
if (confirmation === 1) {
if (this.props.currentView.id === view) {
// Must move to the home tab first!
this.props.history.push(Paths.HOME);
}
const customViews = this.props.preferencesData.customViews.filter(v => v !== view);
const storedViews = this.props.preferencesData.storedViews.filter(v => v.view !== view);
// Make sure the default page is always valid
if (this.props.preferencesData.defaultOpeningPage === joinLibraryRoute(view)) {
updatePreferencesData({
defaultOpeningPage: Paths.HOME,
});
}
updatePreferencesData({
customViews: customViews,
storedViews: storedViews,
}, true);
this.props.searchActions.deleteView({
view: view
});
}
};
getUserInput = async (message: string, warning?: string, placeholder?: string): Promise<string> => {
return new Promise<string>((resolve) => {
const fields: DialogField[] = [];
fields.push({
type: 'string',
name: 'name',
value: placeholder || '',
});
if (warning) {
fields.push({
type: 'string',
name: 'warning',
locked: true,
value: warning,
});
}
const dialog: DialogState = {
id: uuid(),
largeMessage: true,
userCanCancel: false,
message: message,
cancelId: 0,
fields,
buttons: ['Cancel', 'Confirm'],
};
this.props.mainActions.createDialog(dialog);
window.Shared.dialogResEvent.once(dialog.id, (d: DialogState, value: number) => {
if (value === 1 && d.fields) {
const field = d.fields.find(f => f.name === 'name');
if (field) {
resolve(field.value as string);
} else {
resolve('');
}
} else {
resolve('');
}
});
});
};
render() {
const strings = this.context.app;
const {
preferencesData: { browsePageShowLeftSidebar, browsePageShowRightSidebar, enableEditing, showDeveloperTab, onlineManual, offlineManual },
onToggleLeftSidebarClick, onToggleRightSidebarClick
} = this.props;
// FPFSS user context menu
const contextButtons: MenuItemConstructorOptions[] = [
{
label: strings.fpfssProfile,
enabled: true,
click: () => {
remote.shell.openExternal(`${this.props.preferencesData.fpfssBaseUrl}/web/profile`);
}
},
{
label: strings.fpfssLogout,
enabled: true,
click: () => {
this.props.logoutUser();
}
}
];
const browseViews = this.props.preferencesData.useCustomViews ?
Object.keys(this.props.search.views).filter(k => k !== GENERAL_VIEW_ID) :
this.props.libraries;
if (this.props.preferencesData.useCustomViews) {
browseViews.sort((a, b) => {
const aIdx = this.props.preferencesData.customViews.findIndex(v => v === a);
const bIdx = this.props.preferencesData.customViews.findIndex(v => v === b);
return aIdx - bIdx;
});
}
return (
<div className='header'>
{/* Header Menu */}
<div className='header__wrap'>
<ul className='header__menu'>
<MenuItem title={strings.home} link={Paths.HOME} />
{
this.props.preferencesData.useCustomViews ?
browseViews.map(view => (
<MenuItem
key={view}
title={view}
onDragStart={(event) => this.onDragStart(event, view)}
onDrop={(event) => this.onDrop(event, view)}
link={joinLibraryRoute(view)}
onContextMenu={() => {
const contextButtons: MenuItemConstructorOptions[] = [
{
label: browseViews.length > 1 || view !== 'Browse' ? strings.deleteView : strings.deleteOnlyBrowseView,
enabled: browseViews.length > 1 ? true : view !== 'Browse',
click: () => this.onDeleteView(view),
},
{
label: 'Rename View',
click: () => this.onRenameView(view),
},
];
const menu = remote.Menu.buildFromTemplate(contextButtons);
menu.popup({ window: remote.getCurrentWindow() });
}} />
)) :
browseViews.map(view => (
<MenuItem
key={view}
title={getLibraryItemTitle(view, this.context.libraries)}
link={joinLibraryRoute(view)} />
))
}
{this.props.preferencesData.useCustomViews && (
<li className='header__menu__item' onClick={this.onCreateNewView} title={strings.createNewView}>
<OpenIcon icon={'plus'} />
</li>
)}
{enableEditing ? (
<>
<MenuItem
title={strings.tags}
link={Paths.TAGS} />
<MenuItem
title={strings.categories}
link={Paths.CATEGORIES} />
</>
) : undefined}
<MenuItem
title={strings.logs}
link={Paths.LOGS} />
<MenuItem
title={strings.config}
link={Paths.CONFIG} />
{(onlineManual || offlineManual) && (
<MenuItem
title={strings.manual}
link={Paths.MANUAL} />
)}
<MenuItem
title={strings.about}
link={Paths.ABOUT} />
{enableEditing ? (
<>
<MenuItem
title={strings.curate}
link={Paths.CURATE} />
<MenuItem
title={'Game Data'}
link={Paths.BROWSE_GAME_DATA} />
</>
) : undefined}
{showDeveloperTab ? (
<MenuItem
title={strings.developer}
link={Paths.DEVELOPER} />
) : undefined}
</ul>
</div>
{/* Right-most portion */}
<div className='header__wrap header__right'>
{this.props.user && (
<div className='header-user-box' onClick={() => openContextMenu(contextButtons)}>
{/* FPFSS user status */}
<div className='header-user-icon' style={{ backgroundImage: `url(${this.props.user.avatarUrl})` }}></div>
<div className='header-user-name'>{this.props.user.username}</div>
</div>
)}
<div>
{/* Toggle Right Sidebar */}
<div
className='header__toggle-sidebar'
title={browsePageShowRightSidebar ? strings.hideRightSidebar : strings.showRightSidebar}
onClick={onToggleRightSidebarClick}>
<OpenIcon icon={browsePageShowRightSidebar ? 'collapse-right' : 'expand-right'} />
</div>
{/* Toggle Left Sidebar */}
<div
className='header__toggle-sidebar'
title={browsePageShowLeftSidebar ? strings.hideLeftSidebar : strings.showLeftSidebar}
onClick={onToggleLeftSidebarClick}>
<OpenIcon icon={browsePageShowLeftSidebar ? 'collapse-left' : 'expand-left'} />
</div>
</div>
</div>
</div>
);
}
}
type MenuItemProps = {
title: string;
link: string;
onDragStart?: (event: React.DragEvent<HTMLLIElement>) => void;
onDrop?: (event: React.DragEvent<HTMLLIElement>) => void;
onContextMenu?: () => void;
};
// An item in the header menu. Used as buttons to switch between tabs/pages.
function MenuItem({ title, link, onContextMenu, onDragStart, onDrop }: MenuItemProps) {
const location = useLocation();
const selected = link === '/' ? location.pathname === link : location.pathname.startsWith(link);
const onDragOver = (event: React.DragEvent<HTMLLIElement>) => {
event.preventDefault();
};
const onDragLeave = (event: React.DragEvent<HTMLLIElement>) => {
event.preventDefault();
};
return (
<li
className='header__menu__item'
onContextMenu={onContextMenu}
draggable={onDragStart !== undefined}
onDragStart={onDragStart}
onDragOver={onDragStart !== undefined ? onDragOver : undefined}
onDragLeave={onDragStart !== undefined ? onDragLeave : undefined}
onDrop={onDrop}>
<Link to={link} className={`header__menu__item__link ${selected ? 'header__menu__item__link-selected' : ''}`}>{title}</Link>
</li>
);
}