import React, { useState, useEffect } from 'react'; import { Select, Input, Table } from 'antd'; import queryString from 'query-string'; // @ts-ignore import SourceCode from 'dumi-theme-default/src/builtins/SourceCode'; // @ts-ignore import Alert from 'dumi-theme-default/src/builtins/Alert'; import { ColumnsType } from 'antd/es/table'; import './CaskAndFormula/app-detail.less'; const Option = Select.Option; const { Search } = Input; const getQuery = () => { return queryString.parse(window.location.search); }; interface DataType { arch: string; os: string; version: string; url: string; } const columns: ColumnsType = [ { title: 'arch', dataIndex: 'arch', width: 200, onCell: (_, index) => { if (index === 0) { return { rowSpan: 10 }; } return { rowSpan: 0 }; }, }, { title: 'os', dataIndex: 'os', width: 300, }, { title: 'version', dataIndex: 'version', render: (text, row) => { return ( {row.version} ); }, }, ]; const AppDetail = ({}) => { const [caskDetail, setCaskDetail] = useState({}); const [formulaDetail, setFormulaDetail] = useState({}); const [isCask, setIsCask] = useState(true); useEffect(() => { const query = getQuery(); if (query.cid) { getCaskDetail(query.cid); setIsCask(true); } else if (query.fid) { getFormulaDetail(query.fid); setIsCask(false); } }, []); const getCaskDetail = id => { fetch( AVALON_SERVER + '/homebrew/cask-detail?' + queryString.stringify({ id, }), ) .then(response => response.json()) .then(res => { if (res.code === 0 && res.result) { const result = res.result; result.variations = JSON.parse(result.variations); result.depends_on = JSON.parse(result.depends_on); setCaskDetail(result); } }); }; const getFormulaDetail = id => { fetch( AVALON_SERVER + '/homebrew/formula-detail?' + queryString.stringify({ id, }), ) .then(response => response.json()) .then(res => { if (res.code === 0 && res.result) { const result = res.result; // result.versions = JSON.parse(result.versions); // result.versioned_formulae = JSON.parse(result.versioned_formulae); // result.dependencies = JSON.parse(result.dependencies); // result.build_dependencies = JSON.parse(result.build_dependencies); console.log(result); setFormulaDetail(result); } }); }; const renderObjectJson = obj => { if (!obj) return; const list: any = []; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { const element = obj[key]; for (const condition in element) { if (Object.prototype.hasOwnProperty.call(element, condition)) { const version = element[condition]; list.push(

{key} {condition} {version}

, ); } } } } if (list.length === 0) return; return ( <>

系统要求

{list} ); }; const renderVariations = variations => { console.log(variations); if (!variations) return; const keys = Object.keys(variations); if (keys.length === 0) return; const intelDataArray: DataType[] = []; const appleSiliconDataArray: any = []; keys.forEach(key => { const value = variations[key]; if (key.includes('arm64_')) { appleSiliconDataArray.push({ arch: 'Apple Silicon', os: key.replace('arm64_', ''), url: value.url, version: value.version || caskDetail.version, }); } else { intelDataArray.push({ arch: 'Intel', os: key, url: value.url, version: value.version || caskDetail.version, }); } }); return ( <>

软件版本

{intelDataArray.length > 0 && ( )} {appleSiliconDataArray.length > 0 && (
)} ); }; const renderCask = () => { return ( <>

{caskDetail.token} {caskDetail.version}

{caskDetail.desc}

安装命令

{renderObjectJson(caskDetail.depends_on)}
{caskDetail.caveats && {caskDetail.caveats}} {renderVariations(caskDetail.variations)} ); }; const renderVersionedFormulae = versionedFormulae => { if (!versionedFormulae || !versionedFormulae.length) return; return ( <>

其他版本

{ return
{text}
; }, }, { dataIndex: 'versions', render: (text, row) => { return
{text.stable}
; }, }, ]} dataSource={versionedFormulae} showHeader={false} pagination={false} bordered /> ); }; const renderDep = (deps, title) => { if (!deps || !deps.length) return; return ( <>

{title}

{ return
{text}
; }, }, { dataIndex: 'versions', render: (text) => { return
{text.stable}
; }, }, ]} dataSource={deps} showHeader={false} pagination={false} bordered /> ); }; const renderFormula = () => { return ( <>

{formulaDetail.name} {formulaDetail.versions?.stable}

{formulaDetail.desc}

安装命令

{renderVersionedFormulae(formulaDetail.versioned_formulae)} {renderDep(formulaDetail.dependencies, '依赖')} {renderDep(formulaDetail.build_dependencies, '构建依赖')} ); }; return (
{isCask ? renderCask() : renderFormula()}
); }; export default AppDetail;