import { Fragment } from 'vue' import type { TanStackDevtoolsPluginProps } from '@tanstack/devtools' import type { AllowedComponentProps, Component, ComponentCustomProps, VNodeProps, } from 'vue' /** * The props a parent can pass to `TComponent`, without the ones Vue accepts on * every component (`key`, `ref`, `class`, `style`, ...). */ type VueComponentProps = TComponent extends new (...args: any) => { $props: infer TProps } ? { -readonly [ TKey in keyof TProps as TKey extends keyof (VNodeProps & AllowedComponentProps & ComponentCustomProps) ? never : TKey ]: TProps[TKey] } : TComponent extends (props: infer TProps, ...args: any) => any ? TProps : {} type InjectedKey = keyof TanStackDevtoolsPluginProps /** * Makes the props that `@tanstack/vue-devtools` passes from the devtools core * (`theme`, `devtoolsOpen`) optional, since they no longer need to be passed. */ type WithoutInjected = Omit & Partial>> type Simplify = { [TKey in keyof T]: T[TKey] } /** * `@tanstack/vue-devtools` renders the component with * `{ ...TanStackDevtoolsPluginProps, ...props }` and passes the same object as * `devtoolsProps`, so the props it expects there are required instead. */ type VuePluginProps< TComponent, TProps = VueComponentProps, > = Simplify< WithoutInjected> & (TProps extends { devtoolsProps?: infer TDevtoolsProps } ? unknown extends TDevtoolsProps ? {} : WithoutInjected> : {}) > export function createVuePlugin( name: string, component: TComponent, ) { function Plugin(props: VuePluginProps) { return { name, component, props, } } function NoOpPlugin(props: VuePluginProps) { return { name, component: Fragment, props, } } return [Plugin, NoOpPlugin] as const }