Properly use https server url when redirect to

This commit is contained in:
Zoe Roux 2026-03-22 10:29:37 +01:00
parent f893d20e95
commit dcf7b4e794
No known key found for this signature in database
4 changed files with 25 additions and 11 deletions

View File

@ -7,6 +7,7 @@ let hoverTimeout: NodeJS.Timeout | number;
export const useMobileHover = () => {
if (Platform.OS !== "web") return;
// biome-ignore lint/correctness/useHookAtTopLevel: const condition
useEffect(() => {
const enableHover = () => {
if (preventHover) return;

View File

@ -31,6 +31,7 @@ export const AccountProvider = ({ children }: { children: ReactNode }) => {
const router = useRouter();
if (Platform.OS !== "web") {
// biome-ignore lint/correctness/useHookAtTopLevel: static
useEffect(() => {
if (!ret.apiUrl) {
setTimeout(() => {

View File

@ -71,6 +71,7 @@ export const readValue = <T extends ZodType>(key: string, parser: T) => {
export const useLocalSetting = <T extends string>(setting: string, def: T) => {
if (Platform.OS === "web" && typeof window === "undefined")
return [def as T, null!] as const;
// biome-ignore lint/correctness/useHookAtTopLevel: ssr
const [val, setter] = useMMKVString(`settings.${setting}`, storage);
return [(val ?? def) as T, setter] as const;
};

View File

@ -1,8 +1,9 @@
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { Platform, View } from "react-native";
import { RetryableError } from "~/models/retryable-error";
import { Button, H1, Input, Link, P } from "~/primitives";
import { type QueryIdentifier, useFetch } from "~/query";
export const cleanApiUrl = (apiUrl: string) => {
if (Platform.OS === "web") return undefined;
@ -13,9 +14,24 @@ export const cleanApiUrl = (apiUrl: string) => {
export const ServerUrlPage = () => {
const [_apiUrl, setApiUrl] = useState("");
const apiUrl = cleanApiUrl(_apiUrl);
const { data, error } = useFetch({
...ServerUrlPage.query,
options: { apiUrl, authToken: null, returnError: true },
const { data, error } = useQuery({
queryKey: [apiUrl, "api", "health"],
queryFn: async (ctx) => {
try {
const apiUrl = "http://kyoo.sdg.moe";
const resp = await fetch(`${apiUrl}/api/health`, {
method: "GET",
signal: ctx.signal,
});
console.log(resp.url);
return resp.url.replace("/api/health", "");
} catch (e) {
console.log("server select fetch error", e);
throw new RetryableError({
key: "offline",
});
}
},
});
const { t } = useTranslation();
@ -43,14 +59,14 @@ export const ServerUrlPage = () => {
<Button
text={t("login.login")}
as={Link}
href={`/login?apiUrl=${apiUrl}`}
href={`/login?apiUrl=${data}`}
disabled={data == null}
className="flex-1"
/>
<Button
text={t("login.register")}
as={Link}
href={`/register?apiUrl=${apiUrl}`}
href={`/register?apiUrl=${data}`}
disabled={data == null}
className="flex-1"
/>
@ -60,8 +76,3 @@ export const ServerUrlPage = () => {
</View>
);
};
ServerUrlPage.query = {
path: ["api", "health"],
parser: null,
} satisfies QueryIdentifier;