Skip to content

Commit

Permalink
Merge branch 'relay-list-filter-shows-incorrect-count-when-having-mul…
Browse files Browse the repository at this point in the history
…tiple-des-332'
  • Loading branch information
raksooo committed Oct 17, 2023
2 parents 391268b + 2f49c79 commit 5f6b069
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
65 changes: 42 additions & 23 deletions gui/src/renderer/components/Filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ export default function Filter() {
const [ownership, setOwnership] = useState<Ownership>(initialOwnership);

// Available providers are used to only show compatible options after activating a filter.
const { availableProviders, availableOwnershipOptions } = useFilteredFilters(
const availableProviders = useFilteredProviders([], ownership);
const availableOwnershipOptions = useFilteredOwnershipOptions(
formattedProviderList,
ownership,
Ownership.any,
);

// Applies the changes by sending them to the daemon.
Expand Down Expand Up @@ -113,31 +114,21 @@ export default function Filter() {
);
}

// Returns only the options for each filter that are compatible with current filter selection.
function useFilteredFilters(providers: string[], ownership: Ownership) {
// Returns only the ownership options that are compatible with the other filters
function useFilteredOwnershipOptions(providers: string[], ownership: Ownership): Ownership[] {
const relaySettings = useNormalRelaySettings();
const bridgeState = useSelector((state) => state.settings.bridgeState);
const locations = useSelector((state) => state.settings.relayLocations);

const endpointType = bridgeState === 'on' ? EndpointType.any : EndpointType.exit;

const availableProviders = useMemo(() => {
const relayListForEndpointType = filterLocationsByEndPointType(
locations,
endpointType,
relaySettings,
);
const relaylistForFilters = filterLocations(relayListForEndpointType, ownership, []);
return providersFromRelays(relaylistForFilters);
}, [locations, ownership]);

const availableOwnershipOptions = useMemo(() => {
const relayListForEndpointType = filterLocationsByEndPointType(
locations,
endpointType,
relaySettings,
);
const relaylistForFilters = filterLocations(relayListForEndpointType, Ownership.any, providers);
const relaylistForFilters = filterLocations(relayListForEndpointType, ownership, providers);

const filteredRelayOwnership = relaylistForFilters.flatMap((country) =>
country.cities.flatMap((city) => city.relays.map((relay) => relay.owned)),
Expand All @@ -152,9 +143,30 @@ function useFilteredFilters(providers: string[], ownership: Ownership) {
}

return ownershipOptions;
}, [locations, providers]);
}, [locations, providers, ownership]);

return availableOwnershipOptions;
}

// Returns only the providers that are compatible with the other filters
export function useFilteredProviders(providers: string[], ownership: Ownership): string[] {
const relaySettings = useNormalRelaySettings();
const bridgeState = useSelector((state) => state.settings.bridgeState);
const locations = useSelector((state) => state.settings.relayLocations);

const endpointType = bridgeState === 'on' ? EndpointType.any : EndpointType.exit;

const availableProviders = useMemo(() => {
const relayListForEndpointType = filterLocationsByEndPointType(
locations,
endpointType,
relaySettings,
);
const relaylistForFilters = filterLocations(relayListForEndpointType, ownership, providers);
return providersFromRelays(relaylistForFilters);
}, [locations, ownership]);

return { availableProviders, availableOwnershipOptions };
return availableProviders;
}

// Returns all available providers in the provided relay list.
Expand Down Expand Up @@ -252,15 +264,17 @@ function FilterByProvider(props: IFilterByProviderProps) {

const onToggle = useCallback(
(provider: string) =>
props.setProviders((providers) => ({ ...providers, [provider]: !providers[provider] })),
[props.setProviders],
props.setProviders((providers) => {
const newProviders = { ...providers, [provider]: !providers[provider] };
return props.availableOptions.every((provider) => newProviders[provider])
? toggleAllProviders(providers, true)
: newProviders;
}),
[props.availableOptions, props.setProviders],
);

const toggleAll = useCallback(() => {
props.setProviders((providers) => {
const shouldSelect = !Object.values(providers).every((value) => value);
return Object.fromEntries(Object.keys(providers).map((provider) => [provider, shouldSelect]));
});
props.setProviders((providers) => toggleAllProviders(providers));
}, []);

return (
Expand Down Expand Up @@ -290,6 +304,11 @@ function FilterByProvider(props: IFilterByProviderProps) {
);
}

function toggleAllProviders(providers: Record<string, boolean>, value?: boolean) {
const shouldSelect = value ?? !Object.values(providers).every((value) => value);
return Object.fromEntries(Object.keys(providers).map((provider) => [provider, shouldSelect]));
}

interface IStyledRowTitleProps {
bold?: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RoutePath } from '../../lib/routes';
import { useNormalBridgeSettings, useNormalRelaySettings } from '../../lib/utilityHooks';
import { useSelector } from '../../redux/store';
import * as Cell from '../cell';
import { useFilteredProviders } from '../Filter';
import ImageView from '../ImageView';
import { BackAction } from '../KeyboardNavigation';
import { Layout, SettingsContainer } from '../Layout';
Expand Down Expand Up @@ -69,6 +70,7 @@ export default function SelectLocation() {
const relaySettings = useNormalRelaySettings();
const ownership = relaySettings?.ownership ?? Ownership.any;
const providers = relaySettings?.providers ?? [];
const filteredProviders = useFilteredProviders(providers, ownership);

const [searchValue, setSearchValue] = useState('');

Expand Down Expand Up @@ -201,7 +203,7 @@ export default function SelectLocation() {
'select-location-view',
'Providers: %(numberOfProviders)d',
),
{ numberOfProviders: providers.length },
{ numberOfProviders: filteredProviders.length },
)}
<StyledClearFilterButton
aria-label={messages.gettext('Clear')}
Expand Down

0 comments on commit 5f6b069

Please sign in to comment.