diff --git a/front/packages/ui/src/fetch.tsx b/front/packages/ui/src/fetch.tsx index 15942858..ee4eaa89 100644 --- a/front/packages/ui/src/fetch.tsx +++ b/front/packages/ui/src/fetch.tsx @@ -29,10 +29,15 @@ export type WithLoading = | (Item & { isLoading: false }) | (Partial & { isLoading: true }); +// We keep a Partial on the error value to allow destructuring. +export type WithError = + | (Item & { isError: false; error: undefined }) + | (Partial & { isError: true; error: KyooErrors }); + const isPage = (obj: unknown): obj is Page => (typeof obj === "object" && obj && "items" in obj) || false; -export const Fetch = ({ +export const FetchNE = ({ query, placeholderCount = 1, children, @@ -40,13 +45,16 @@ export const Fetch = ({ query: QueryIdentifier; placeholderCount?: number; children: ( - item: Data extends Page ? WithLoading : WithLoading, + item: Data extends Page + ? WithError> + : WithError>, i: number, ) => JSX.Element | null; }): JSX.Element | null => { const { data, error } = useFetch(query); - if (error) return ; + // @ts-ignore + if (error) return children({ isError: true, error }, 0); if (!data) { const placeholders = [...Array(placeholderCount)].map((_, i) => children({ isLoading: true } as any, i), @@ -58,6 +66,27 @@ export const Fetch = ({ return <>{data.items.map((item, i) => children({ ...item, isLoading: false } as any, i))}; }; +export const Fetch = ({ + children, + ...params +}: { + query: QueryIdentifier; + placeholderCount?: number; + children: ( + item: Data extends Page ? WithLoading : WithLoading, + i: number, + ) => JSX.Element | null; +}): JSX.Element | null => { + return ( + + {({ isError, error, ...item }, i) => + // @ts-ignore + isError ? : children(item, i) + } + + ); +}; + export const ErrorView = ({ error }: { error: KyooErrors }) => { const { css } = useYoshiki(); diff --git a/front/packages/ui/src/navbar/index.tsx b/front/packages/ui/src/navbar/index.tsx index dada4964..bc84c38e 100644 --- a/front/packages/ui/src/navbar/index.tsx +++ b/front/packages/ui/src/navbar/index.tsx @@ -37,7 +37,7 @@ import { useRouter } from "solito/router"; import { rem, Stylable, useYoshiki } from "yoshiki/native"; import Menu from "@material-symbols/svg-400/rounded/menu-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 { forwardRef, useRef, useState } from "react"; @@ -93,7 +93,7 @@ export const NavbarProfile = () => { // TODO: show logged in user. return ( - + {({ username }) => ( { /> )} - + ); }; export const NavbarRight = () => {