Theme disabled buttons

This commit is contained in:
Zoe Roux 2024-03-06 22:02:18 +01:00
parent 3821950e49
commit a2a3134523
6 changed files with 52 additions and 29 deletions

View File

@ -49,7 +49,7 @@ export const queryFn = async <Parser extends z.ZodTypeAny>(
type?: Parser, type?: Parser,
token?: string | null, token?: string | null,
): Promise<z.infer<Parser>> => { ): Promise<z.infer<Parser>> => {
const url = context.apiUrl ? null : getCurrentApiUrl(); const url = context.apiUrl ?? getCurrentApiUrl();
if (token === undefined && context.authenticated !== false) token = await getToken(); if (token === undefined && context.authenticated !== false) token = await getToken();
const path = [url] const path = [url]
.concat( .concat(
@ -166,6 +166,7 @@ export type QueryIdentifier<T = unknown, Ret = T> = {
placeholderData?: T | (() => T); placeholderData?: T | (() => T);
enabled?: boolean; enabled?: boolean;
apiUrl?: string;
options?: Partial<Parameters<typeof queryFn>[0]>; options?: Partial<Parameters<typeof queryFn>[0]>;
}; };
@ -183,10 +184,10 @@ export type QueryPage<Props = {}, Items = unknown> = ComponentType<
export const toQueryKey = (query: { export const toQueryKey = (query: {
path: (string | undefined)[]; path: (string | undefined)[];
params?: { [query: string]: boolean | number | string | string[] | undefined }; params?: { [query: string]: boolean | number | string | string[] | undefined };
options?: { apiUrl?: string }; apiUrl?: string;
}) => { }) => {
return [ return [
query.options?.apiUrl, query.apiUrl,
...query.path, ...query.path,
query.params query.params
? "?" + ? "?" +
@ -195,7 +196,7 @@ export const toQueryKey = (query: {
.map(([k, v]) => `${k}=${Array.isArray(v) ? v.join(",") : v}`) .map(([k, v]) => `${k}=${Array.isArray(v) ? v.join(",") : v}`)
.join("&") .join("&")
: null, : null,
]; ].filter((x) => x);
}; };
export const useFetch = <Data,>(query: QueryIdentifier<Data>) => { export const useFetch = <Data,>(query: QueryIdentifier<Data>) => {

View File

@ -31,6 +31,7 @@ export const Button = forwardRef(function Button<AsProps = PressableProps>(
text, text,
icon, icon,
licon, licon,
disabled,
as, as,
...props ...props
}: { }: {
@ -38,6 +39,7 @@ export const Button = forwardRef(function Button<AsProps = PressableProps>(
text?: string; text?: string;
licon?: ReactElement | Falsy; licon?: ReactElement | Falsy;
icon?: ReactElement | Falsy; icon?: ReactElement | Falsy;
disabled?: boolean;
as?: ComponentType<AsProps>; as?: ComponentType<AsProps>;
} & AsProps, } & AsProps,
ref: ForwardedRef<unknown>, ref: ForwardedRef<unknown>,
@ -48,22 +50,35 @@ export const Button = forwardRef(function Button<AsProps = PressableProps>(
return ( return (
<Container <Container
ref={ref as any} ref={ref as any}
disabled={disabled}
{...(css( {...(css(
{ [
flexGrow: 0, {
flexDirection: "row", flexGrow: 0,
alignItems: "center", flexDirection: "row",
justifyContent: "center", alignItems: "center",
overflow: "hidden", justifyContent: "center",
p: ts(0.5), overflow: "hidden",
borderRadius: ts(5), p: ts(0.5),
borderColor: (theme: Theme) => theme.accent, borderRadius: ts(5),
borderWidth: ts(0.5), borderColor: (theme: Theme) => theme.accent,
fover: { borderWidth: ts(0.5),
self: { bg: (theme: Theme) => theme.accent }, fover: {
text: { color: (theme: Theme) => theme.colors.white }, self: { bg: (theme: Theme) => theme.accent },
text: { color: (theme: Theme) => theme.colors.white },
},
}, },
}, disabled && {
child: {
self: {
borderColor: (theme) => theme.overlay1,
},
text: {
color: (theme) => theme.overlay1,
},
},
},
],
props as any, props as any,
) as AsProps)} ) as AsProps)}
> >

View File

@ -44,7 +44,6 @@ export const LoginPage: QueryPage<{ apiUrl?: string; error?: string }> = ({
const { css } = useYoshiki(); const { css } = useYoshiki();
useEffect(() => { useEffect(() => {
console.log("login", apiUrl);
if (!apiUrl && Platform.OS !== "web") if (!apiUrl && Platform.OS !== "web")
router.replace("/server-url", undefined, { router.replace("/server-url", undefined, {
experimental: { nativeBehavior: "stack-replace", isNestedNavigator: false }, experimental: { nativeBehavior: "stack-replace", isNestedNavigator: false },

View File

@ -19,7 +19,7 @@
*/ */
import { QueryIdentifier, QueryPage, ServerInfo, ServerInfoP, useFetch } from "@kyoo/models"; import { QueryIdentifier, QueryPage, ServerInfo, ServerInfoP, useFetch } from "@kyoo/models";
import { Button, P, Input, ts, H1, HR, Link } from "@kyoo/primitives"; import { Button, P, Input, ts, H1, HR, Link, tooltip } from "@kyoo/primitives";
import { useState } from "react"; import { useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Platform, View } from "react-native"; import { Platform, View } from "react-native";
@ -42,11 +42,12 @@ const query: QueryIdentifier<ServerInfo> = {
export const ServerUrlPage: QueryPage = () => { export const ServerUrlPage: QueryPage = () => {
const [_apiUrl, setApiUrl] = useState(""); const [_apiUrl, setApiUrl] = useState("");
const apiUrl = cleanApiUrl(_apiUrl); const apiUrl = cleanApiUrl(_apiUrl);
const { data, error } = useFetch({ ...query, options: { apiUrl: apiUrl } }); const { data, error } = useFetch({ ...query, apiUrl });
const router = useRouter(); const router = useRouter();
const { t } = useTranslation(); const { t } = useTranslation();
const { css } = useYoshiki(); const { css } = useYoshiki();
console.log(data);
return ( return (
<View <View
{...css({ {...css({
@ -58,15 +59,18 @@ export const ServerUrlPage: QueryPage = () => {
<H1>{t("login.server")}</H1> <H1>{t("login.server")}</H1>
<View {...css({ justifyContent: "center" })}> <View {...css({ justifyContent: "center" })}>
<Input variant="big" onChangeText={setApiUrl} /> <Input variant="big" onChangeText={setApiUrl} />
<P {...css({ color: (theme: Theme) => theme.colors.red, alignSelf: "center" })}> {!data && (
{error?.errors[0] ?? " "} <P {...css({ color: (theme: Theme) => theme.colors.red, alignSelf: "center" })}>
</P> {error?.errors[0] ?? t("misc.loading")}
</P>
)}
</View> </View>
<View {...css({ marginTop: ts(5) })}> <View {...css({ marginTop: ts(5) })}>
<Button <Button
text={t("login.guest")} text={t("login.guest")}
onPress={() => {}} onPress={() => {}}
disabled={error != null || data?.allowGuests != true} disabled={data?.allowGuests != true}
{...(data?.allowGuests === false ? tooltip(t("login.guets-forbidden")) : {})}
/> />
<HR /> <HR />
<View {...css({ flexDirection: "row", gap: ts(2) })}> <View {...css({ flexDirection: "row", gap: ts(2) })}>
@ -75,7 +79,7 @@ export const ServerUrlPage: QueryPage = () => {
onPress={() => { onPress={() => {
router.push(`/login?apiUrl=${apiUrl}`); router.push(`/login?apiUrl=${apiUrl}`);
}} }}
disabled={error != null} disabled={data == null}
{...css({ flexGrow: 1, flexShrink: 1 })} {...css({ flexGrow: 1, flexShrink: 1 })}
/> />
<Button <Button
@ -83,7 +87,7 @@ export const ServerUrlPage: QueryPage = () => {
onPress={() => { onPress={() => {
router.push(`/register?apiUrl=${apiUrl}`); router.push(`/register?apiUrl=${apiUrl}`);
}} }}
disabled={error != null} disabled={data == null}
{...css({ flexGrow: 1, flexShrink: 1 })} {...css({ flexGrow: 1, flexShrink: 1 })}
/> />
</View> </View>

View File

@ -69,7 +69,8 @@
"expand": "Expand", "expand": "Expand",
"collapse": "Collapse", "collapse": "Collapse",
"edit": "Edit", "edit": "Edit",
"or": "OR" "or": "OR",
"loading": "Loading"
}, },
"navbar": { "navbar": {
"home": "Home", "home": "Home",
@ -167,6 +168,7 @@
"login": "Login", "login": "Login",
"register": "Register", "register": "Register",
"guest": "Continue as guest", "guest": "Continue as guest",
"guest-forbidden": "This instance of kyoo does not allow guests",
"via": "Continue with {{provider}}", "via": "Continue with {{provider}}",
"add-account": "Add account", "add-account": "Add account",
"logout": "Logout", "logout": "Logout",

View File

@ -69,7 +69,8 @@
"expand": "Développer", "expand": "Développer",
"collapse": "Replier", "collapse": "Replier",
"edit": "Changer", "edit": "Changer",
"or": "OU" "or": "OU",
"loading": "Chargement en cours"
}, },
"navbar": { "navbar": {
"home": "Accueil", "home": "Accueil",
@ -168,6 +169,7 @@
"via": "Continuer avec {{provider}}", "via": "Continuer avec {{provider}}",
"register": "Créer un compte", "register": "Créer un compte",
"guest": "Continuer en tant qu'invité", "guest": "Continuer en tant qu'invité",
"guest-forbidden": "Cette instance de kyoo n'autorise pas les comptes invités.",
"add-account": "Ajouter un compte", "add-account": "Ajouter un compte",
"logout": "Déconnexion", "logout": "Déconnexion",
"server": "Addresse du serveur", "server": "Addresse du serveur",