-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbehold.tsx
185 lines (164 loc) · 6.78 KB
/
behold.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
import React from 'react';
import { Link, navigate } from 'gatsby';
import { Step, Icon, Button, Message, Popup } from 'semantic-ui-react';
import { CrewMember } from '../model/crew';
import { GlobalContext } from '../context/globalcontext';
import DataPageLayout from '../components/page/datapagelayout';
import CrewPicker from '../components/crewpicker';
import { crewCopy } from '../utils/crewutils';
import { useStateWithStorage } from '../utils/storage';
import { BeholdOptionsModal, BeholdModalOptions, DEFAULT_BEHOLD_OPTIONS } from '../components/behold/optionsmodal';
import { ClassicView } from '../components/behold/classicview';
import { StandardView } from '../components/behold/standardview';
import { TableView } from '../components/behold/tableview';
type BeholdsPageProps = {
location: any;
};
const BeholdsPage = (props: BeholdsPageProps) => {
const globalContext = React.useContext(GlobalContext);
const { t } = globalContext.localized;
const crewList = crewCopy<CrewMember>(globalContext.core.crew)
.sort((a, b) => a.name.localeCompare(b.name));
let crewFromUrl = [] as string[];
if (props.location) {
const urlParams = new URLSearchParams(props.location.search);
if (urlParams.has('crew')) crewFromUrl = urlParams.getAll('crew');
}
return (
<DataPageLayout
pageTitle={t('menu.tools.behold_helper')}
pageDescription={t('behold_helper.heading')}
playerPromptType='recommend'
>
<React.Fragment>
<BeholdHelper crewList={crewList} initSelection={crewFromUrl} />
</React.Fragment>
</DataPageLayout>
);
};
type BeholdHelperProps = {
crewList: CrewMember[];
initSelection: string[];
};
const BeholdHelper = (props: BeholdHelperProps) => {
const { crewList } = props;
const globalContext = React.useContext(GlobalContext);
const { t, tfmt } = globalContext.localized;
const [selectedCrew, setSelectedCrew] = useStateWithStorage<string[]>('behold/crew', props.initSelection);
const [crewView, setCrewView] = useStateWithStorage('behold/view', 'classic'); // Do not use rememberForever on this
const [options, setOptions] = React.useState<BeholdModalOptions>(DEFAULT_BEHOLD_OPTIONS);
const filterCrew = (data: CrewMember[], searchFilter: string = ''): CrewMember[] => {
// Filtering
const portalFilter = (crew: CrewMember) => {
if (options.portal.slice(0, 6) === 'portal' && !crew.in_portal) return false;
if (options.portal === 'portal-unique' && (crew.unique_polestar_combos?.length ?? 0) === 0) return false;
if (options.portal === 'portal-nonunique' && (crew.unique_polestar_combos?.length ?? 0) > 0) return false;
if (options.portal === 'nonportal' && crew.in_portal) return false;
return true;
};
const query = (input: string) => input.toLowerCase().replace(/[^a-z0-9]/g, '').indexOf(searchFilter.toLowerCase().replace(/[^a-z0-9]/g, '')) >= 0;
data = data.filter(crew =>
true
&& (options.portal === '' || portalFilter(crew))
&& (options.series.length === 0 || (crew.series && options.series.includes(crew.series)))
&& (options.rarities.length === 0 || options.rarities.includes(crew.max_rarity))
&& (searchFilter === '' || (query(crew.name) || query(crew.short_name)))
);
return data;
};
const permalink = selectedCrew.reduce((prev, curr) => { if (prev !== '') prev += '&'; return prev+'crew='+curr; }, '');
return (
<React.Fragment>
<CrewPicker defaultOptions={DEFAULT_BEHOLD_OPTIONS} pickerModal={BeholdOptionsModal}
options={options} setOptions={setOptions}
crewList={crewList} filterCrew={filterCrew} handleSelect={onCrewPick}
/>
{selectedCrew.length > 0 && (
<React.Fragment>
<Step.Group fluid widths={4}>
<Step active={crewView === 'classic'} onClick={() => setCrewView('classic')}>
<Icon name='block layout' />
<Step.Content>
<Step.Title>{t('behold_helper.views.classic.title')}</Step.Title>
<Step.Description>{t('behold_helper.views.classic.description')}</Step.Description>
</Step.Content>
</Step>
<Step active={crewView === 'details'} onClick={() => setCrewView('details')}>
<Icon name='newspaper' />
<Step.Content>
<Step.Title>{t('behold_helper.views.standard.title')}</Step.Title>
<Step.Description>{t('behold_helper.views.standard.description')}</Step.Description>
</Step.Content>
</Step>
<Step active={crewView === 'table'} onClick={() => setCrewView('table')}>
<Icon name='table' />
<Step.Content>
<Step.Title>{t('behold_helper.views.table.title')}</Step.Title>
<Step.Description>{t('behold_helper.views.table.description')}</Step.Description>
</Step.Content>
</Step>
<Step onClick={() => setSelectedCrew([])}>
<Icon name='x' color='red' />
<Step.Content>
<Step.Title>{t('behold_helper.views.dismiss_all.title')}</Step.Title>
<Step.Description>{t('behold_helper.views.dismiss_all.description')}</Step.Description>
</Step.Content>
</Step>
</Step.Group>
{crewView === 'classic' &&
<ClassicView selectedCrew={selectedCrew} crewList={crewList} handleDismiss={onCrewDismiss} />
}
{crewView === 'details' &&
<StandardView selectedCrew={selectedCrew} crewList={crewList} handleDismiss={onCrewDismiss} />
}
{crewView === 'table' &&
<TableView selectedCrew={selectedCrew} crewList={crewList} />
}
{false && selectedCrew.length > 1 &&
<div style={{ marginTop: '2em', textAlign: 'right' }}>
<Button size='large' fluid onClick={() => addProspects(selectedCrew)} disabled>
<Icon name='add user' color='green' />
{t('behold_helper.preview_all')}
</Button>
</div>
}
<Message style={{ marginTop: '2em' }}>
<Message.Header>{t('behold_helper.share_page_heading')}</Message.Header>
<p>{tfmt('behold_helper.share_help', {
permalink: <Link to={`/behold?${permalink}`}>{t('global.permalink')}</Link>
})}</p>
<Popup
content='Copied!'
on='click'
position='right center'
size='tiny'
trigger={
<Button icon='clipboard' content={t('global.copy_permalink_clipboard')}
onClick={() => navigator.clipboard.writeText(`${process.env.GATSBY_DATACORE_URL}/behold?${permalink}`)}
/>
}
/>
</Message>
</React.Fragment>
)}
</React.Fragment>
);
function onCrewPick(crew: CrewMember): void {
if (!selectedCrew.includes(crew.symbol)) {
selectedCrew.push(crew.symbol);
setSelectedCrew([...selectedCrew]);
}
}
function onCrewDismiss(selectedIndex: number): void {
selectedCrew.splice(selectedIndex, 1);
setSelectedCrew([...selectedCrew]);
}
function addProspects(crewSymbols: string[]): void {
const linkUrl = '/playertools?tool=crew';
const linkState = {
prospect: crewSymbols
};
navigate(linkUrl, { state: linkState });
}
};
export default BeholdsPage;