Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(headless,headless-react): make ssr terminology more consistent #3159

Merged
merged 9 commits into from
Sep 13, 2023
34 changes: 17 additions & 17 deletions packages/headless-react/src/search-engine.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Headless react SSR utils', () => {
build,
useEngine,
controllers,
SSRStateProvider,
InitialStateProvider,
CSRProvider,
...rest
} = defineSearchEngine({
Expand All @@ -38,7 +38,7 @@ describe('Headless react SSR utils', () => {
hydrateInitialState,
build,
useEngine,
SSRStateProvider,
InitialStateProvider,
CSRProvider,
].forEach((returnValue) => expect(typeof returnValue).toBe('function'));

Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Headless react SSR utils', () => {
const {
fetchInitialState,
hydrateInitialState,
SSRStateProvider,
InitialStateProvider,
CSRProvider,
controllers,
useEngine,
Expand Down Expand Up @@ -121,19 +121,19 @@ describe('Headless react SSR utils', () => {
});

test('should render with SSRProvider', async () => {
btaillon-coveo marked this conversation as resolved.
Show resolved Hide resolved
const ssrState = await fetchInitialState();
const initialState = await fetchInitialState();
render(
<SSRStateProvider controllers={ssrState.controllers}>
<InitialStateProvider controllers={initialState.controllers}>
<TestResultList />
</SSRStateProvider>
</InitialStateProvider>
);

await checkRenderedResultList();
});

test('should hydrate results with CSRProvider', async () => {
const ssrState = await fetchInitialState();
const {engine, controllers} = await hydrateInitialState(ssrState);
const initialState = await fetchInitialState();
const {engine, controllers} = await hydrateInitialState(initialState);

render(
<CSRProvider engine={engine} controllers={controllers}>
Expand All @@ -153,25 +153,25 @@ describe('Headless react SSR utils', () => {
});

test('should not return engine with SSRProvider', async () => {
const ssrState = await fetchInitialState();
function ssrStateProviderWrapper({children}: PropsWithChildren) {
const initialState = await fetchInitialState();
function initialStateProviderWrapper({children}: PropsWithChildren) {
return (
<SSRStateProvider controllers={ssrState.controllers}>
<InitialStateProvider controllers={initialState.controllers}>
{children}
</SSRStateProvider>
</InitialStateProvider>
);
}

const {result} = renderHook(() => useEngine(), {
wrapper: ssrStateProviderWrapper,
wrapper: initialStateProviderWrapper,
});
expect(result.current).toBeUndefined();
});

test('should return engine with CSRProvider', async () => {
const ssrState = await fetchInitialState();
const {engine, controllers} = await hydrateInitialState(ssrState);
function csrStateProviderWrapper({children}: PropsWithChildren) {
const initialState = await fetchInitialState();
const {engine, controllers} = await hydrateInitialState(initialState);
function hydratedStateProviderWrapper({children}: PropsWithChildren) {
return (
<CSRProvider controllers={controllers} engine={engine}>
{children}
Expand All @@ -180,7 +180,7 @@ describe('Headless react SSR utils', () => {
}

const {result} = renderHook(() => useEngine(), {
wrapper: csrStateProviderWrapper,
wrapper: hydratedStateProviderWrapper,
});
expect(result.current).toStrictEqual(engine);
});
Expand Down
15 changes: 9 additions & 6 deletions packages/headless-react/src/search-engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {useContext, useCallback, useMemo, Context} from 'react';
import React from 'react';
import {useSyncMemoizedStore} from './client-utils';
import {
ContextCSRState,
ContextHydratedState,
ContextState,
ControllerHook,
InferControllerHooksMapFromDefinition,
Expand All @@ -20,7 +20,7 @@ import {SingletonGetter, capitalize, singleton, mapObject} from './utils';

export class MissingEngineProviderError extends Error {
static message =
'Unable to find Context. Please make sure you are wrapping your component with either `SSRStateProvider` or `CSRProvider` component that can provide the required context.';
'Unable to find Context. Please make sure you are wrapping your component with either `InitialStateProvider` or `CSRProvider` component that can provide the required context.';
btaillon-coveo marked this conversation as resolved.
Show resolved Hide resolved
constructor() {
super(MissingEngineProviderError.message);
}
Expand All @@ -40,7 +40,7 @@ function isCSRContext<
TControllers extends ControllerDefinitionsMap<TEngine, Controller>,
>(
ctx: ContextState<TEngine, TControllers>
): ctx is ContextCSRState<TEngine, TControllers> {
): ctx is ContextHydratedState<TEngine, TControllers> {
btaillon-coveo marked this conversation as resolved.
Show resolved Hide resolved
return 'engine' in ctx;
}

Expand All @@ -64,8 +64,11 @@ function buildControllerHook<
isCSRContext(ctx) ? ctx.controllers[key].subscribe(listener) : () => {},
[ctx]
);
const getSSRState = useCallback(() => ctx.controllers[key].state, [ctx]);
const state = useSyncMemoizedStore(subscribe, getSSRState);
const getInitialState = useCallback(
() => ctx.controllers[key].state,
[ctx]
);
const state = useSyncMemoizedStore(subscribe, getInitialState);
const methods = useMemo(() => {
if (!isCSRContext(ctx)) {
return undefined;
Expand Down Expand Up @@ -108,7 +111,7 @@ export function defineSearchEngine<
])
)
: {}) as InferControllerHooksMapFromDefinition<TControllers>,
SSRStateProvider: ({controllers, children}) => {
InitialStateProvider: ({controllers, children}) => {
const {Provider} = singletonContext.get();
return <Provider value={{controllers}}>{children}</Provider>;
},
Expand Down
16 changes: 8 additions & 8 deletions packages/headless-react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import {InferControllerFromDefinition} from '@coveo/headless/dist/definitions/ap
import {
ControllerDefinitionsMap,
InferControllersMapFromDefinition,
InferControllerSSRStateMapFromDefinitions,
InferControllerInitialStateMapFromDefinitions,
EngineDefinition,
} from '@coveo/headless/ssr';
import {FunctionComponent, PropsWithChildren} from 'react';

export type ContextSSRState<
export type ContextInitialState<
TEngine extends CoreEngine,
TControllers extends ControllerDefinitionsMap<TEngine, Controller>,
> = {controllers: InferControllerSSRStateMapFromDefinitions<TControllers>};
> = {controllers: InferControllerInitialStateMapFromDefinitions<TControllers>};

export type ContextCSRState<
export type ContextHydratedState<
TEngine extends CoreEngine,
TControllers extends ControllerDefinitionsMap<TEngine, Controller>,
> = {
Expand All @@ -30,8 +30,8 @@ export type ContextState<
TEngine extends CoreEngine,
TControllers extends ControllerDefinitionsMap<TEngine, Controller>,
> =
| ContextSSRState<TEngine, TControllers>
| ContextCSRState<TEngine, TControllers>;
| ContextInitialState<TEngine, TControllers>
| ContextHydratedState<TEngine, TControllers>;

export type ControllerHook<TController extends Controller> = () => {
state: TController['state'];
Expand All @@ -53,9 +53,9 @@ export type ReactEngineDefinition<
> = EngineDefinition<TEngine, TControllers, TEngineOptions> & {
controllers: InferControllerHooksMapFromDefinition<TControllers>;
useEngine(): TEngine | undefined;
SSRStateProvider: FunctionComponent<
InitialStateProvider: FunctionComponent<
PropsWithChildren<{
controllers: InferControllerSSRStateMapFromDefinitions<TControllers>;
controllers: InferControllerInitialStateMapFromDefinitions<TControllers>;
}>
>;
CSRProvider: FunctionComponent<
btaillon-coveo marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ describe('SSR', () => {

it('fetches initial state of engine', async () => {
const {fetchInitialState} = engineDefinition;
const engineSSRState = await fetchInitialState();
expect(engineSSRState).toBeTruthy();
const engineInitialState = await fetchInitialState();
expect(engineInitialState).toBeTruthy();
});

it('hydrates engine and fetches results using hydrated engine', async () => {
const {fetchInitialState, hydrateInitialState} = engineDefinition;
const engineSSRState = await fetchInitialState();
const {engine} = await hydrateInitialState(engineSSRState);
const engineInitialState = await fetchInitialState();
const {engine} = await hydrateInitialState(engineInitialState);
expect(engine.state.configuration.organizationId).toEqual(
getSampleSearchEngineConfiguration().organizationId
);
Expand Down
10 changes: 5 additions & 5 deletions packages/headless/src/app/search-engine/search-engine.ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {EngineDefinitionBuildOptionsWithProps} from '../ssr-engine/types/build';
import {
ControllerDefinitionsMap,
ControllersMap,
EngineSSRState,
InferControllerSSRStateMapFromDefinitions,
EngineInitialState,
InferControllerInitialStateMapFromDefinitions,
InferControllersMapFromDefinition,
OptionsExtender,
} from '../ssr-engine/types/common';
Expand Down Expand Up @@ -91,9 +91,9 @@ export function defineSearchEngine<
>
) =>
new Promise<
EngineSSRState<
EngineInitialState<
{type: string},
InferControllerSSRStateMapFromDefinitions<TControllerDefinitions>
InferControllerInitialStateMapFromDefinitions<TControllerDefinitions>
>
>((resolve, reject) => {
let initialControllers: ControllersMap;
Expand All @@ -103,7 +103,7 @@ export function defineSearchEngine<
resolve({
controllers: mapObject(initialControllers, (controller) => ({
state: controller.state,
})) as InferControllerSSRStateMapFromDefinitions<TControllerDefinitions>,
})) as InferControllerInitialStateMapFromDefinitions<TControllerDefinitions>,
searchFulfilledAction: JSON.parse(JSON.stringify(action)),
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/headless/src/app/ssr-engine/types/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CoreEngine} from '../../engine';
import {
ControllersMap,
ControllersPropsMap,
EngineAndControllers,
HydratedState,
OptionsExtender,
} from './common';

Expand Down Expand Up @@ -31,7 +31,7 @@ export interface BuildWithProps<
TEngineOptions,
TControllersProps
>
): Promise<EngineAndControllers<TEngine, TControllersMap>>;
): Promise<HydratedState<TEngine, TControllersMap>>;
}

export interface BuildWithoutProps<
Expand All @@ -44,5 +44,5 @@ export interface BuildWithoutProps<
*/
build(
options?: EngineDefinitionBuildOptionsWithoutProps<TEngineOptions>
): Promise<EngineAndControllers<TEngine, TControllersMap>>;
): Promise<HydratedState<TEngine, TControllersMap>>;
}
20 changes: 10 additions & 10 deletions packages/headless/src/app/ssr-engine/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export interface ControllersMap {
[customName: string]: Controller;
}

export interface ControllerSSRState<TState> {
export interface ControllerInitialState<TState> {
state: TState;
}

export interface ControllerSSRStateMap {
[customName: string]: ControllerSSRState<unknown>;
export interface ControllerInitialStateMap {
[customName: string]: ControllerInitialState<unknown>;
}
/**
* @internal
Expand Down Expand Up @@ -68,15 +68,15 @@ export interface ControllerDefinitionsMap<
[customName: string]: ControllerDefinition<TEngine, TController>;
}

export interface EngineSSRState<
export interface EngineInitialState<
TSearchFulfilledAction extends AnyAction,
TControllers extends ControllerSSRStateMap
TControllers extends ControllerInitialStateMap
> {
searchFulfilledAction: TSearchFulfilledAction;
controllers: TControllers;
}

export interface EngineAndControllers<
export interface HydratedState<
btaillon-coveo marked this conversation as resolved.
Show resolved Hide resolved
TEngine extends CoreEngine,
TControllers extends ControllersMap
> {
Expand Down Expand Up @@ -122,16 +122,16 @@ export type InferControllersMapFromDefinition<
TControllers extends ControllerDefinitionsMap<CoreEngine, Controller>
> = {[K in keyof TControllers]: InferControllerFromDefinition<TControllers[K]>};

export type InferControllerSSRStateFromDefinition<
export type InferControllerInitialStateFromDefinition<
TDefinition extends ControllerDefinition<CoreEngine, Controller>
> = TDefinition extends ControllerDefinition<infer _, infer TController>
? ControllerSSRState<TController['state']>
? ControllerInitialState<TController['state']>
: never;

export type InferControllerSSRStateMapFromDefinitions<
export type InferControllerInitialStateMapFromDefinitions<
TControllers extends ControllerDefinitionsMap<CoreEngine, Controller>
> = {
[K in keyof TControllers]: InferControllerSSRStateFromDefinition<
[K in keyof TControllers]: InferControllerInitialStateFromDefinition<
TControllers[K]
>;
};
16 changes: 8 additions & 8 deletions packages/headless/src/app/ssr-engine/types/core-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
ControllersPropsMap,
HasKeys,
InferControllerPropsMapFromDefinitions,
InferControllerSSRStateMapFromDefinitions,
InferControllerInitialStateMapFromDefinitions,
InferControllersMapFromDefinition,
ControllerSSRStateMap,
ControllerInitialStateMap,
ControllersMap,
} from './common';
import {
Expand Down Expand Up @@ -59,7 +59,7 @@ export interface EngineDefinitionWithoutProps<
TControllers extends ControllerDefinitionsMap<TEngine, Controller>,
TEngineOptions
> extends FetchInitialStateWithoutProps<
InferControllerSSRStateMapFromDefinitions<TControllers>,
InferControllerInitialStateMapFromDefinitions<TControllers>,
AnyAction
>,
HydrateInitialStateWithoutProps<
Expand All @@ -79,7 +79,7 @@ export interface EngineDefinitionWithProps<
TEngineOptions,
TControllerProps extends ControllersPropsMap
> extends FetchInitialStateWithProps<
InferControllerSSRStateMapFromDefinitions<TControllers>,
InferControllerInitialStateMapFromDefinitions<TControllers>,
AnyAction,
TControllerProps
>,
Expand All @@ -99,19 +99,19 @@ export interface EngineDefinitionWithProps<
/**
* @internal
*/
export type InferSSRState<
export type InferInitialState<
T extends
| FetchInitialStateWithoutProps<ControllerSSRStateMap, AnyAction>
| FetchInitialStateWithoutProps<ControllerInitialStateMap, AnyAction>
| FetchInitialStateWithProps<
ControllerSSRStateMap,
ControllerInitialStateMap,
AnyAction,
ControllersPropsMap
>
> = Awaited<ReturnType<T['fetchInitialState']>>;
/**
* @internal
*/
export type InferCSRState<
export type InferHydratedState<
T extends
| HydrateInitialStateWithoutProps<CoreEngine, ControllersMap, AnyAction>
| HydrateInitialStateWithProps<
Expand Down
Loading
Loading