forked from truckermudgeon/maps
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAchievementSearchBar.tsx
More file actions
170 lines (162 loc) · 5.01 KB
/
Copy pathAchievementSearchBar.tsx
File metadata and controls
170 lines (162 loc) · 5.01 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
import {
Autocomplete,
AutocompleteOption,
createFilterOptions,
ListItemContent,
ListItemDecorator,
Typography,
} from '@mui/joy';
import { assertExists } from '@truckermudgeon/base/assert';
import { putIfAbsent } from '@truckermudgeon/base/map';
import type {
AtsDlcGuard,
AtsSelectableDlc,
} from '@truckermudgeon/map/constants';
import { toAtsDlcGuards } from '@truckermudgeon/map/constants';
import type { AchievementFeature } from '@truckermudgeon/map/types';
import { type StateCode } from '@truckermudgeon/ui';
import type { ReactElement } from 'react';
import { useEffect, useState } from 'react';
export interface AchievementOption {
// achievement title
label: string;
// achievement id
value: string;
desc: string;
imgUrl: string;
features: {
coordinates: [number, number];
dlcGuard: number;
}[];
}
// The files ats-achievements.json and ets2-achievements.json can be created by
// visiting the SteamDB achievement stats page for each game and executing a JS
// command. The result is the `data` array in the AchievementsJson interface.
// ATS: https://steamdb.info/app/270880/stats/
// ETS2: https://steamdb.info/app/227300/stats/
// $$('tr[id|=achievement]').map(tr=>{const[id,titleAndDesc,imgs]=$$('td',tr);const[title,desc]=titleAndDesc.textContent.trim().split('\\n\\n');return{id:id.textContent,title,desc,imgUrl:$('img',imgs).src}});
export interface AchievementsJson {
data: {
id: string;
title: string;
desc: string;
imgUrl: string;
}[];
}
type SearchBarProps = {
selectDecorator: ReactElement;
onSelect: (option: AchievementOption | null) => void;
} & (
| {
map: 'usa';
visibleStates: Set<StateCode>;
visibleStateDlcs: Set<AtsSelectableDlc>;
}
| {
map: 'europe';
}
);
export const AchievementSearchBar = (props: SearchBarProps) => {
const { map, selectDecorator, onSelect } = props;
const [achievements, setAchievements] = useState<AchievementOption[]>([]);
useEffect(() => {
const game = map === 'usa' ? 'ats' : 'ets2';
Promise.all([
fetch(`${game}-achievements.json`).then(
r => r.json() as Promise<AchievementsJson>,
),
fetch(`${game}-achievements.geojson`).then(
r =>
r.json() as Promise<
GeoJSON.FeatureCollection<
AchievementFeature['geometry'],
AchievementFeature['properties']
>
>,
),
]).then(
([achievements, geoJson]) => {
const features = new Map<
string,
{ coordinates: [number, number]; dlcGuard: number }[]
>();
for (const f of geoJson.features) {
putIfAbsent(f.properties.name, [], features).push({
coordinates: f.geometry.coordinates as [number, number],
dlcGuard: f.properties.dlcGuard,
});
}
const geoNames = new Set<string>(
geoJson.features.map(feature => feature.properties.name),
);
setAchievements([
...achievements.data
.filter(a => geoNames.has(a.id))
.map(a => ({
...a,
label: a.title,
value: a.id,
features: assertExists(features.get(a.id)),
}))
.sort((a, b) => a.label.localeCompare(b.label)),
]);
},
() => console.error('could not load achievements json.'),
);
}, [map]);
const options = achievements.filter(a => {
if (map === 'europe') {
// TODO add country filtering for europe
return true;
}
const enabledDlcGuards = toAtsDlcGuards(props.visibleStateDlcs);
return a.features.some(f =>
enabledDlcGuards.has(f.dlcGuard as AtsDlcGuard),
);
});
const filterOptions = createFilterOptions<AchievementOption>({
stringify: option => [option.label, option.desc].join(' '),
});
return (
<Autocomplete
// Hacky way to clear the current selection when `map` prop changes.
key={map}
onChange={(_, v) => onSelect(v)}
placeholder={'Search achievements...'}
options={options}
filterOptions={filterOptions}
blurOnSelect
autoComplete
sx={{
paddingInlineStart: 0,
flexBasis: '28em',
}}
startDecorator={selectDecorator}
renderOption={(props, option) => (
<AutocompleteOption {...props} key={option.value}>
<ListItemDecorator
sx={{
border: '2px solid var(--joy-palette-neutral-outlinedBorder)',
borderRadius: 6,
overflow: 'hidden',
minWidth: 'fit-content',
mr: 0.5,
}}
>
<img
loading="lazy"
width="48"
height="48"
src={option.imgUrl}
alt=""
/>
</ListItemDecorator>
<ListItemContent>
<Typography level={'title-md'}>{option.label}</Typography>
<Typography level={'body-xs'}>{option.desc}</Typography>
</ListItemContent>
</AutocompleteOption>
)}
/>
);
};