diff --git a/README.md b/README.md index d38bece..f4fcb26 100644 --- a/README.md +++ b/README.md @@ -328,14 +328,14 @@ export const { controller, usePromoManager, useActivePromo } = createPromoManage import { usePromoManager } from './promo-manager'; -const { status, requestStart, cancelStart } = usePromoManager('issuePoll'); +const { status, requestStart, skipPromo } = usePromoManager('issuePoll'); useMount(() => { requestStart(); }); useUnmount(() => { - cancelStart(); + skipPromo(); }); if(status === 'active') { diff --git a/src/promo-manager/core/controller.ts b/src/promo-manager/core/controller.ts index f6d0e2e..669f22b 100644 --- a/src/promo-manager/core/controller.ts +++ b/src/promo-manager/core/controller.ts @@ -9,9 +9,7 @@ import type { InitPromoManagerOptions, Nullable, ProgressInfoConfig, - Promo, PromoBaseState as BaseState, - PromoGroup, PromoGroupSlug, PromoManagerStatus, PromoOptions, @@ -219,7 +217,7 @@ export class Controller { this.updateProgressInfo(slug); }; - cancelStart = (slug: Nullable) => { + skipPromo = (slug: Nullable) => { this.logger.debug('Skip promo run', slug); if (!slug) { return; @@ -260,28 +258,12 @@ export class Controller { return 'canRun'; }; - getFirstAvailablePromoByType = (slug: PromoGroupSlug): Nullable => { - const promoGroup = Object.values(this.options.config.promoGroups).find( - (currentPromoGroup: PromoGroup) => currentPromoGroup.slug === slug, - ); - - if (!promoGroup) { - return null; - } - - return ( - promoGroup.promos.find( - (promo: Promo) => this.isAbleToRun(promo.slug) || this.isPending(promo.slug), - )?.slug ?? null - ); - }; - - getActivePromo = (promoType?: PromoGroupSlug): Nullable => { + getActivePromo = (promoGroup?: PromoGroupSlug): Nullable => { const activePromo = this.state.base.activePromo; - if (!promoType) return activePromo; + if (!promoGroup) return activePromo; - return this.getGroupBySlug(activePromo) === promoType ? activePromo : null; + return this.getGroupBySlug(activePromo) === promoGroup ? activePromo : null; }; subscribe = (listener: Listener) => { @@ -490,7 +472,7 @@ export class Controller { const result = await this.requestStart(stepData.preset); if (!result) { - this.cancelStart(stepData.preset); + this.skipPromo(stepData.preset); } return result; }, @@ -507,7 +489,7 @@ export class Controller { instance.events.subscribe('closeHint', async ({hint}: OnboardingEventsMap['closeHint']) => { if (this.promoPresets.has(hint.preset)) { - this.cancelStart(hint.preset); + this.skipPromo(hint.preset); } }); diff --git a/src/promo-manager/core/getHooks.ts b/src/promo-manager/core/getHooks.ts index 1f3c676..bdd308e 100644 --- a/src/promo-manager/core/getHooks.ts +++ b/src/promo-manager/core/getHooks.ts @@ -4,64 +4,59 @@ import type {Controller} from './controller'; import type {PromoGroupSlug, PromoSlug} from './types'; export function getHooks(controller: Controller) { - const usePromoManager = (promo: PromoSlug) => { - const status = useSyncExternalStore(controller.subscribe, () => - controller.getPromoStatus(promo), + const usePromoManager = () => { + const activePromo = useSyncExternalStore( + controller.subscribe, + () => controller.state.base.activePromo, ); return useMemo( () => ({ - status, - requestStart: () => { - controller.requestStart(promo).catch((error) => { - controller.logger.error(error); - }); - }, - finish: (closeActiveTimeout?: number) => - controller.finishPromo(promo, closeActiveTimeout), - cancel: (closeActiveTimeout?: number) => - controller.cancelPromo(promo, closeActiveTimeout), - cancelStart: () => controller.cancelStart(promo), - updateProgressInfo: () => controller.updateProgressInfo(promo), + activePromo, + requestStartPromo: controller.requestStart, + finisPromo: controller.finishPromo, + cancelPromo: controller.cancelPromo, + skipPromo: controller.skipPromo, }), - [promo, status], + [activePromo], ); }; - const useActivePromo = (promoGroupSlug?: PromoGroupSlug) => { - const promo = useSyncExternalStore(controller.subscribe, () => - controller.getActivePromo(promoGroupSlug), + const usePromo = (promo: PromoSlug) => { + const status = useSyncExternalStore(controller.subscribe, () => + controller.getPromoStatus(promo), ); return useMemo( () => ({ - promo, - promoGroup: controller.getGroupBySlug(promo), - metaInfo: controller.getPromoMeta(promo), + status, + requestStart: () => controller.requestStart(promo), finish: (closeActiveTimeout?: number) => controller.finishPromo(promo, closeActiveTimeout), cancel: (closeActiveTimeout?: number) => controller.cancelPromo(promo, closeActiveTimeout), - cancelStart: () => controller.cancelStart(promo), - updateProgressInfo: () => controller.updateProgressInfo(promo), + skip: () => controller.skipPromo(promo), }), - [promo], + [status], ); }; - const useAvailablePromo = (type: PromoGroupSlug) => { + const useActivePromo = (promoGroupSlug?: PromoGroupSlug) => { const promo = useSyncExternalStore(controller.subscribe, () => - controller.getFirstAvailablePromoByType(type), + controller.getActivePromo(promoGroupSlug), ); return useMemo( () => ({ promo, - requestStart: () => controller.requestStart(promo), + promoGroup: controller.getGroupBySlug(promo), + metaInfo: controller.getPromoMeta(promo), finish: (closeActiveTimeout?: number) => controller.finishPromo(promo, closeActiveTimeout), cancel: (closeActiveTimeout?: number) => controller.cancelPromo(promo, closeActiveTimeout), + skipPromo: () => controller.skipPromo(promo), + updateProgressInfo: () => controller.updateProgressInfo(promo), }), [promo], ); @@ -69,7 +64,7 @@ export function getHooks(controller: Controller) { return { usePromoManager, - useAvailablePromo, + usePromo, useActivePromo, }; } diff --git a/src/promo-manager/setups/survey-manager.ts b/src/promo-manager/setups/survey-manager.ts index e62f650..a84d036 100644 --- a/src/promo-manager/setups/survey-manager.ts +++ b/src/promo-manager/setups/survey-manager.ts @@ -26,7 +26,7 @@ export const createSurveyManager = ({ const surveySlug = surveyGroup?.slug; const useActiveSurvey = () => { - const {promo, promoGroup, metaInfo, finish, cancel, cancelStart, updateProgressInfo} = + const {promo, promoGroup, metaInfo, finish, cancel, skipPromo, updateProgressInfo} = useActivePromo(); return { @@ -34,7 +34,7 @@ export const createSurveyManager = ({ config: metaInfo, finish, cancel, - cancelStart, + skipPromo, updateProgressInfo, }; }; diff --git a/src/promo-manager/tests/controller.test.ts b/src/promo-manager/tests/controller.test.ts index 5ad7c38..eb3e994 100644 --- a/src/promo-manager/tests/controller.test.ts +++ b/src/promo-manager/tests/controller.test.ts @@ -116,13 +116,13 @@ describe('trigger subscribe', () => { expect(callback).toHaveBeenCalledTimes(1); }); - it('cancelStart -> 1 update', async () => { + it('skipPromo -> 1 update', async () => { await controller.requestStart('boardPoll'); const callback = jest.fn(); controller.subscribe(callback); - await controller.cancelStart('boardPoll'); + await controller.skipPromo('boardPoll'); expect(callback).toHaveBeenCalledTimes(1); });