From afc2e749b98584033665587861772acc04cfeffa Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev Date: Tue, 24 Nov 2026 23:42:05 +0200 Subject: [PATCH] feat: add support of download button and make build logs link permanent --- frontend/src/api/client/FetchClient.ts | 4 -- frontend/src/components/common/CodeBlock.tsx | 31 +++++++++- .../components/dialogs/PackageInfoDialog.tsx | 15 +++-- .../src/components/package/BuildLogsTab.tsx | 60 +++++++++---------- .../src/components/table/PackageTable.tsx | 6 +- frontend/src/contexts/RepositoryProvider.tsx | 2 +- frontend/src/hooks/QueryKeys.ts | 4 +- frontend/src/hooks/useBuildLogStream.ts | 4 +- frontend/src/hooks/useLocationQuery.ts | 17 ++++-- frontend/src/hooks/useTableState.ts | 2 +- 10 files changed, 90 insertions(+), 55 deletions(-) diff --git a/frontend/src/api/client/FetchClient.ts b/frontend/src/api/client/FetchClient.ts index 3c857d3e..430d9e60 100644 --- a/frontend/src/api/client/FetchClient.ts +++ b/frontend/src/api/client/FetchClient.ts @@ -75,14 +75,10 @@ export class FetchClient { async fetchPackageLogs( packageBase: string, repository: RepositoryId, - version?: string, processId?: string, head?: boolean, ): Promise { const query: Record = { ...repository.toQuery() }; - if (version) { - query.version = version; - } if (processId) { query.process_id = processId; } diff --git a/frontend/src/components/common/CodeBlock.tsx b/frontend/src/components/common/CodeBlock.tsx index 2818112a..3ca309de 100644 --- a/frontend/src/components/common/CodeBlock.tsx +++ b/frontend/src/components/common/CodeBlock.tsx @@ -19,15 +19,17 @@ */ import "components/common/syntaxLanguages"; -import { Box, useTheme } from "@mui/material"; +import DownloadIcon from "@mui/icons-material/Download"; +import { Box, IconButton, Tooltip, useTheme } from "@mui/material"; import CopyButton from "components/common/CopyButton"; import { useThemeMode } from "hooks/useThemeMode"; -import React, { type RefObject } from "react"; +import React, { type RefObject, useEffect, useRef } from "react"; import { Light as SyntaxHighlighter } from "react-syntax-highlighter"; import { githubGist, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs"; interface CodeBlockProps { content: string; + downloadFilename?: string; height?: number | string; language?: string; onScroll?: () => void; @@ -36,6 +38,7 @@ interface CodeBlockProps { export default function CodeBlock({ content, + downloadFilename, height, language = "text", onScroll, @@ -43,6 +46,17 @@ export default function CodeBlock({ }: CodeBlockProps): React.JSX.Element { const { mode } = useThemeMode(); const theme = useTheme(); + const downloadRef = useRef(null); + + useEffect(() => { + if (!downloadRef.current) { + return; + } + + const url = URL.createObjectURL(new Blob([content], { type: "text/plain;charset=utf-8" })); + downloadRef.current.href = url; + return () => URL.revokeObjectURL(url); + }, [content, downloadFilename]); return - {content && + {content && + {downloadFilename && + + + + } } ; } diff --git a/frontend/src/components/dialogs/PackageInfoDialog.tsx b/frontend/src/components/dialogs/PackageInfoDialog.tsx index 69b6873d..0a3c19f8 100644 --- a/frontend/src/components/dialogs/PackageInfoDialog.tsx +++ b/frontend/src/components/dialogs/PackageInfoDialog.tsx @@ -33,12 +33,13 @@ import { type TabKey, tabs } from "components/package/TabKey"; import { QueryKeys } from "hooks/QueryKeys"; import { useAuth } from "hooks/useAuth"; import { useClient } from "hooks/useClient"; +import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery"; import { useNotification } from "hooks/useNotification"; import { useRepository } from "hooks/useRepository"; import type { Dependencies } from "models/Dependencies"; import type { PackageStatus } from "models/PackageStatus"; import type { Patch } from "models/Patch"; -import React, { useState } from "react"; +import React, { useEffect, useState } from "react"; import { StatusHeaderStyles } from "theme/StatusColors"; interface PackageInfoDialogProps { @@ -59,11 +60,17 @@ export default function PackageInfoDialog({ const queryClient = useQueryClient(); const localPackageBase = packageBase; - const [activeTab, setActiveTab] = useState("logs"); + const requestedTab = new URLSearchParams(useLocationQuery()).get("tab"); + const activeTab = tabs.find(tab => tab.key === requestedTab)?.key ?? "logs"; const [refreshDatabase, setRefreshDatabase] = useState(true); + useEffect(() => { + if (open && requestedTab === null) { + navigate(packageHref({ tab: "logs" })); + } + }, [open, requestedTab]); + const handleClose = (): void => { - setActiveTab("logs"); setRefreshDatabase(true); onClose(); }; @@ -167,7 +174,7 @@ export default function PackageInfoDialog({ {localPackageBase && currentRepository && <> - setActiveTab(tab)} value={activeTab}> + navigate(packageHref({ tab }))} value={activeTab}> {tabs.map(({ key, label }) => )} diff --git a/frontend/src/components/package/BuildLogsTab.tsx b/frontend/src/components/package/BuildLogsTab.tsx index 92134028..cf25a887 100644 --- a/frontend/src/components/package/BuildLogsTab.tsx +++ b/frontend/src/components/package/BuildLogsTab.tsx @@ -19,12 +19,13 @@ */ import ListIcon from "@mui/icons-material/List"; import { Box, Button, Menu, MenuItem, Typography } from "@mui/material"; -import { keepPreviousData, skipToken, useQuery } from "@tanstack/react-query"; +import { skipToken, useQuery } from "@tanstack/react-query"; import CodeBlock from "components/common/CodeBlock"; import { QueryKeys } from "hooks/QueryKeys"; import { useAutoScroll } from "hooks/useAutoScroll"; import { useBuildLogStream } from "hooks/useBuildLogStream"; import { useClient } from "hooks/useClient"; +import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery"; import type { LogRecord } from "models/LogRecord"; import type { RepositoryId } from "models/RepositoryId"; import React, { useEffect, useMemo, useState } from "react"; @@ -54,7 +55,8 @@ export default function BuildLogsTab({ }: BuildLogsTabProps): React.JSX.Element { const client = useClient(); useBuildLogStream(packageBase, repository); - const [selectedVersionKey, setSelectedVersionKey] = useState(null); + const query = new URLSearchParams(useLocationQuery()); + const selectedProcessId = query.get("process_id"); const [anchorEl, setAnchorEl] = useState(null); const { data: allLogs } = useQuery({ @@ -70,53 +72,47 @@ export default function BuildLogsTab({ return []; } - const grouped: Record = {}; + const grouped = new Map(); for (const record of allLogs) { - const key = `${record.version}-${record.process_id}`; - const existing = grouped[key]; + const key = record.process_id; + const existing = grouped.get(key); if (!existing) { - grouped[key] = { ...record, minCreated: record.created }; + grouped.set(key, { ...record, minCreated: record.created }); } else { existing.minCreated = Math.min(existing.minCreated, record.created); } } - return Object.values(grouped) + return Array.from(grouped.values()) .sort((left, right) => right.minCreated - left.minCreated) .map(record => ({ created: record.minCreated, logs: convertLogs( allLogs, - right => record.version === right.version && record.process_id === right.process_id, + right => record.process_id === right.process_id, ), processId: record.process_id, version: record.version, })); }, [allLogs]); - // Compute active index from selected version key, defaulting to newest (index 0) - const activeIndex = useMemo(() => { - if (selectedVersionKey) { - const index = versions.findIndex(record => `${record.version}-${record.processId}` === selectedVersionKey); - if (index >= 0) { - return index; - } - } - return 0; - }, [versions, selectedVersionKey]); + const activeVersion = versions.find(run => run.processId === selectedProcessId); + const newestProcessId = versions[0]?.processId; - const activeVersion = versions[activeIndex]; - const activeVersionKey = activeVersion ? `${activeVersion.version}-${activeVersion.processId}` : null; + useEffect(() => { + if (selectedProcessId === null && newestProcessId) { + navigate(packageHref({ process_id: newestProcessId })); + } + }, [selectedProcessId, newestProcessId]); // Refresh active version logs const { data: versionLogs } = useQuery({ - placeholderData: keepPreviousData, queryFn: activeVersion ? () => client.fetch.fetchPackageLogs( - packageBase, repository, activeVersion.version, activeVersion.processId, + packageBase, repository, activeVersion.processId, ) : skipToken, - queryKey: QueryKeys.logsVersion(packageBase, repository, activeVersion?.version ?? "", activeVersion?.processId ?? ""), + queryKey: QueryKeys.logsRun(packageBase, repository, activeVersion?.processId ?? ""), refetchOnMount: "always", }); @@ -133,7 +129,7 @@ export default function BuildLogsTab({ // Reset scroll tracking when active version changes useEffect(() => { resetScroll(); - }, [activeVersionKey, resetScroll]); + }, [selectedProcessId, resetScroll]); // Scroll to bottom on new logs useEffect(() => { @@ -153,17 +149,18 @@ export default function BuildLogsTab({ onClose={() => setAnchorEl(null)} open={Boolean(anchorEl)} > - {versions.map((logs, index) => + {versions.map(logs => { - setSelectedVersionKey(`${logs.version}-${logs.processId}`); + navigate(packageHref({ process_id: logs.processId })); setAnchorEl(null); - resetScroll(); }} - selected={index === activeIndex} + selected={logs === activeVersion} > - {new Date(logs.created * 1000).toISOStringShort()} + + {logs.version} ยท {new Date(logs.created * 1000).toISOStringShort()} + , )} {versions.length === 0 && @@ -175,6 +172,9 @@ export default function BuildLogsTab({ { const modified = event.ctrlKey || event.metaKey || event.shiftKey || event.altKey; if (event.button === 0 && !modified) { event.preventDefault(); - navigate(packageHref(currentRepository?.key ?? "", params.row.base)); + navigate(packageHref({ + repository_id: currentRepository?.key, package: params.row.base, + })); } }} tabIndex={params.tabIndex} diff --git a/frontend/src/contexts/RepositoryProvider.tsx b/frontend/src/contexts/RepositoryProvider.tsx index be9ed8c6..96d5e2ee 100644 --- a/frontend/src/contexts/RepositoryProvider.tsx +++ b/frontend/src/contexts/RepositoryProvider.tsx @@ -34,7 +34,7 @@ export function RepositoryProvider({ children }: { children: ReactNode }): React }, [repositories, repositoryKey]); const setCurrentRepository = useCallback((repository: RepositoryId) => { - navigate(packageHref(repository.key, null)); + navigate(packageHref({ repository_id: repository.key, package: null })); }, []); const value = useMemo(() => ({ diff --git a/frontend/src/hooks/QueryKeys.ts b/frontend/src/hooks/QueryKeys.ts index e1017624..6205a4f5 100644 --- a/frontend/src/hooks/QueryKeys.ts +++ b/frontend/src/hooks/QueryKeys.ts @@ -33,8 +33,8 @@ export const QueryKeys = { logs: (packageBase: string, repository: RepositoryId) => ["logs", repository.key, packageBase] as const, - logsVersion: (packageBase: string, repository: RepositoryId, version: string, processId: string) => - ["logs", repository.key, packageBase, version, processId] as const, + logsRun: (packageBase: string, repository: RepositoryId, processId: string) => + ["logs", repository.key, packageBase, processId] as const, package: (packageBase: string, repository: RepositoryId) => ["packages", repository.key, packageBase] as const, diff --git a/frontend/src/hooks/useBuildLogStream.ts b/frontend/src/hooks/useBuildLogStream.ts index cf06c6d2..aa946db4 100644 --- a/frontend/src/hooks/useBuildLogStream.ts +++ b/frontend/src/hooks/useBuildLogStream.ts @@ -82,9 +82,9 @@ export function useBuildLogStream(packageBase: string, repository: RepositoryId) existing => appendLogRecord(existing, record), ); - // Append to the version-specific cache + // Append to the run-specific cache queryClient.setQueryData( - ["logs", repository.key, packageBase, record.version, record.process_id], + ["logs", repository.key, packageBase, record.process_id], existing => appendLogRecord(existing, record), ); }); diff --git a/frontend/src/hooks/useLocationQuery.ts b/frontend/src/hooks/useLocationQuery.ts index bbf4cc5c..eff5f4c5 100644 --- a/frontend/src/hooks/useLocationQuery.ts +++ b/frontend/src/hooks/useLocationQuery.ts @@ -32,13 +32,18 @@ export function useLocationQuery(): string { return useSyncExternalStore(subscribe, getSnapshot); } -export function packageHref(repositoryKey: string, packageBase: string | null): string { +export function packageHref(values: Record): string { const query = new URLSearchParams(window.location.search); - query.set("repository_id", repositoryKey); - if (packageBase) { - query.set("package", packageBase); - } else { - query.delete("package"); + for (const [key, value] of Object.entries(values)) { + if (value) { + query.set(key, value); + } else { + query.delete(key); + } + } + if (values.package === null) { + query.delete("tab"); + query.delete("process_id"); } return `${window.location.pathname}?${query}`; } diff --git a/frontend/src/hooks/useTableState.ts b/frontend/src/hooks/useTableState.ts index 16db76ad..6b40bf96 100644 --- a/frontend/src/hooks/useTableState.ts +++ b/frontend/src/hooks/useTableState.ts @@ -50,7 +50,7 @@ export function useTableState(): UseTableStateResult { const selectedPackage = currentRepository && packageBase ? packageBase : null; const setSelectedPackage = (base: string | null): void => { if (currentRepository) { - navigate(packageHref(currentRepository.key, base)); + navigate(packageHref({ repository_id: currentRepository.key, package: base })); } }; const [searchText, setSearchText] = useState("");