
// import react
import { Virtuoso } from 'react-virtuoso';
import React, { useEffect, useState } from 'react';

// import local dependencies
import Loading from '../../../Loading';
import ApiSignal from '../../../../signals/Api';
import useResults from '../../../../hooks/useResults';
import { useSignal } from '../../../../signal';
import WorkflowFrameListItem from './Item';
import { current as TeamSignal } from '../../../../signals/Teams';

/**
 * component frames
 *
 * @param props 
 */
const ComponentWorkflowFrameList = (props = {}) => {
  // signals
  const [api] = useSignal(ApiSignal);
  const [team] = useSignal(TeamSignal);
  
  // state
  const [ready, setReady] = useState(false);
  
  // items
  const {
    key,
    resetting,
    items : frames,

    loadNext,
  } = useResults(api.Workflow.Frame.list, 'frames', {
    sort     : props.sort,
    team     : team?.id,
    search   : props.search,
    disable  : !team?.id,
    workflow : props.workflow?.id || props.workflow,
  }, null, props.onResult);

  // use effect
  useEffect(() => {
    // ready
    setReady(true);
  }, [])

  // return jsx
  return (
    <div className="flex-1 frames">
      {(ready && !resetting) ? (
        <Virtuoso
          useWindowScroll

          data={frames}
          overscan={{
            main    : 2048,
            reverse : 2048,
          }}
          endReached={() => loadNext()}
          itemContent={(index, item) => (
            <WorkflowFrameListItem
              key={item.id}
              item={item}
            />
          )}
        />
      ) : (
        <div className="flex flex-1 justify-center items-center">
          <Loading />
        </div>
      )}
    </div>
  );
};

// export default
export default ComponentWorkflowFrameList;