'use client'
import stringify from "json-stable-stringify";
import md5 from "md5";
import type { PropsWithChildren } from "react";
import useSWR from "swr";
import type { BareFetcher, SWRConfiguration } from "swr/_internal";
export default function UseSWRComponent<
P extends any = any,
Props extends PropsWithChildren
= PropsWithChildren
,
ReactNode extends React.ReactNode = React.ReactNode,
Component extends (props: Props) => Promise | ReactNode = (
props: Props
) => Promise | ReactNode,
Error = any
>({
key,
props,
Component,
children,
Loading,
Validating,
Error,
...conf
}: {
key?: string;
props: Props;
/** could be ServerComponent or ClientComponent */
Component: Component;
Loading?: (props: { children?: ReactNode }) => ReactNode;
Validating?: (props: { children?: ReactNode }) => ReactNode;
Error?: (props: { children?: ReactNode; error: any }) => ReactNode;
children: SWRConfiguration<
ReactNode,
Error,
BareFetcher
>["fallbackData"];
} & SWRConfiguration>) {
key ??= "SWRCOMP:" + md5(stringify(props) ?? '');
conf.fallbackData ??= children;
const { data, isLoading, isValidating, error } = useSWR(
key,
() => Component(props),
conf
);
if (error && Error) return {data};
if (isLoading && Loading) return {data};
if (isValidating && Validating) return {data};
return data;
}