import InfoGroup from '@/components/InfoGroup';
import { getFileReq } from '@/services/file';
import { to } from '@/utils/promise';
import { useEffect, useState } from 'react';
import styles from './index.less';

type ExperimentResultProps = {
  fileUrl?: string;
};

function ExperimentResult({ fileUrl }: ExperimentResultProps) {
  const [result, setResult] = useState<string | undefined>('');

  useEffect(() => {
    // 获取实验运行历史记录
    const getResultFile = async () => {
      const [res] = await to(getFileReq(fileUrl));
      if (res) {
        setResult(res as any as string);
      }
    };

    if (fileUrl) {
      getResultFile();
    }
  }, [fileUrl]);

  return (
    <div className={styles['experiment-result']}>
      <InfoGroup title="最佳实验结果" width="100%">
        <div className={styles['experiment-result__text']}>{result}</div>
      </InfoGroup>
    </div>
  );
}

export default ExperimentResult;
