UNPKG

@sanity/preview-kit

Version:

General purpose utils for live content and visual editing

1 lines 3.91 kB
{"version":3,"file":"live-query.cjs","sources":["../src/live-query/LiveQuery.tsx"],"sourcesContent":["import type {QueryParams as ClientQueryParams} from '@sanity/client'\nimport type {LiveQueryClientComponentProps} from '@sanity/preview-kit/live-query/client-component'\nimport {Children, isValidElement, lazy} from 'react'\n\nconst ClientComponent = lazy(() => import('@sanity/preview-kit/live-query/client-component'))\n\n/** @public */\nexport interface LiveQueryProps<\n QueryResult,\n QueryParams extends ClientQueryParams = ClientQueryParams,\n> extends LiveQueryClientComponentProps<QueryResult, QueryParams> {\n enabled: boolean\n // eslint-disable-next-line no-warning-comments\n // @TODO fix typing of this\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n as?: React.ComponentType<any>\n}\n\nconst DEFAULT_PARAMS = {} as ClientQueryParams\n\n/** @public */\nexport function LiveQuery<QueryResult, QueryParams extends ClientQueryParams = ClientQueryParams>(\n props: LiveQueryProps<QueryResult, QueryParams>,\n): React.ReactNode {\n // Always passthrough when not enabled\n if (!props.enabled) {\n return props.children\n }\n\n const {query, params = DEFAULT_PARAMS, initialData, as: LiveComponent} = props\n // If we have an `as` prop it means we're likely working around a `children` that is RSC, and the `as` prop provides a Client Component\n if (LiveComponent) {\n if (Children.count(props.children) > 1) {\n throw new Error('LiveQuery: `as` prop can only be used with a single child')\n }\n if (!isValidElement(props.children)) {\n throw new Error('LiveQuery: `as` prop requires a valid `children` prop')\n }\n\n return (\n <ClientComponent\n // eslint-disable-next-line no-warning-comments\n // @TODO improve the typing of this\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initialData={initialData as any}\n query={query}\n // eslint-disable-next-line no-warning-comments\n // @TODO improve the typing of this\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params={params as any}\n throwOnMissingProvider={props.throwOnMissingProvider}\n >\n <LiveComponent\n {\n // eslint-disable-next-line no-warning-comments\n // @ts-expect-error -- @todo fix the typings\n ...props.children.props\n }\n />\n </ClientComponent>\n )\n }\n\n // Setup a `useLiveQuery` wrapper and override the `data` prop on the children component\n return (\n <ClientComponent\n // eslint-disable-next-line no-warning-comments\n // @TODO improve the typing of this\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n initialData={initialData as any}\n query={query}\n // eslint-disable-next-line no-warning-comments\n // @TODO improve the typing of this\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n params={params as any}\n >\n {props.children}\n </ClientComponent>\n )\n}\nLiveQuery.displayName = 'LiveQuery'\n"],"names":["lazy","Children","isValidElement","jsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAIA,MAAM,kBAAkBA,WAAK,MAAM,OAAO,iDAAiD,CAAC,GActF,iBAAiB,CAAC;AAGjB,SAAS,UACd,OACiB;AAEjB,MAAI,CAAC,MAAM;AACT,WAAO,MAAM;AAGf,QAAM,EAAC,OAAO,SAAS,gBAAgB,aAAa,IAAI,kBAAiB;AAEzE,MAAI,eAAe;AACjB,QAAIC,MAAS,SAAA,MAAM,MAAM,QAAQ,IAAI;AAC7B,YAAA,IAAI,MAAM,2DAA2D;AAEzE,QAAA,CAACC,MAAAA,eAAe,MAAM,QAAQ;AAC1B,YAAA,IAAI,MAAM,uDAAuD;AAIvE,WAAAC,2BAAA;AAAA,MAAC;AAAA,MAAA;AAAA,QAIC;AAAA,QACA;AAAA,QAIA;AAAA,QACA,wBAAwB,MAAM;AAAA,QAE9B,UAAAA,2BAAA;AAAA,UAAC;AAAA,UAAA;AAAA;AAAA;AAAA,YAIG,GAAG,MAAM,SAAS;AAAA,UAAA;AAAA,QAAA;AAAA,MAEtB;AAAA,IACF;AAAA,EAAA;AAMF,SAAAA,2BAAA;AAAA,IAAC;AAAA,IAAA;AAAA,MAIC;AAAA,MACA;AAAA,MAIA;AAAA,MAEC,UAAM,MAAA;AAAA,IAAA;AAAA,EACT;AAEJ;AACA,UAAU,cAAc;;;"}