mirror of
https://github.com/p-stream/p-stream.git
synced 2026-03-11 17:55:33 +00:00
remove it from the backend :(
This commit is contained in:
parent
2a4b74bbec
commit
1192582263
3 changed files with 17 additions and 117 deletions
|
|
@ -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<RegionResponse>(`/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<RegionResponse>(`/users/${account.userId}/region`, {
|
||||
method: "GET",
|
||||
headers: getAuthHeaders(account.token),
|
||||
baseURL: url,
|
||||
});
|
||||
}
|
||||
|
|
@ -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 (
|
||||
<>
|
||||
<Heading2 className="mb-8 mt-12">Region Selector</Heading2>
|
||||
|
|
@ -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"
|
||||
/>
|
||||
</div>
|
||||
<p className="max-w-[30rem] text-type-danger">
|
||||
Use with caution. Changing the region could reset your token!
|
||||
Use with caution. Changing the region will reset your token!
|
||||
</p>
|
||||
</Box>
|
||||
</>
|
||||
|
|
|
|||
|
|
@ -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<RegionStore>()(
|
|||
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<Region> {
|
||||
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<Region> {
|
|||
}
|
||||
|
||||
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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue