import { useRef, useEffect } from 'react';
import { useQuery, useSubscription } from '@apollo/client';
import { get } from 'lodash';
import { useSelector, useDispatch } from 'react-redux';

import { FETCH_ALL_CONSORTIA_QUERY, RUN_STARTED_SUBSCRIPTION } from '../../../state/graphql/functions';
import { startRun } from '../../../state/ducks/runs';

/*
 * This effect checks if there are runs that must be started locally when the app first starts.
 */
function useStartDecentralizedRun() {
  const dispatch = useDispatch();

  const auth = useSelector(state => state.auth);

  const { data } = useQuery(FETCH_ALL_CONSORTIA_QUERY);
  const { data: runSubData } = useSubscription(RUN_STARTED_SUBSCRIPTION, {
    variables: {
      userId: auth.user.id,
    },
    skip: !auth || !auth.user || !auth.user.id,
  });

  const consortia = get(data, 'fetchAllConsortia');

  useEffect(() => {
    const run = get(runSubData, 'runStarted');

    if (!run || run.status !== 'started') return;

    const consortium = consortia.find(c => c.id === run.consortiumId);

    dispatch(startRun(run, consortium));
  }, [runSubData]);
}

export default useStartDecentralizedRun;

import { useEffect } from 'react';
import { useQuery, useSubscription } from '@apollo/client';
import { get } from 'lodash';
import { useSelector, useDispatch } from 'react-redux';

import { FETCH_ALL_CONSORTIA_QUERY, RUN_STARTED_SUBSCRIPTION } from '../../../state/graphql/functions';
import { startRun } from '../../../state/ducks/runs';

/**
 * This effect starts decentralized runs based on the RUN_STARTED_SUBSCRIPTION.
 *
 * The RUN_STARTED_SUBSCRIPTION has new data when a new decentralized run (of which the
 * current user is a part of) is started.
 */
function useStartDecentralizedRun() {
  const dispatch = useDispatch();

  const auth = useSelector(state => state.auth);

  const { data } = useQuery(FETCH_ALL_CONSORTIA_QUERY);
  const { data: runSubData } = useSubscription(RUN_STARTED_SUBSCRIPTION, {
    variables: {
      userId: auth.user.id,
    },
    skip: !auth || !auth.user || !auth.user.id,
  });

  const consortia = get(data, 'fetchAllConsortia');

  useEffect(() => {
    const run = get(runSubData, 'runStarted');

    if (!run || run.status !== 'started') return;

    const consortium = consortia.find(c => c.id === run.consortiumId);

    dispatch(startRun(run, consortium));
  }, [runSubData]);
}

export default useStartDecentralizedRun;

Update comment on the component responsible for starting decentralized runs
Update comment on the component responsible for starting decentralized runs
