import { ContextStoreComponentInstanceContext } from '@/context-store/states/contexts/ContextStoreComponentInstanceContext';
import { mainContextStoreComponentInstanceIdState } from '@/context-store/states/mainContextStoreComponentInstanceId';
import { useContext, useEffect } from 'react';
import { useSetRecoilState } from 'recoil';

export const MainContextStoreComponentInstanceIdSetterEffect = () => {
  const setMainContextStoreComponentInstanceId = useSetRecoilState(
    mainContextStoreComponentInstanceIdState,
  );

  const context = useContext(ContextStoreComponentInstanceContext);

  useEffect(() => {
    setMainContextStoreComponentInstanceId(context?.instanceId ?? 'app');

    return () => {
      setMainContextStoreComponentInstanceId('app');
    };
  }, [context, setMainContextStoreComponentInstanceId]);

  return null;
};
