mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-06-01 04:34:50 -04:00
Display a login button when the user is loggedout
This commit is contained in:
parent
e3be74d519
commit
98a0466761
@ -29,10 +29,15 @@ export type WithLoading<Item> =
|
|||||||
| (Item & { isLoading: false })
|
| (Item & { isLoading: false })
|
||||||
| (Partial<Item> & { isLoading: true });
|
| (Partial<Item> & { isLoading: true });
|
||||||
|
|
||||||
|
// We keep a Partial<Item> on the error value to allow destructuring.
|
||||||
|
export type WithError<Item> =
|
||||||
|
| (Item & { isError: false; error: undefined })
|
||||||
|
| (Partial<Item> & { isError: true; error: KyooErrors });
|
||||||
|
|
||||||
const isPage = <T = unknown,>(obj: unknown): obj is Page<T> =>
|
const isPage = <T = unknown,>(obj: unknown): obj is Page<T> =>
|
||||||
(typeof obj === "object" && obj && "items" in obj) || false;
|
(typeof obj === "object" && obj && "items" in obj) || false;
|
||||||
|
|
||||||
export const Fetch = <Data,>({
|
export const FetchNE = <Data,>({
|
||||||
query,
|
query,
|
||||||
placeholderCount = 1,
|
placeholderCount = 1,
|
||||||
children,
|
children,
|
||||||
@ -40,13 +45,16 @@ export const Fetch = <Data,>({
|
|||||||
query: QueryIdentifier<Data>;
|
query: QueryIdentifier<Data>;
|
||||||
placeholderCount?: number;
|
placeholderCount?: number;
|
||||||
children: (
|
children: (
|
||||||
item: Data extends Page<infer Item> ? WithLoading<Item> : WithLoading<Data>,
|
item: Data extends Page<infer Item>
|
||||||
|
? WithError<WithLoading<Item>>
|
||||||
|
: WithError<WithLoading<Data>>,
|
||||||
i: number,
|
i: number,
|
||||||
) => JSX.Element | null;
|
) => JSX.Element | null;
|
||||||
}): JSX.Element | null => {
|
}): JSX.Element | null => {
|
||||||
const { data, error } = useFetch(query);
|
const { data, error } = useFetch(query);
|
||||||
|
|
||||||
if (error) return <ErrorView error={error} />;
|
// @ts-ignore
|
||||||
|
if (error) return children({ isError: true, error }, 0);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
const placeholders = [...Array(placeholderCount)].map((_, i) =>
|
const placeholders = [...Array(placeholderCount)].map((_, i) =>
|
||||||
children({ isLoading: true } as any, i),
|
children({ isLoading: true } as any, i),
|
||||||
@ -58,6 +66,27 @@ export const Fetch = <Data,>({
|
|||||||
return <>{data.items.map((item, i) => children({ ...item, isLoading: false } as any, i))}</>;
|
return <>{data.items.map((item, i) => children({ ...item, isLoading: false } as any, i))}</>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const Fetch = <Data,>({
|
||||||
|
children,
|
||||||
|
...params
|
||||||
|
}: {
|
||||||
|
query: QueryIdentifier<Data>;
|
||||||
|
placeholderCount?: number;
|
||||||
|
children: (
|
||||||
|
item: Data extends Page<infer Item> ? WithLoading<Item> : WithLoading<Data>,
|
||||||
|
i: number,
|
||||||
|
) => JSX.Element | null;
|
||||||
|
}): JSX.Element | null => {
|
||||||
|
return (
|
||||||
|
<FetchNE {...params}>
|
||||||
|
{({ isError, error, ...item }, i) =>
|
||||||
|
// @ts-ignore
|
||||||
|
isError ? <ErrorView error={error} /> : children(item, i)
|
||||||
|
}
|
||||||
|
</FetchNE>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export const ErrorView = ({ error }: { error: KyooErrors }) => {
|
export const ErrorView = ({ error }: { error: KyooErrors }) => {
|
||||||
const { css } = useYoshiki();
|
const { css } = useYoshiki();
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ import { useRouter } from "solito/router";
|
|||||||
import { rem, Stylable, useYoshiki } from "yoshiki/native";
|
import { rem, Stylable, useYoshiki } from "yoshiki/native";
|
||||||
import Menu from "@material-symbols/svg-400/rounded/menu-fill.svg";
|
import Menu from "@material-symbols/svg-400/rounded/menu-fill.svg";
|
||||||
import Search from "@material-symbols/svg-400/rounded/search-fill.svg";
|
import Search from "@material-symbols/svg-400/rounded/search-fill.svg";
|
||||||
import { Fetch } from "../fetch";
|
import { Fetch, FetchNE } from "../fetch";
|
||||||
import { KyooLongLogo } from "./icon";
|
import { KyooLongLogo } from "./icon";
|
||||||
import { forwardRef, useRef, useState } from "react";
|
import { forwardRef, useRef, useState } from "react";
|
||||||
|
|
||||||
@ -93,7 +93,7 @@ export const NavbarProfile = () => {
|
|||||||
|
|
||||||
// TODO: show logged in user.
|
// TODO: show logged in user.
|
||||||
return (
|
return (
|
||||||
<Fetch query={MeQuery}>
|
<FetchNE query={MeQuery}>
|
||||||
{({ username }) => (
|
{({ username }) => (
|
||||||
<Link
|
<Link
|
||||||
href="/login"
|
href="/login"
|
||||||
@ -108,7 +108,7 @@ export const NavbarProfile = () => {
|
|||||||
/>
|
/>
|
||||||
</Link>
|
</Link>
|
||||||
)}
|
)}
|
||||||
</Fetch>
|
</FetchNE>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
export const NavbarRight = () => {
|
export const NavbarRight = () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user