From e43e918a1791b2a132c166417b1750709e437c70 Mon Sep 17 00:00:00 2001 From: zhangtao25 Date: Wed, 20 Nov 2024 20:12:23 +0800 Subject: [PATCH] chore: repo overall update --- .../[id]/commits/[sha]/[...filepath]/page.tsx | 67 +++++-------------- packages/canyon-platform/utils/handle.ts | 45 +++++++++++++ 2 files changed, 60 insertions(+), 52 deletions(-) create mode 100644 packages/canyon-platform/utils/handle.ts diff --git a/packages/canyon-platform/app/projects/[id]/commits/[sha]/[...filepath]/page.tsx b/packages/canyon-platform/app/projects/[id]/commits/[sha]/[...filepath]/page.tsx index 8eb90d29..b83dc040 100644 --- a/packages/canyon-platform/app/projects/[id]/commits/[sha]/[...filepath]/page.tsx +++ b/packages/canyon-platform/app/projects/[id]/commits/[sha]/[...filepath]/page.tsx @@ -1,23 +1,14 @@ "use client"; -import { useParams, useRouter } from "next/navigation"; -import React, { useMemo, useState } from "react"; +import { useParams } from "next/navigation"; +import React, { useState } from "react"; import useSWR from "swr"; import axios from "axios"; import MainBox from "@/components/main-box"; import { Report } from "../../../../../../../canyon-report/src/components"; import { theme } from "antd"; -import { genSummaryMapByCoverageMap, getSummaryByPath } from "canyon-data"; +import { handleSelect } from "@/utils/handle"; +// import {handleSelect} from "@/utils/handle"; const { useToken } = theme; -function getDecode(str: string) { - return decodeURIComponent( - atob(str) - .split("") - .map(function (c) { - return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); - }) - .join(""), - ); -} // http://localhost:3000/projects/490316875/commits/f721bf684d49254717e03ca14e53a1b1f8882019/src/index.tsx const fetcher = ({ url, params }) => @@ -29,22 +20,9 @@ const fetcher = ({ url, params }) => export default function Page() { const { filepath, id, sha } = useParams(); // 获取动态路由参数 - const filePath = filepath ? filepath.join("/") : ""; + const defultfilepath = filepath ? filepath.join("/") : ""; const { token } = useToken(); - const router = useRouter(); - // const [value, setValue] = useState(window.location.hash.slice(1)); - - const { data: fileCoverage } = useSWR( - { - url: "/api/cov/map", - params: { - project_id: id, - sha, - filepath: filePath, - }, - }, - fetcher, - ); + const [value, setValue] = useState(defultfilepath); const { data: summary } = useSWR( { @@ -52,37 +30,22 @@ export default function Page() { params: { project_id: id, sha, - // filepath: filePath, - }, - }, - fetcher, - ); - - const { data: fileContent } = useSWR( - { - url: "/api/sourcecode", - params: { - project_id: id, - sha, - filepath: filePath, }, }, fetcher, ); const onSelect = (val) => { - // console.log(val, "val"); - router.push(`/projects/${id}/commits/${sha}/${val}`); - return new Promise((resolve) => { - resolve({ - fileContent: getDecode(fileContent.content), - fileCoverage: fileCoverage[filePath], - }); + history.pushState(null, "", `/projects/${id}/commits/${sha}/${val}`); + setValue(val); + // handleSelect + return handleSelect({ + projectID: id, + sha, + filepath: val, }); }; - // const summary = genSummaryMapByCoverageMap(cov, []); - return (
- {summary?.length && fileContent && fileCoverage && ( + {summary?.length && ( )}
diff --git a/packages/canyon-platform/utils/handle.ts b/packages/canyon-platform/utils/handle.ts new file mode 100644 index 00000000..681ff90d --- /dev/null +++ b/packages/canyon-platform/utils/handle.ts @@ -0,0 +1,45 @@ +import axios from "axios"; +// import { getDecode } from "@/utils/coverage"; + +// 用于文件base64解码后的格式化 +function getDecode(str: string) { + return decodeURIComponent( + atob(str) + .split("") + .map(function (c) { + return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); + }) + .join(""), + ); +} +export const handleSelect = ({ projectID, sha, filepath }) => { + return Promise.all([ + axios.get("/api/cov/map", { + params: { + project_id: projectID, + sha, + filepath, + }, + }), + axios + .get("/api/sourcecode", { + params: { + project_id: projectID, + sha, + filepath, + }, + }) + .catch((err) => { + return { + data: { + content: "", + }, + }; + }), + ]).then(([res1, res2]) => { + return { + fileCoverage: res1.data[filepath], + fileContent: getDecode(res2.data.content), + }; + }); +};