Fix base uri handling

This commit is contained in:
Zoe Roux 2025-07-07 23:13:52 +02:00
parent c7103410dc
commit aa342cfbd3
No known key found for this signature in database
7 changed files with 22 additions and 17 deletions

View File

@ -43,7 +43,7 @@ export const User = z
...x,
logo: `auth/users/${x.id}/logo`,
// isVerified: x.permissions.length > 0,
// isAdmin: x.permissions?.includes("admin.write"),
isAdmin: true, //x.permissions?.includes("admin.write"),
}));
export type User = z.infer<typeof User>;
@ -54,5 +54,5 @@ export const Account = User.and(
token: z.string(),
selected: z.boolean(),
}),
).transform((x) => ({ ...x, isAdmin: true }));
);
export type Account = z.infer<typeof Account>;

View File

@ -20,8 +20,9 @@ function useLinkTo({
}) {
const router = useRouter();
// TODO: add href attr for web
return {
// @ts-expect-error href is not known
href: href,
onPress: (e) => {
if (e?.defaultPrevented) return;
if (href.startsWith("http")) {

View File

@ -6,7 +6,7 @@ export const AccountContext = createContext<{
authToken: string | null;
selectedAccount: Account | null;
accounts: (Account & { select: () => void; remove: () => void })[];
}>({ apiUrl: "", authToken: null, selectedAccount: null, accounts: [] });
}>({ apiUrl: "/", authToken: null, selectedAccount: null, accounts: [] });
export const useToken = () => {
const { apiUrl, authToken } = useContext(AccountContext);

View File

@ -1,6 +1,5 @@
import { useQueryClient } from "@tanstack/react-query";
import { type ReactNode, useEffect, useMemo, useRef } from "react";
import { Platform } from "react-native";
import { z } from "zod/v4";
import { Account, User } from "~/models";
import { useFetch } from "~/query";
@ -16,7 +15,7 @@ export const AccountProvider = ({ children }: { children: ReactNode }) => {
const ret = useMemo(() => {
const acc = accounts.find((x) => x.selected);
return {
apiUrl: acc?.apiUrl ?? "",
apiUrl: acc?.apiUrl ?? "/",
authToken: acc?.token ?? null,
selectedAccount: acc ?? null,
accounts: accounts.map((account) => ({

View File

@ -15,10 +15,9 @@ import { setServerData } from "~/utils";
const ssrApiUrl = process.env.KYOO_URL ?? "http://api:3567/api";
const cleanSlash = (str: string | null, keepFirst = false) => {
if (!str) return null;
if (keepFirst) return str.replace(/\/$/g, "");
return str.replace(/^\/|\/$/g, "");
const cleanSlash = (str: string | null) => {
if (str === null) return null;
return str.replace(/\/$/g, "");
};
export const queryFn = async <Parser extends z.ZodTypeAny>(context: {
@ -31,6 +30,7 @@ export const queryFn = async <Parser extends z.ZodTypeAny>(context: {
parser: Parser | null;
signal?: AbortSignal;
}): Promise<z.infer<Parser>> => {
console.log(context.url);
if (
Platform.OS === "web" &&
typeof window === "undefined" &&
@ -154,7 +154,7 @@ const toQueryKey = (query: {
};
}) => {
return [
cleanSlash(query.apiUrl, true),
cleanSlash(query.apiUrl),
...query.path,
query.params
? `?${Object.entries(query.params)
@ -162,7 +162,7 @@ const toQueryKey = (query: {
.map(([k, v]) => `${k}=${Array.isArray(v) ? v.join(",") : v}`)
.join("&")}`
: null,
].filter((x) => x);
].filter((x) => x !== undefined);
};
export const keyToUrl = (key: ReturnType<typeof toQueryKey>) => {

View File

@ -541,7 +541,9 @@ export const Header = ({
// studio={data.studio}
playHref={data.kind !== "collection" ? data.playHref : null}
trailerUrl={data.kind !== "collection" ? data.trailerUrl : null}
watchStatus={data.kind !== "collection" ? data.watchStatus?.status! : null}
watchStatus={
data.kind !== "collection" ? data.watchStatus?.status! : null
}
{...css({
marginTop: {
xs: max(vh(20), px(200)),
@ -596,10 +598,13 @@ export const Header = ({
);
};
Header.query = (kind: string, slug: string): QueryIdentifier<Show> => ({
Header.query = (
kind: "serie" | "movie" | "collection",
slug: string,
): QueryIdentifier<Show> => ({
parser: Show,
path: [kind, slug],
path: ["api", `${kind}s`, slug],
params: {
with: ["studio", "watchStatus"],
with: ["studios"],
},
});

View File

@ -20,7 +20,7 @@ export const login = async (
apiUrl: string | null;
},
): Promise<Result<Account, string>> => {
apiUrl ??= "";
apiUrl ??= "/";
try {
const controller = new AbortController();
setTimeout(() => controller.abort(), 5_000);