summaryrefslogtreecommitdiffhomepage
path: root/gui/src
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2021-12-21 16:28:10 +0100
committerOskar Nyberg <oskar@mullvad.net>2022-01-03 13:48:03 +0100
commitc7027fac2b5db7a2a19f1f79137b1a2d371335cb (patch)
tree750ac3f0737f4580258c69540e491303ac619461 /gui/src
parentcce2e17e71d5da6616f321f27264c985a17d5f81 (diff)
downloadmullvadvpn-c7027fac2b5db7a2a19f1f79137b1a2d371335cb.tar.xz
mullvadvpn-c7027fac2b5db7a2a19f1f79137b1a2d371335cb.zip
Memoize modified location list to prevent it from being calculated every render
Diffstat (limited to 'gui/src')
-rw-r--r--gui/src/renderer/components/LocationList.tsx44
1 files changed, 41 insertions, 3 deletions
diff --git a/gui/src/renderer/components/LocationList.tsx b/gui/src/renderer/components/LocationList.tsx
index 720d90a6c2..855efdc066 100644
--- a/gui/src/renderer/components/LocationList.tsx
+++ b/gui/src/renderer/components/LocationList.tsx
@@ -281,17 +281,55 @@ interface IRelayLocationsProps {
onTransitionEnd?: () => void;
}
+interface Relay extends IRelayLocationRelayRedux {
+ label: string;
+ disabled: boolean;
+}
+
+interface City extends Omit<IRelayLocationCityRedux, 'relays'> {
+ label: string;
+ active: boolean;
+ disabled: boolean;
+ relays: Array<Relay>;
+}
+
+interface Country extends Omit<IRelayLocationRedux, 'cities'> {
+ label: string;
+ active: boolean;
+ disabled: boolean;
+ cities: Array<City>;
+}
+
+type CountryList = Array<Country>;
+
+interface IRelayLocationsState {
+ countries: CountryList;
+}
+
interface ICommonCellProps {
location: RelayLocation;
selected: boolean;
ref?: React.Ref<HTMLDivElement>;
}
-export class RelayLocations extends React.PureComponent<IRelayLocationsProps> {
+export class RelayLocations extends React.PureComponent<
+ IRelayLocationsProps,
+ IRelayLocationsState
+> {
+ public state = {
+ countries: this.prepareRelaysForPresentation(this.props.source),
+ };
+
+ public componentDidUpdate(prevProps: IRelayLocationsProps) {
+ if (this.props.source !== prevProps.source) {
+ this.setState({ countries: this.prepareRelaysForPresentation(this.props.source) });
+ }
+ }
+
public render() {
return (
<>
- {this.prepareRelaysForPresentation(this.props.source).map((relayCountry) => {
+ {this.state.countries.map((relayCountry) => {
const countryLocation: RelayLocation = { country: relayCountry.code };
return (
@@ -349,7 +387,7 @@ export class RelayLocations extends React.PureComponent<IRelayLocationsProps> {
);
}
- private prepareRelaysForPresentation(relayList: IRelayLocationRedux[]) {
+ private prepareRelaysForPresentation(relayList: IRelayLocationRedux[]): CountryList {
return relayList
.map((country) => {
const countryDisabled = this.isCountryDisabled(country, country.code);