From 1f6448a652271b0805a54c2aa53abd95a8eae426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Allen=20Zhang=20=28=E5=BC=A0=E6=B6=9B=29?= Date: Thu, 15 Feb 2024 14:35:26 +0800 Subject: [PATCH] feat: update --- .../index/projects/[id]/commits/[sha].tsx | 61 ++++++++++++++----- packages/canyon-platform/vite.config.ts | 2 +- .../src/Report/components/IstanbulReport.tsx | 20 +++--- packages/canyon-report/src/Report/init.tsx | 3 +- packages/canyon-report/src/Report/types.ts | 1 + packages/canyon-report/src/main.tsx | 34 +++++++---- 6 files changed, 82 insertions(+), 39 deletions(-) diff --git a/packages/canyon-platform/src/pages/index/projects/[id]/commits/[sha].tsx b/packages/canyon-platform/src/pages/index/projects/[id]/commits/[sha].tsx index 52f9fa65..22f287bb 100755 --- a/packages/canyon-platform/src/pages/index/projects/[id]/commits/[sha].tsx +++ b/packages/canyon-platform/src/pages/index/projects/[id]/commits/[sha].tsx @@ -4,13 +4,18 @@ import { useRequest } from 'ahooks'; import axios from 'axios'; import { useEffect, useRef } from 'react'; // import { useParams } from 'react-router'; -import { useParams, useSearchParams } from 'react-router-dom'; +import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom'; // import { GetCoverageSummaryMapDocument } from '../../../../helpers/backend/gen/graphql.ts'; import { handleSelectFile } from './helper'; const Sha = () => { const prm = useParams(); + const nav = useNavigate(); const [sprm] = useSearchParams(); + // 在组件中 + const location = useLocation(); + const currentPathname = location.pathname; + // const { data } = useQuery(GetCoverageSummaryMapDocument, { // variables: { // reportID: sprm.get('report_id') || '', @@ -28,31 +33,57 @@ const Sha = () => { sha: prm.sha || '', mode: sprm.get('mode') || '', }, - }).then(({data}) => data); + }).then(({ data }) => data); }, { - onSuccess(res) { - }, + onSuccess() {}, }, ); const reportRef = useRef(null); useEffect(() => { if (reportRef.current && data) { const report = init(reportRef.current, { - theme:localStorage.getItem('theme')||'light', + defaultPath: sprm.get('path') || '~', + theme: localStorage.getItem('theme') || 'light', onSelectFile(path: string) { - return handleSelectFile({ - filepath: path, - reportID: sprm.get('report_id') || '', - sha: prm.sha || '', - projectID: prm.id || '', - mode: sprm.get('mode') || '', - }).then(r=>{ - return r - }); + // console.log(path) + + // 假设你想要导航到 '/path' 并且拼接参数 + // const path1 = `/path`; + const params = new URLSearchParams(); + if (sprm.get('report_id')) { + params.append('report_id', sprm.get('report_id') || ''); + } + if (sprm.get('mode')) { + params.append('mode', sprm.get('mode') || ''); + } + + // params.append('mode', sprm.get('mode')); + params.append('path', path); + + // 将参数拼接到路径中 + const pathWithParams = `${currentPathname}?${params.toString()}`; + + nav(pathWithParams); + if (path.includes('.')) { + return handleSelectFile({ + filepath: path, + reportID: sprm.get('report_id') || '', + sha: prm.sha || '', + projectID: prm.id || '', + mode: sprm.get('mode') || '', + }).then((r) => { + return r; + }); + } else { + return new Promise((resolve) => { + resolve({}); + }); + // return new P + } }, }); - const summary = data.reduce((acc: any, cur) => { + const summary = data.reduce((acc: any, cur: any) => { acc[cur.path] = cur; return acc; }, {}); diff --git a/packages/canyon-platform/vite.config.ts b/packages/canyon-platform/vite.config.ts index b28062bc..036e1180 100644 --- a/packages/canyon-platform/vite.config.ts +++ b/packages/canyon-platform/vite.config.ts @@ -43,7 +43,7 @@ export default defineConfig({ host: '0.0.0.0', proxy: { '^/graphql|^/api': { - target: 'http://10.130.238.190', + target: 'http://10.128.124.139', changeOrigin: true, } }, diff --git a/packages/canyon-report/src/Report/components/IstanbulReport.tsx b/packages/canyon-report/src/Report/components/IstanbulReport.tsx index df0257e5..824f49d1 100755 --- a/packages/canyon-report/src/Report/components/IstanbulReport.tsx +++ b/packages/canyon-report/src/Report/components/IstanbulReport.tsx @@ -14,16 +14,18 @@ const capitalized = (word: string) => const dims = ['statements', 'branches', 'functions', 'lines', 'newlines']; const IstanbulReport: FC = ({ theme, + defaultPath, onSelectFile, watermarks, }) => { + console.log(defaultPath,'defaultPath') const [summary, setSummary] = useState({}); // @ts-ignore const [fileCoverage, setFileCoverage] = useState({} as any); const [fileContent, setFileContent] = useState(''); const [fileCodeChange, setFileCodeChange] = useState([]); const [loading, setLoading] = useState(false); - const [activePath, setActivePath] = useState('~'); + const [activePath, setActivePath] = useState(defaultPath); const summaryTreeItem = useMemo(() => { return genSummaryTreeItem(activePath, summary); @@ -46,17 +48,17 @@ const IstanbulReport: FC = ({ }, [activePath]); useEffect(() => { - if (activePath.includes('.')) { - setLoading(true); - onSelectFile(activePath).then( - ({ fileContent, fileCoverage, fileCodeChange }) => { + setLoading(true); + onSelectFile(activePath).then( + ({ fileContent, fileCoverage, fileCodeChange }) => { + if (activePath.includes('.')) { setFileContent(fileContent); setFileCoverage(fileCoverage); setFileCodeChange(fileCodeChange); - setLoading(false); - }, - ); - } + } + setLoading(false); + }, + ); }, [activePath]); useEffect(() => { const handleSetOptionEvent = (e: { detail: any }) => { diff --git a/packages/canyon-report/src/Report/init.tsx b/packages/canyon-report/src/Report/init.tsx index c75c3b3b..7f4e0455 100755 --- a/packages/canyon-report/src/Report/init.tsx +++ b/packages/canyon-report/src/Report/init.tsx @@ -12,7 +12,7 @@ const configWatermarks = { }; const init = ( dom: HTMLElement, - { onSelectFile,theme }: { onSelectFile: OnSelectFile,theme:string }, + { onSelectFile,theme,defaultPath }: { onSelectFile: OnSelectFile,theme:string,defaultPath:string }, ) => { loadCssCode(); render( @@ -20,6 +20,7 @@ const init = ( theme={theme} onSelectFile={onSelectFile} watermarks={configWatermarks} + defaultPath={defaultPath} />, dom!, ); diff --git a/packages/canyon-report/src/Report/types.ts b/packages/canyon-report/src/Report/types.ts index 9fe83ce8..b604c9b3 100755 --- a/packages/canyon-report/src/Report/types.ts +++ b/packages/canyon-report/src/Report/types.ts @@ -42,4 +42,5 @@ export type IstanbulReportProps = { onSelectFile: OnSelectFile; watermarks: Watermarks; theme: string; + defaultPath:string; }; diff --git a/packages/canyon-report/src/main.tsx b/packages/canyon-report/src/main.tsx index 19356c60..ff4da61f 100755 --- a/packages/canyon-report/src/main.tsx +++ b/packages/canyon-report/src/main.tsx @@ -1,5 +1,6 @@ import './reset.css'; -import './assets/css/index.css' +import './assets/css/index.css'; + import { genSummaryMapByCoverageMap } from '@canyon/data'; import { init } from './index'; @@ -17,19 +18,26 @@ function getDecode(str: string) { } const report = init(document.querySelector('#root') as any, { - theme:'light', + theme: 'light', + defaultPath: '~/src', onSelectFile(path: string) { - return new Promise((resolve) => { - setTimeout(()=>{ - resolve({ - // @ts-ignore - fileCoverage: __coverage__[path], - // @ts-ignore - fileContent: getDecode(__filecontent__[path]), - fileCodeChange:[1,2,3,4] - }); - },200) - }); + if (path.includes('.')) { + return new Promise((resolve) => { + setTimeout(() => { + resolve({ + // @ts-ignore + fileCoverage: __coverage__[path], + // @ts-ignore + fileContent: getDecode(__filecontent__[path]), + fileCodeChange: [1, 2, 3, 4], + }); + }, 200); + }); + } else { + return new Promise((resolve) => { + resolve({}); + }); + } }, }); report.setOption({