Cleanup getting server base url

This commit is contained in:
schroda
2025-10-19 20:22:31 +02:00
parent 2a9a794744
commit ed0d597185
3 changed files with 9 additions and 3 deletions

View File

@@ -9,12 +9,11 @@
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { TextSetting } from '@/base/components/settings/text/TextSetting.tsx'; import { TextSetting } from '@/base/components/settings/text/TextSetting.tsx';
import { requestManager } from '@/lib/requests/RequestManager.ts'; import { requestManager } from '@/lib/requests/RequestManager.ts';
import { useLocalStorage } from '@/base/hooks/useStorage.tsx';
export const ServerAddressSetting = () => { export const ServerAddressSetting = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const [serverAddress, setServerAddress] = useLocalStorage<string>('serverBaseURL', window.location.origin); const [serverAddress, setServerAddress] = requestManager.useBaseUrl();
const handleServerAddressChange = (address: string) => { const handleServerAddressChange = (address: string) => {
const serverBaseUrl = address.replaceAll(/(\/)+$/g, ''); const serverBaseUrl = address.replaceAll(/(\/)+$/g, '');

View File

@@ -338,6 +338,7 @@ import { MangaIdInfo } from '@/features/manga/Manga.types.ts';
import { updateMetadataList } from '@/features/metadata/services/MetadataApolloCacheHandler.ts'; import { updateMetadataList } from '@/features/metadata/services/MetadataApolloCacheHandler.ts';
import { USER_LOGIN, USER_REFRESH } from '@/lib/graphql/mutations/UserMutation.ts'; import { USER_LOGIN, USER_REFRESH } from '@/lib/graphql/mutations/UserMutation.ts';
import { AuthManager } from '@/features/authentication/AuthManager.ts'; import { AuthManager } from '@/features/authentication/AuthManager.ts';
import { useLocalStorage } from '@/base/hooks/useStorage.tsx';
enum GQLMethod { enum GQLMethod {
QUERY = 'QUERY', QUERY = 'QUERY',
@@ -487,6 +488,10 @@ export class RequestManager {
return this.restClient.getBaseUrl(); return this.restClient.getBaseUrl();
} }
public useBaseUrl() {
return useLocalStorage(BaseClient.BASE_URL_KEY, () => this.getBaseUrl());
}
public getValidUrlFor(endpoint: string, apiVersion: string = RequestManager.API_VERSION): string { public getValidUrlFor(endpoint: string, apiVersion: string = RequestManager.API_VERSION): string {
return `${this.getBaseUrl()}${apiVersion}${endpoint}`; return `${this.getBaseUrl()}${apiVersion}${endpoint}`;
} }

View File

@@ -20,6 +20,8 @@ interface QueuedRequest {
} }
export abstract class BaseClient<Client, ClientConfig, Fetcher> { export abstract class BaseClient<Client, ClientConfig, Fetcher> {
static readonly BASE_URL_KEY = 'serverBaseURL';
protected abstract client: Client; protected abstract client: Client;
public abstract readonly fetcher: Fetcher; public abstract readonly fetcher: Fetcher;
@@ -90,7 +92,7 @@ export abstract class BaseClient<Client, ClientConfig, Fetcher> {
? import.meta.env.VITE_SERVER_URL_DEFAULT ? import.meta.env.VITE_SERVER_URL_DEFAULT
: `${protocol}//${hostname}:${port}`; : `${protocol}//${hostname}:${port}`;
const serverBaseURL = AppStorage.local.getItemParsed('serverBaseURL', defaultUrl); const serverBaseURL = AppStorage.local.getItemParsed(BaseClient.BASE_URL_KEY, defaultUrl);
// Apply subpath configuration to the base URL // Apply subpath configuration to the base URL
return SubpathUtil.getApiBaseUrl(serverBaseURL); return SubpathUtil.getApiBaseUrl(serverBaseURL);