mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-11-25 18:09:31 +00:00
feat: add support of download button and make build logs link permanent
This commit is contained in:
1 parent
941a51d496
commit
afc2e749b9
10 files changed
+90
-55
No files matched your search
@@ -75,14 +75,10 @@ export class FetchClient {
|
||||
async fetchPackageLogs(
|
||||
packageBase: string,
|
||||
repository: RepositoryId,
|
||||
version?: string,
|
||||
processId?: string,
|
||||
head?: boolean,
|
||||
): Promise<LogRecord[]> {
|
||||
const query: Record<string, string | boolean> = { ...repository.toQuery() };
|
||||
if (version) {
|
||||
query.version = version;
|
||||
}
|
||||
if (processId) {
|
||||
query.process_id = processId;
|
||||
}
|
||||
|
||||
@@ -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<HTMLAnchorElement>(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 <Box sx={{ position: "relative" }}>
|
||||
<Box
|
||||
@@ -63,8 +77,19 @@ export default function CodeBlock({
|
||||
{content}
|
||||
</SyntaxHighlighter>
|
||||
</Box>
|
||||
{content && <Box sx={{ position: "absolute", right: 8, top: 8 }}>
|
||||
{content && <Box sx={{ display: "flex", position: "absolute", right: 8, top: 8 }}>
|
||||
<CopyButton text={content} />
|
||||
{downloadFilename && <Tooltip title="Download">
|
||||
<IconButton
|
||||
aria-label="Download"
|
||||
component="a"
|
||||
download={downloadFilename}
|
||||
ref={downloadRef}
|
||||
size="small"
|
||||
>
|
||||
<DownloadIcon fontSize="small" />
|
||||
</IconButton>
|
||||
</Tooltip>}
|
||||
</Box>}
|
||||
</Box>;
|
||||
}
|
||||
@@ -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<TabKey>("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 &&
|
||||
<>
|
||||
<Box sx={{ borderBottom: 1, borderColor: "divider", mt: 2 }}>
|
||||
<Tabs onChange={(_, tab: TabKey) => setActiveTab(tab)} value={activeTab}>
|
||||
<Tabs onChange={(_, tab: TabKey) => navigate(packageHref({ tab }))} value={activeTab}>
|
||||
{tabs.map(({ key, label }) => <Tab key={key} label={label} value={key} />)}
|
||||
</Tabs>
|
||||
</Box>
|
||||
|
||||
@@ -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<string | null>(null);
|
||||
const query = new URLSearchParams(useLocationQuery());
|
||||
const selectedProcessId = query.get("process_id");
|
||||
const [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
|
||||
|
||||
const { data: allLogs } = useQuery<LogRecord[]>({
|
||||
@@ -70,53 +72,47 @@ export default function BuildLogsTab({
|
||||
return [];
|
||||
}
|
||||
|
||||
const grouped: Record<string, LogRecord & { minCreated: number }> = {};
|
||||
const grouped = new Map<string, LogRecord & { minCreated: number }>();
|
||||
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<LogRecord[]>({
|
||||
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 =>
|
||||
<MenuItem
|
||||
key={`${logs.version}-${logs.processId}`}
|
||||
key={logs.processId}
|
||||
onClick={() => {
|
||||
setSelectedVersionKey(`${logs.version}-${logs.processId}`);
|
||||
navigate(packageHref({ process_id: logs.processId }));
|
||||
setAnchorEl(null);
|
||||
resetScroll();
|
||||
}}
|
||||
selected={index === activeIndex}
|
||||
selected={logs === activeVersion}
|
||||
>
|
||||
<Typography variant="body2">{new Date(logs.created * 1000).toISOStringShort()}</Typography>
|
||||
<Typography variant="body2">
|
||||
{logs.version} · {new Date(logs.created * 1000).toISOStringShort()}
|
||||
</Typography>
|
||||
</MenuItem>,
|
||||
)}
|
||||
{versions.length === 0 &&
|
||||
@@ -175,6 +172,9 @@ export default function BuildLogsTab({
|
||||
<Box sx={{ flex: 1 }}>
|
||||
<CodeBlock
|
||||
content={displayedLogs}
|
||||
downloadFilename={activeVersion
|
||||
? `${packageBase}-${activeVersion.version}-${activeVersion.processId}.log`
|
||||
: undefined}
|
||||
height={400}
|
||||
onScroll={handleScroll}
|
||||
preRef={preRef}
|
||||
|
||||
@@ -83,12 +83,14 @@ export default function PackageTable(): React.JSX.Element {
|
||||
minWidth: 150,
|
||||
renderCell: params =>
|
||||
<Link
|
||||
href={packageHref(currentRepository?.key ?? "", params.row.base)}
|
||||
href={packageHref({ repository_id: currentRepository?.key, package: params.row.base })}
|
||||
onClick={event => {
|
||||
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}
|
||||
|
||||
@@ -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(() => ({
|
||||
|
||||
@@ -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,
|
||||
|
||||
|
||||
@@ -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<LogRecord[]>(
|
||||
["logs", repository.key, packageBase, record.version, record.process_id],
|
||||
["logs", repository.key, packageBase, record.process_id],
|
||||
existing => appendLogRecord(existing, record),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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, string | null | undefined>): 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}`;
|
||||
}
|
||||
|
||||
@@ -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("");
|
||||
|
||||
Reference in new issue
Block a user