Skip to content

The open alternative to Salesforce, designed for AI.

1.4 KB
Raw Blame
import { GETTING_STARTED_AUTO_ADVANCE_DELAY_MS } from '@/getting-started/constants/GettingStartedAutoAdvanceDelayMs';
import { useSelectGettingStartedStep } from '@/getting-started/hooks/useSelectGettingStartedStep';
import { gettingStartedJustCompletedStepState } from '@/getting-started/states/gettingStartedJustCompletedStepState';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
import { useEffect, useEffectEvent } from 'react';

import { type GettingStartedStepActionKey } from '~/generated-metadata/graphql';

type GettingStartedAutoAdvanceEffectProps = {
  completedStepActionKey: GettingStartedStepActionKey;
  nextStepActionKey: GettingStartedStepActionKey;
};

export const GettingStartedAutoAdvanceEffect = ({
  completedStepActionKey,
  nextStepActionKey,
}: GettingStartedAutoAdvanceEffectProps) => {
  const { selectGettingStartedStep } = useSelectGettingStartedStep();
  const setGettingStartedJustCompletedStep = useSetAtomState(
    gettingStartedJustCompletedStepState,
  );
  const advance = useEffectEvent(() =>
    selectGettingStartedStep(nextStepActionKey),
  );

  useEffect(() => {
    const timeoutId = setTimeout(
      () => advance(),
      GETTING_STARTED_AUTO_ADVANCE_DELAY_MS,
    );

    return () => clearTimeout(timeoutId);
  }, [completedStepActionKey]);

  useEffect(
    () => () => setGettingStartedJustCompletedStep(null),
    [setGettingStartedJustCompletedStep],
  );

  return null;
};