"use client"; import { createContext, useContext, useMemo } from "react"; import { useEffectOnce } from "usehooks-ts"; import type { ProviderProps } from "@/@types/types/provider-props.types"; import { cookieHelper } from "@/libs/helpers/cookie-helper"; import type { IClientConfiguration } from "@/models/client-configs.model"; import type { IClientDomainAddonResponse } from "@/server/services/addon.services"; import type { IPathResponse } from "@/server/services/path.services"; import type { StrategyKeysTypes } from "@/services/app-api/apis/strategy.services"; export interface IStrategyKeys { hotelList: string; hotelDetail: string; addToCart: string; createPost: string; selectList: string; requestChangeStatus: string; flightList: string; flightDetail: string; flightAddToCart: string; tourList: string; tourDetail: string; } interface IServerDataContext { strategyKeys: StrategyKeysTypes; clientToken: string; clientConfig?: IClientConfiguration; pathValue?: IPathResponse; addonList: IClientDomainAddonResponse[]; } const ServerDataContext = createContext( {} as IServerDataContext, ); interface ServerDataProviderProps extends ProviderProps { clientToken?: string; strategyKeys?: StrategyKeysTypes; clientConfig?: IClientConfiguration; addonList: IClientDomainAddonResponse[]; pathValue?: IPathResponse; } export const ServerDataProvider = ({ children, ...props }: ServerDataProviderProps) => { const providerValueMemoized = useMemo( () => ({ clientToken: props.clientToken!, strategyKeys: props.strategyKeys!, clientConfig: props.clientConfig, addonList: props.addonList, pathValue: props.pathValue, }), [ props.clientToken, props.strategyKeys, props.clientConfig, props.addonList, props.pathValue, ], ); useEffectOnce(() => { if (props.clientToken) { const clientTokenCookie = cookieHelper.getStr("client-token"); if (!clientTokenCookie || clientTokenCookie !== props.clientToken) { cookieHelper.setStr("client-token", props.clientToken); } } }); // if (!props.clientToken || !props.strategyKeys || !props.clientConfig) { // return
Error
; // } return ( {children} ); }; export const useServerDataCtx = () => useContext(ServerDataContext);