Categorize servers by continents

This commit is contained in:
redphx 2024-10-29 16:51:29 +07:00
parent 67de264aa9
commit 392dc2cf86
4 changed files with 99 additions and 35 deletions

View File

@ -1044,7 +1044,31 @@ export class SettingsNavigationDialog extends NavigationDialog {
}
private renderServerSetting(setting: SettingTabContentItem): HTMLElement {
let selectedValue;
let selectedValue =getPref(PrefKey.SERVER_REGION);
const continents: Record<ServerContinent, {
label: string,
children?: HTMLOptionElement[],
}> = {
'america-north': {
label: t('continent-north-america'),
},
'america-south': {
label: t('continent-south-america'),
},
'asia': {
label: t('continent-asia'),
},
'australia': {
label: t('continent-australia'),
},
'europe': {
label: t('continent-europe'),
},
'other': {
label: t('other'),
},
};
const $control = CE<HTMLSelectElement>('select', {
id: `bx_setting_${setting.pref}`,
@ -1058,8 +1082,6 @@ export class SettingsNavigationDialog extends NavigationDialog {
this.onGlobalSettingChanged(e);
});
selectedValue = getPref(PrefKey.SERVER_REGION);
setting.options = {};
for (const regionName in STATES.serverRegions) {
const region = STATES.serverRegions[regionName];
@ -1076,15 +1098,29 @@ export class SettingsNavigationDialog extends NavigationDialog {
}
setting.options[value] = label;
const $option = CE<HTMLOptionElement>('option', {value: value}, label);
const continent = continents[region.contintent];
if (!continent.children) {
continent.children = [];
}
continent.children.push($option);
}
for (const value in setting.options) {
const label = setting.options[value];
const $option = CE('option', {value: value}, label);
$control.appendChild($option);
const fragment = document.createDocumentFragment();
let key: keyof typeof continents;
for (key in continents) {
const continent = continents[key];
if (!continent.children) {
continue;
}
fragment.appendChild(CE('optgroup', {
label: continent.label,
}, ...continent.children));
}
$control.appendChild(fragment);
$control.disabled = Object.keys(STATES.serverRegions).length === 0;
// Select preferred region

16
src/types/index.d.ts vendored
View File

@ -21,14 +21,24 @@ interface Window {
interface NavigatorBattery extends Navigator {
getBattery: () => Promise<{
charging: boolean,
level: float,
charging: boolean;
level: float;
}>,
}
type ServerContinent = 'america-north' | 'america-south' | 'asia' | 'australia' | 'europe' | 'other';
type ServerRegion = {
baseUri: string;
isDefault: boolean;
name: string;
shortName: string;
contintent: ServerContinent;
};
type BxStates = {
supportedRegion: boolean;
serverRegions: any;
serverRegions: Record<string, ServerRegion>;
selectedRegion: any;
gsToken: string;
isSignedIn: boolean;

View File

@ -67,6 +67,11 @@ const Texts = {
"confirm-reload-stream": "Do you want to refresh the stream?",
"connected": "Connected",
"console-connect": "Connect",
"continent-asia": "Asia",
"continent-australia": "Australia",
"continent-europe": "Europe",
"continent-north-america": "North America",
"continent-south-america": "South America",
"contrast": "Contrast",
"controller": "Controller",
"controller-friendly-ui": "Controller-friendly UI",

View File

@ -14,22 +14,31 @@ import { PrefKey } from "@/enums/pref-keys";
import { getPref, StreamResolution, StreamTouchController } from "./settings-storages/global-settings-storage";
export class XcloudInterceptor {
private static readonly SERVER_EMOJIS = {
AustraliaEast: '🇦🇺',
AustraliaSouthEast: '🇦🇺',
BrazilSouth: '🇧🇷',
EastUS: '🇺🇸',
EastUS2: '🇺🇸',
JapanEast: '🇯🇵',
KoreaCentral: '🇰🇷',
MexicoCentral: '🇲🇽',
NorthCentralUs: '🇺🇸',
SouthCentralUS: '🇺🇸',
SwedenCentral: '🇸🇪',
UKSouth: '🇬🇧',
WestEurope: '🇪🇺',
WestUS: '🇺🇸',
WestUS2: '🇺🇸',
private static readonly SERVER_EXTRA_INFO: Record<string, [string, ServerContinent]> = {
// North America
EastUS: ['🇺🇸', 'america-north'],
EastUS2: ['🇺🇸', 'america-north'],
NorthCentralUs: ['🇺🇸', 'america-north'],
SouthCentralUS: ['🇺🇸', 'america-north'],
WestUS: ['🇺🇸', 'america-north'],
WestUS2: ['🇺🇸', 'america-north'],
MexicoCentral: ['🇲🇽', 'america-north'],
// South America
BrazilSouth: ['🇧🇷', 'america-south'],
// Asia
JapanEast: ['🇯🇵', 'asia'],
KoreaCentral: ['🇰🇷', 'asia'],
// Australia
AustraliaEast: ['🇦🇺', 'australia'],
AustraliaSouthEast: ['🇦🇺', 'australia'],
// Europe
SwedenCentral: ['🇸🇪', 'europe'],
UKSouth: ['🇬🇧', 'europe'],
WestEurope: ['🇪🇺', 'europe'],
};
private static async handleLogin(request: RequestInfo | URL, init?: RequestInit) {
@ -53,10 +62,11 @@ export class XcloudInterceptor {
// Get server list
const serverRegex = /\/\/(\w+)\./;
const serverEmojis = XcloudInterceptor.SERVER_EMOJIS;
const serverExtra = XcloudInterceptor.SERVER_EXTRA_INFO;
for (let region of obj.offeringSettings.regions) {
const regionName = region.name as keyof typeof serverEmojis;
let region: ServerRegion;
for (region of obj.offeringSettings.regions) {
const regionName = region.name as keyof typeof serverExtra;
let shortName = region.name;
if (region.isDefault) {
@ -66,8 +76,11 @@ export class XcloudInterceptor {
let match = serverRegex.exec(region.baseUri);
if (match) {
shortName = match[1];
if (serverEmojis[regionName]) {
shortName = serverEmojis[regionName] + ' ' + shortName;
if (serverExtra[regionName]) {
shortName = serverExtra[regionName][0] + ' ' + shortName;
region.contintent = serverExtra[regionName][1];
} else {
region.contintent = 'other';
}
}