Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,122 changes: 1,488 additions & 1,634 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
},
"dependencies": {
"@croct/plug": "^0.20.0",
"@croct/sdk": "^0.19.0"
"@croct/plug": "https://pkg.pr.new/@croct/plug@358",
"@croct/sdk": "https://pkg.pr.new/@croct/sdk@485"
},
"devDependencies": {
"@babel/core": "^7.25.2",
Expand Down
61 changes: 39 additions & 22 deletions src/components/Slot/index.d.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,28 @@ describe('<Slot /> typing', () => {
return info.name;
}

it('should infer whether the schema is requested', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} schema>
{(params) => typeof params.metadata}
</Slot>;
`,
mapping: true,
};

expect(() => compileCode(code)).not.toThrow();

expect(getParameterType(code)).toBe(
'FetchResponse<boolean | HomeBannerV1, {children: {};id: "home-banner";initial: boolean;schema: true;}>',
);
});

it('should allow a renderer that accepts JSON objects or covariants for unmapped slots', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'}>
{(params: {foo: string}) => typeof params}
{(params: {content: {foo: string}}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -89,7 +106,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'}>
{(params: true) => typeof params}
{(params: {content: true}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -102,7 +119,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true}>
{(params: {foo: string}|boolean) => typeof params}
{(params: {content: {foo: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -115,7 +132,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true}>
{(params: {foo: string}) => typeof params}
{(params: {content: {foo: string}}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -128,7 +145,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} fallback={true}>
{(params: {foo: string}|boolean) => typeof params}
{(params: {content: {foo: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -141,7 +158,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} fallback={true}>
{(params: {foo: string}) => typeof params}
{(params: {content: {foo: string}}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -154,7 +171,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {foo: string}|boolean|number) => typeof params}
{(params: {content: {foo: string}|boolean|number}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -167,7 +184,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {foo: string}|boolean) => typeof params}
{(params: {content: {foo: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -180,7 +197,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {foo: string}|number) => typeof params}
{(params: {content: {foo: string}|number}) => typeof params.content}
</Slot>;
`,
mapping: false,
Expand All @@ -201,14 +218,14 @@ describe('<Slot /> typing', () => {

expect(() => compileCode(code)).not.toThrow();

expect(getParameterType(code)).toBe('HomeBannerV1');
expect(getParameterType(code)).toBe('FetchResponse<HomeBannerV1, FetchResponseOptions>');
});

it('should allow a covariant renderer parameter type for mapped slots', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'}>
{(params: {title: string}) => typeof params}
{(params: {content: {title: string}}) => typeof params}
</Slot>;
`,
mapping: true,
Expand All @@ -221,7 +238,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'}>
{(params: {foo: string}) => typeof params}
{(params: {content: {foo: string}}) => typeof params}
</Slot>;
`,
mapping: true,
Expand All @@ -242,14 +259,14 @@ describe('<Slot /> typing', () => {

expect(() => compileCode(code)).not.toThrow();

expect(getParameterType(code)).toBe('boolean | HomeBannerV1');
expect(getParameterType(code)).toBe('FetchResponse<boolean | HomeBannerV1, FetchResponseOptions>');
});

it('should allow a renderer that accepts the initial value for mapped slots', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true}>
{(params: {title: string}|boolean) => typeof params}
{(params: {content: {title: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -262,7 +279,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true}>
{(params: {title: string}) => typeof params}
{(params: {content: {title: string}}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -283,14 +300,14 @@ describe('<Slot /> typing', () => {

expect(() => compileCode(code)).not.toThrow();

expect(getParameterType(code)).toBe('boolean | HomeBannerV1');
expect(getParameterType(code)).toBe('FetchResponse<boolean | HomeBannerV1, FetchResponseOptions>');
});

it('should allow a renderer that accepts the fallback value for mapped slots', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} fallback={true}>
{(params: {title: string}|boolean) => typeof params}
{(params: {content: {title: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -303,7 +320,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} fallback={true}>
{(params: {title: string}) => typeof params}
{(params: {content: {title: string}}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -324,14 +341,14 @@ describe('<Slot /> typing', () => {

expect(() => compileCode(code)).not.toThrow();

expect(getParameterType(code)).toBe('number | boolean | HomeBannerV1');
expect(getParameterType(code)).toBe('FetchResponse<number | boolean | HomeBannerV1, FetchResponseOptions>');
});

it('should allow a renderer that accepts both the initial and fallback values for mapped slots', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {title: string}|boolean|number) => typeof params}
{(params: {content: {title: string}|boolean|number}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -344,7 +361,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {title: string}|boolean) => typeof params}
{(params: {content: {title: string}|boolean}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand All @@ -357,7 +374,7 @@ describe('<Slot /> typing', () => {
const code: CodeOptions = {
code: `
<Slot id={'home-banner'} initial={true} fallback={1}>
{(params: {title: string}|number) => typeof params}
{(params: {content: {title: string}|number}) => typeof params.content}
</Slot>;
`,
mapping: true,
Expand Down
15 changes: 11 additions & 4 deletions src/components/Slot/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {render, screen} from '@testing-library/react';
import {Slot, SlotProps} from './index';
import {useContent} from '../../hooks';
import {FetchResponse, useContent} from '../../hooks';
import '@testing-library/jest-dom';

jest.mock(
Expand All @@ -14,11 +14,18 @@ describe('<Slot />', () => {
it('should fetch and render a slot', () => {
const {id, children, ...options}: SlotProps<{title: string}> = {
id: 'home-banner',
children: jest.fn(({title}) => title),
children: jest.fn(({content: {title}}) => title),
fallback: {title: 'fallback'},
};

const result = {title: 'result'};
const result = {
metadata: {
version: '1.0',
},
content: {
title: 'result',
},
} satisfies FetchResponse;

jest.mocked(useContent).mockReturnValue(result);

Expand All @@ -30,6 +37,6 @@ describe('<Slot />', () => {

expect(useContent).toHaveBeenCalledWith(id, options);
expect(children).toHaveBeenCalledWith(result);
expect(screen.getByText(result.title)).toBeInTheDocument();
expect(screen.getByText(result.content.title)).toBeInTheDocument();
});
});
39 changes: 27 additions & 12 deletions src/components/Slot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
'use client';

import {Fragment, ReactElement, ReactNode} from 'react';
import {SlotContent, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
import {DynamicSlotId, SlotContent, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
import {JsonObject} from '@croct/plug/sdk/json';
import {useContent, UseContentOptions} from '../../hooks';
import {FetchResponseOptions} from '@croct/sdk/contentFetcher';
import {FetchResponse, useContent, UseContentOptions} from '../../hooks';

type Renderer<P> = (props: P) => ReactNode;

export type SlotProps<P, I = P, F = P, S extends VersionedSlotId = VersionedSlotId> = UseContentOptions<I, F> & {
export type SlotProps<
P,
I = P,
F = P,
S extends VersionedSlotId = VersionedSlotId,
O extends FetchResponseOptions = FetchResponseOptions
> = O & UseContentOptions<I, F> & {
id: S,
children: Renderer<P | I | F>,
children: Renderer<FetchResponse<P | I | F, O>>,
};

type SlotComponent = {
<P, I, F>(
<P, I, F, O extends FetchResponseOptions>(
props:
Extract<P | I | F, JsonObject> extends never
? SlotProps<JsonObject, never, never, keyof VersionedSlotMap extends never ? string : never>
: SlotProps<P, I, F, keyof VersionedSlotMap extends never ? string : never>
? SlotProps<JsonObject, never, never, keyof VersionedSlotMap extends never ? string : never, O>
: SlotProps<P, I, F, keyof VersionedSlotMap extends never ? string : never, O>
): ReactElement,

<S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, never, never, S>): ReactElement,
<S extends VersionedSlotId, O extends FetchResponseOptions>(
props: SlotProps<SlotContent<S>, never, never, S, O>
): ReactElement,

<I, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, I, never, S>): ReactElement,
<I, S extends VersionedSlotId, O extends FetchResponseOptions>(
props: SlotProps<SlotContent<S>, I, never, S, O>
): ReactElement,

<F, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, never, F, S>): ReactElement,
<F, S extends VersionedSlotId, O extends FetchResponseOptions>(
props: SlotProps<SlotContent<S>, never, F, S, O>
): ReactElement,

<I, F, S extends VersionedSlotId>(props: SlotProps<SlotContent<S>, I, F, S>): ReactElement,
<I, F, S extends VersionedSlotId, O extends FetchResponseOptions>(
props: SlotProps<SlotContent<S>, I, F, S, O>
): ReactElement,

(props: SlotProps<void, void, void>): ReactElement,
};

export const Slot: SlotComponent = <I, F>(props: SlotProps<JsonObject, I, F>): ReactElement => {
const {id, children, ...options} = props;
const data = useContent(id, options);
const data = useContent<DynamicSlotId>(id, options);

return <Fragment>{children(data)}</Fragment>;
};
Loading
Loading