From 11925822637fb5f0f6c15546c48dc1f3455ae350 Mon Sep 17 00:00:00 2001 From: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Fri, 18 Apr 2025 17:45:37 -0600 Subject: [PATCH] remove it from the backend :( --- src/backend/accounts/region.ts | 44 ------------- src/pages/parts/admin/RegionSelectorPart.tsx | 21 +----- src/utils/detectRegion.tsx | 69 ++++---------------- 3 files changed, 17 insertions(+), 117 deletions(-) delete mode 100644 src/backend/accounts/region.ts diff --git a/src/backend/accounts/region.ts b/src/backend/accounts/region.ts deleted file mode 100644 index d4246605..00000000 --- a/src/backend/accounts/region.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { ofetch } from "ofetch"; - -import { getAuthHeaders } from "@/backend/accounts/auth"; -import { AccountWithToken } from "@/stores/auth"; - -export type Region = - | "us-east" - | "us-west" - | "south" - | "asia" - | "europe" - | "unknown"; - -export interface RegionResponse { - region: Region; - lastChecked: number; - userPicked: boolean; -} - -export async function updateRegion( - url: string, - account: AccountWithToken, - region: Region, - userPicked: boolean = false, -) { - return ofetch(`/users/${account.userId}/region`, { - method: "PUT", - headers: getAuthHeaders(account.token), - baseURL: url, - body: { - region, - userPicked, - lastChecked: Math.floor(Date.now() / 1000), - }, - }); -} - -export async function getRegion(url: string, account: AccountWithToken) { - return ofetch(`/users/${account.userId}/region`, { - method: "GET", - headers: getAuthHeaders(account.token), - baseURL: url, - }); -} diff --git a/src/pages/parts/admin/RegionSelectorPart.tsx b/src/pages/parts/admin/RegionSelectorPart.tsx index c0b5f549..b0b2f02b 100644 --- a/src/pages/parts/admin/RegionSelectorPart.tsx +++ b/src/pages/parts/admin/RegionSelectorPart.tsx @@ -1,14 +1,10 @@ -import { useState } from "react"; - -import { Region } from "@/backend/accounts/region"; import { Dropdown } from "@/components/form/Dropdown"; import { Box } from "@/components/layout/Box"; import { Heading2 } from "@/components/utils/Text"; -import { useRegionStore } from "@/utils/detectRegion"; +import { Region, useRegionStore } from "@/utils/detectRegion"; export function RegionSelectorPart() { const { region, setRegion } = useRegionStore(); - const [isUpdating, setIsUpdating] = useState(false); const regionOptions = [ { id: "us-east", name: "US East (Ohio)" }, @@ -18,17 +14,6 @@ export function RegionSelectorPart() { { id: "europe", name: "Europe Central (London)" }, ]; - const handleRegionChange = async (item: { id: string; name: string }) => { - setIsUpdating(true); - try { - await setRegion(item.id as Region, true); - } catch (error) { - console.error("Failed to update region:", error); - } finally { - setIsUpdating(false); - } - }; - return ( <> Region Selector @@ -48,12 +33,12 @@ export function RegionSelectorPart() { regionOptions.find((r) => r.id === region)?.name || "Unknown (US East)", }} - setSelectedItem={handleRegionChange} + setSelectedItem={(item) => setRegion(item.id as Region, true)} direction="up" />

- Use with caution. Changing the region could reset your token! + Use with caution. Changing the region will reset your token!

diff --git a/src/utils/detectRegion.tsx b/src/utils/detectRegion.tsx index 849ac50b..32d8b93a 100644 --- a/src/utils/detectRegion.tsx +++ b/src/utils/detectRegion.tsx @@ -1,9 +1,13 @@ import { create } from "zustand"; import { persist } from "zustand/middleware"; -import { Region, getRegion, updateRegion } from "@/backend/accounts/region"; -import { conf } from "@/setup/config"; -import { useAuthStore } from "@/stores/auth"; +export type Region = + | "us-east" + | "us-west" + | "south" + | "asia" + | "europe" + | "unknown"; interface RegionStore { region: Region | null; @@ -18,33 +22,12 @@ export const useRegionStore = create()( region: null, lastChecked: null, userPicked: false, - setRegion: async (region, userPicked = false) => { - const url = conf().BACKEND_URL; - const account = useAuthStore.getState().account; - - if (url && account) { - try { - const response = await updateRegion( - url, - account, - region, - userPicked, - ); - set({ - region: response.region, - lastChecked: response.lastChecked, - userPicked: response.userPicked, - }); - } catch (error) { - console.error("Failed to update region:", error); - } - } else { - set({ - region, - lastChecked: Math.floor(Date.now() / 1000), - userPicked, - }); - } + setRegion: (region, userPicked = false) => { + set({ + region, + lastChecked: Math.floor(Date.now() / 1000), + userPicked, + }); }, }), { @@ -106,16 +89,11 @@ function determineRegion(data: { // 1. Check if user manually picked a region (highest priority) // 2. Check if we need to refresh the region -// 3. If refresh needed: -// a. Try to get fresh region from backend -// b. If backend region is fresh, use it -// c. If backend region is expired or unavailable, fall back to IP detection +// 3. If refresh needed, fall back to IP detection // 4. If no refresh needed, use existing region export async function detectRegion(): Promise { const store = useRegionStore.getState(); - const url = conf().BACKEND_URL; - const account = useAuthStore.getState().account; // If user picked a region, always return that if (store.userPicked && store.region) { @@ -133,25 +111,6 @@ export async function detectRegion(): Promise { } try { - // Try to get fresh region from backend first - if (url && account) { - try { - const response = await getRegion(url, account); - // Only update if the backend has a fresh region - if ( - response.lastChecked && - Math.floor(Date.now() / 1000) - response.lastChecked < 2592000 - ) { - if (!store.userPicked) { - store.setRegion(response.region, response.userPicked); - } - return response.region; - } - } catch (error) { - console.warn("Failed to get region from backend:", error); - } - } - // Fallback to IP-based detection const response = await fetch("https://ipapi.co/json/"); const data = await response.json();