You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { Reducer, useMemo, useReducer } from "preact/hooks";
|
|
import { MakeClient, assert, makeClient } from "../common/deps/yaypi.ts";
|
|
import { Api } from "../common/api.ts";
|
|
|
|
export type Client = MakeClient<typeof Api>;
|
|
|
|
export type Loadable<T, E = string> =
|
|
| null
|
|
| { loading: true }
|
|
| { loading: boolean; data: T }
|
|
| { loading: boolean; error: string };
|
|
export const isLoading = <T>(loadable: Loadable<T>) => loadable?.loading;
|
|
|
|
export type Data = {
|
|
schemas: Loadable<ReturnType<Client["v1"]["listSchemas"]>>;
|
|
};
|
|
export type Action = never;
|
|
|
|
const reduce: Reducer<Data, Action> = (data, action) => {
|
|
return data;
|
|
};
|
|
|
|
const emptyData: Data = {
|
|
schemas: null,
|
|
};
|
|
|
|
// NOTE: this should only be used in App.ts
|
|
export const useFetch = () => {
|
|
const client = useMemo(() => makeClient(Api, "/api"), []);
|
|
const [data, reducer] = useReducer(reduce, emptyData);
|
|
|
|
const listSchemas = async (
|
|
...args: Parameters<typeof client.v1.listSchemas>
|
|
) => {
|
|
const schemas = assert(await client.v1.listSchemas(...args));
|
|
};
|
|
};
|