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(
|
async fetchPackageLogs(
|
||||||
packageBase: string,
|
packageBase: string,
|
||||||
repository: RepositoryId,
|
repository: RepositoryId,
|
||||||
version?: string,
|
|
||||||
processId?: string,
|
processId?: string,
|
||||||
head?: boolean,
|
head?: boolean,
|
||||||
): Promise<LogRecord[]> {
|
): Promise<LogRecord[]> {
|
||||||
const query: Record<string, string | boolean> = { ...repository.toQuery() };
|
const query: Record<string, string | boolean> = { ...repository.toQuery() };
|
||||||
if (version) {
|
|
||||||
query.version = version;
|
|
||||||
}
|
|
||||||
if (processId) {
|
if (processId) {
|
||||||
query.process_id = processId;
|
query.process_id = processId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,15 +19,17 @@
|
|||||||
*/
|
*/
|
||||||
import "components/common/syntaxLanguages";
|
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 CopyButton from "components/common/CopyButton";
|
||||||
import { useThemeMode } from "hooks/useThemeMode";
|
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 { Light as SyntaxHighlighter } from "react-syntax-highlighter";
|
||||||
import { githubGist, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs";
|
import { githubGist, vs2015 } from "react-syntax-highlighter/dist/esm/styles/hljs";
|
||||||
|
|
||||||
interface CodeBlockProps {
|
interface CodeBlockProps {
|
||||||
content: string;
|
content: string;
|
||||||
|
downloadFilename?: string;
|
||||||
height?: number | string;
|
height?: number | string;
|
||||||
language?: string;
|
language?: string;
|
||||||
onScroll?: () => void;
|
onScroll?: () => void;
|
||||||
@@ -36,6 +38,7 @@ interface CodeBlockProps {
|
|||||||
|
|
||||||
export default function CodeBlock({
|
export default function CodeBlock({
|
||||||
content,
|
content,
|
||||||
|
downloadFilename,
|
||||||
height,
|
height,
|
||||||
language = "text",
|
language = "text",
|
||||||
onScroll,
|
onScroll,
|
||||||
@@ -43,6 +46,17 @@ export default function CodeBlock({
|
|||||||
}: CodeBlockProps): React.JSX.Element {
|
}: CodeBlockProps): React.JSX.Element {
|
||||||
const { mode } = useThemeMode();
|
const { mode } = useThemeMode();
|
||||||
const theme = useTheme();
|
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" }}>
|
return <Box sx={{ position: "relative" }}>
|
||||||
<Box
|
<Box
|
||||||
@@ -63,8 +77,19 @@ export default function CodeBlock({
|
|||||||
{content}
|
{content}
|
||||||
</SyntaxHighlighter>
|
</SyntaxHighlighter>
|
||||||
</Box>
|
</Box>
|
||||||
{content && <Box sx={{ position: "absolute", right: 8, top: 8 }}>
|
{content && <Box sx={{ display: "flex", position: "absolute", right: 8, top: 8 }}>
|
||||||
<CopyButton text={content} />
|
<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>}
|
||||||
</Box>;
|
</Box>;
|
||||||
}
|
}
|
||||||
@@ -33,12 +33,13 @@ import { type TabKey, tabs } from "components/package/TabKey";
|
|||||||
import { QueryKeys } from "hooks/QueryKeys";
|
import { QueryKeys } from "hooks/QueryKeys";
|
||||||
import { useAuth } from "hooks/useAuth";
|
import { useAuth } from "hooks/useAuth";
|
||||||
import { useClient } from "hooks/useClient";
|
import { useClient } from "hooks/useClient";
|
||||||
|
import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery";
|
||||||
import { useNotification } from "hooks/useNotification";
|
import { useNotification } from "hooks/useNotification";
|
||||||
import { useRepository } from "hooks/useRepository";
|
import { useRepository } from "hooks/useRepository";
|
||||||
import type { Dependencies } from "models/Dependencies";
|
import type { Dependencies } from "models/Dependencies";
|
||||||
import type { PackageStatus } from "models/PackageStatus";
|
import type { PackageStatus } from "models/PackageStatus";
|
||||||
import type { Patch } from "models/Patch";
|
import type { Patch } from "models/Patch";
|
||||||
import React, { useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
import { StatusHeaderStyles } from "theme/StatusColors";
|
import { StatusHeaderStyles } from "theme/StatusColors";
|
||||||
|
|
||||||
interface PackageInfoDialogProps {
|
interface PackageInfoDialogProps {
|
||||||
@@ -59,11 +60,17 @@ export default function PackageInfoDialog({
|
|||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const localPackageBase = packageBase;
|
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);
|
const [refreshDatabase, setRefreshDatabase] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (open && requestedTab === null) {
|
||||||
|
navigate(packageHref({ tab: "logs" }));
|
||||||
|
}
|
||||||
|
}, [open, requestedTab]);
|
||||||
|
|
||||||
const handleClose = (): void => {
|
const handleClose = (): void => {
|
||||||
setActiveTab("logs");
|
|
||||||
setRefreshDatabase(true);
|
setRefreshDatabase(true);
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
@@ -167,7 +174,7 @@ export default function PackageInfoDialog({
|
|||||||
{localPackageBase && currentRepository &&
|
{localPackageBase && currentRepository &&
|
||||||
<>
|
<>
|
||||||
<Box sx={{ borderBottom: 1, borderColor: "divider", mt: 2 }}>
|
<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.map(({ key, label }) => <Tab key={key} label={label} value={key} />)}
|
||||||
</Tabs>
|
</Tabs>
|
||||||
</Box>
|
</Box>
|
||||||
|
|||||||
@@ -19,12 +19,13 @@
|
|||||||
*/
|
*/
|
||||||
import ListIcon from "@mui/icons-material/List";
|
import ListIcon from "@mui/icons-material/List";
|
||||||
import { Box, Button, Menu, MenuItem, Typography } from "@mui/material";
|
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 CodeBlock from "components/common/CodeBlock";
|
||||||
import { QueryKeys } from "hooks/QueryKeys";
|
import { QueryKeys } from "hooks/QueryKeys";
|
||||||
import { useAutoScroll } from "hooks/useAutoScroll";
|
import { useAutoScroll } from "hooks/useAutoScroll";
|
||||||
import { useBuildLogStream } from "hooks/useBuildLogStream";
|
import { useBuildLogStream } from "hooks/useBuildLogStream";
|
||||||
import { useClient } from "hooks/useClient";
|
import { useClient } from "hooks/useClient";
|
||||||
|
import { navigate, packageHref, useLocationQuery } from "hooks/useLocationQuery";
|
||||||
import type { LogRecord } from "models/LogRecord";
|
import type { LogRecord } from "models/LogRecord";
|
||||||
import type { RepositoryId } from "models/RepositoryId";
|
import type { RepositoryId } from "models/RepositoryId";
|
||||||
import React, { useEffect, useMemo, useState } from "react";
|
import React, { useEffect, useMemo, useState } from "react";
|
||||||
@@ -54,7 +55,8 @@ export default function BuildLogsTab({
|
|||||||
}: BuildLogsTabProps): React.JSX.Element {
|
}: BuildLogsTabProps): React.JSX.Element {
|
||||||
const client = useClient();
|
const client = useClient();
|
||||||
useBuildLogStream(packageBase, repository);
|
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 [anchorEl, setAnchorEl] = useState<HTMLElement | null>(null);
|
||||||
|
|
||||||
const { data: allLogs } = useQuery<LogRecord[]>({
|
const { data: allLogs } = useQuery<LogRecord[]>({
|
||||||
@@ -70,53 +72,47 @@ export default function BuildLogsTab({
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const grouped: Record<string, LogRecord & { minCreated: number }> = {};
|
const grouped = new Map<string, LogRecord & { minCreated: number }>();
|
||||||
for (const record of allLogs) {
|
for (const record of allLogs) {
|
||||||
const key = `${record.version}-${record.process_id}`;
|
const key = record.process_id;
|
||||||
const existing = grouped[key];
|
const existing = grouped.get(key);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
grouped[key] = { ...record, minCreated: record.created };
|
grouped.set(key, { ...record, minCreated: record.created });
|
||||||
} else {
|
} else {
|
||||||
existing.minCreated = Math.min(existing.minCreated, record.created);
|
existing.minCreated = Math.min(existing.minCreated, record.created);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.values(grouped)
|
return Array.from(grouped.values())
|
||||||
.sort((left, right) => right.minCreated - left.minCreated)
|
.sort((left, right) => right.minCreated - left.minCreated)
|
||||||
.map(record => ({
|
.map(record => ({
|
||||||
created: record.minCreated,
|
created: record.minCreated,
|
||||||
logs: convertLogs(
|
logs: convertLogs(
|
||||||
allLogs,
|
allLogs,
|
||||||
right => record.version === right.version && record.process_id === right.process_id,
|
right => record.process_id === right.process_id,
|
||||||
),
|
),
|
||||||
processId: record.process_id,
|
processId: record.process_id,
|
||||||
version: record.version,
|
version: record.version,
|
||||||
}));
|
}));
|
||||||
}, [allLogs]);
|
}, [allLogs]);
|
||||||
|
|
||||||
// Compute active index from selected version key, defaulting to newest (index 0)
|
const activeVersion = versions.find(run => run.processId === selectedProcessId);
|
||||||
const activeIndex = useMemo(() => {
|
const newestProcessId = versions[0]?.processId;
|
||||||
if (selectedVersionKey) {
|
|
||||||
const index = versions.findIndex(record => `${record.version}-${record.processId}` === selectedVersionKey);
|
|
||||||
if (index >= 0) {
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}, [versions, selectedVersionKey]);
|
|
||||||
|
|
||||||
const activeVersion = versions[activeIndex];
|
useEffect(() => {
|
||||||
const activeVersionKey = activeVersion ? `${activeVersion.version}-${activeVersion.processId}` : null;
|
if (selectedProcessId === null && newestProcessId) {
|
||||||
|
navigate(packageHref({ process_id: newestProcessId }));
|
||||||
|
}
|
||||||
|
}, [selectedProcessId, newestProcessId]);
|
||||||
|
|
||||||
// Refresh active version logs
|
// Refresh active version logs
|
||||||
const { data: versionLogs } = useQuery<LogRecord[]>({
|
const { data: versionLogs } = useQuery<LogRecord[]>({
|
||||||
placeholderData: keepPreviousData,
|
|
||||||
queryFn: activeVersion
|
queryFn: activeVersion
|
||||||
? () => client.fetch.fetchPackageLogs(
|
? () => client.fetch.fetchPackageLogs(
|
||||||
packageBase, repository, activeVersion.version, activeVersion.processId,
|
packageBase, repository, activeVersion.processId,
|
||||||
)
|
)
|
||||||
: skipToken,
|
: skipToken,
|
||||||
queryKey: QueryKeys.logsVersion(packageBase, repository, activeVersion?.version ?? "", activeVersion?.processId ?? ""),
|
queryKey: QueryKeys.logsRun(packageBase, repository, activeVersion?.processId ?? ""),
|
||||||
refetchOnMount: "always",
|
refetchOnMount: "always",
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -133,7 +129,7 @@ export default function BuildLogsTab({
|
|||||||
// Reset scroll tracking when active version changes
|
// Reset scroll tracking when active version changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
resetScroll();
|
resetScroll();
|
||||||
}, [activeVersionKey, resetScroll]);
|
}, [selectedProcessId, resetScroll]);
|
||||||
|
|
||||||
// Scroll to bottom on new logs
|
// Scroll to bottom on new logs
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -153,17 +149,18 @@ export default function BuildLogsTab({
|
|||||||
onClose={() => setAnchorEl(null)}
|
onClose={() => setAnchorEl(null)}
|
||||||
open={Boolean(anchorEl)}
|
open={Boolean(anchorEl)}
|
||||||
>
|
>
|
||||||
{versions.map((logs, index) =>
|
{versions.map(logs =>
|
||||||
<MenuItem
|
<MenuItem
|
||||||
key={`${logs.version}-${logs.processId}`}
|
key={logs.processId}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
setSelectedVersionKey(`${logs.version}-${logs.processId}`);
|
navigate(packageHref({ process_id: logs.processId }));
|
||||||
setAnchorEl(null);
|
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>,
|
</MenuItem>,
|
||||||
)}
|
)}
|
||||||
{versions.length === 0 &&
|
{versions.length === 0 &&
|
||||||
@@ -175,6 +172,9 @@ export default function BuildLogsTab({
|
|||||||
<Box sx={{ flex: 1 }}>
|
<Box sx={{ flex: 1 }}>
|
||||||
<CodeBlock
|
<CodeBlock
|
||||||
content={displayedLogs}
|
content={displayedLogs}
|
||||||
|
downloadFilename={activeVersion
|
||||||
|
? `${packageBase}-${activeVersion.version}-${activeVersion.processId}.log`
|
||||||
|
: undefined}
|
||||||
height={400}
|
height={400}
|
||||||
onScroll={handleScroll}
|
onScroll={handleScroll}
|
||||||
preRef={preRef}
|
preRef={preRef}
|
||||||
|
|||||||
@@ -83,12 +83,14 @@ export default function PackageTable(): React.JSX.Element {
|
|||||||
minWidth: 150,
|
minWidth: 150,
|
||||||
renderCell: params =>
|
renderCell: params =>
|
||||||
<Link
|
<Link
|
||||||
href={packageHref(currentRepository?.key ?? "", params.row.base)}
|
href={packageHref({ repository_id: currentRepository?.key, package: params.row.base })}
|
||||||
onClick={event => {
|
onClick={event => {
|
||||||
const modified = event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
|
const modified = event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
|
||||||
if (event.button === 0 && !modified) {
|
if (event.button === 0 && !modified) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
navigate(packageHref(currentRepository?.key ?? "", params.row.base));
|
navigate(packageHref({
|
||||||
|
repository_id: currentRepository?.key, package: params.row.base,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
tabIndex={params.tabIndex}
|
tabIndex={params.tabIndex}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ export function RepositoryProvider({ children }: { children: ReactNode }): React
|
|||||||
}, [repositories, repositoryKey]);
|
}, [repositories, repositoryKey]);
|
||||||
|
|
||||||
const setCurrentRepository = useCallback((repository: RepositoryId) => {
|
const setCurrentRepository = useCallback((repository: RepositoryId) => {
|
||||||
navigate(packageHref(repository.key, null));
|
navigate(packageHref({ repository_id: repository.key, package: null }));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const value = useMemo(() => ({
|
const value = useMemo(() => ({
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ export const QueryKeys = {
|
|||||||
|
|
||||||
logs: (packageBase: string, repository: RepositoryId) => ["logs", repository.key, packageBase] as const,
|
logs: (packageBase: string, repository: RepositoryId) => ["logs", repository.key, packageBase] as const,
|
||||||
|
|
||||||
logsVersion: (packageBase: string, repository: RepositoryId, version: string, processId: string) =>
|
logsRun: (packageBase: string, repository: RepositoryId, processId: string) =>
|
||||||
["logs", repository.key, packageBase, version, processId] as const,
|
["logs", repository.key, packageBase, processId] as const,
|
||||||
|
|
||||||
package: (packageBase: string, repository: RepositoryId) => ["packages", repository.key, packageBase] 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),
|
existing => appendLogRecord(existing, record),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Append to the version-specific cache
|
// Append to the run-specific cache
|
||||||
queryClient.setQueryData<LogRecord[]>(
|
queryClient.setQueryData<LogRecord[]>(
|
||||||
["logs", repository.key, packageBase, record.version, record.process_id],
|
["logs", repository.key, packageBase, record.process_id],
|
||||||
existing => appendLogRecord(existing, record),
|
existing => appendLogRecord(existing, record),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,13 +32,18 @@ export function useLocationQuery(): string {
|
|||||||
return useSyncExternalStore(subscribe, getSnapshot);
|
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);
|
const query = new URLSearchParams(window.location.search);
|
||||||
query.set("repository_id", repositoryKey);
|
for (const [key, value] of Object.entries(values)) {
|
||||||
if (packageBase) {
|
if (value) {
|
||||||
query.set("package", packageBase);
|
query.set(key, value);
|
||||||
} else {
|
} else {
|
||||||
query.delete("package");
|
query.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (values.package === null) {
|
||||||
|
query.delete("tab");
|
||||||
|
query.delete("process_id");
|
||||||
}
|
}
|
||||||
return `${window.location.pathname}?${query}`;
|
return `${window.location.pathname}?${query}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ export function useTableState(): UseTableStateResult {
|
|||||||
const selectedPackage = currentRepository && packageBase ? packageBase : null;
|
const selectedPackage = currentRepository && packageBase ? packageBase : null;
|
||||||
const setSelectedPackage = (base: string | null): void => {
|
const setSelectedPackage = (base: string | null): void => {
|
||||||
if (currentRepository) {
|
if (currentRepository) {
|
||||||
navigate(packageHref(currentRepository.key, base));
|
navigate(packageHref({ repository_id: currentRepository.key, package: base }));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const [searchText, setSearchText] = useState("");
|
const [searchText, setSearchText] = useState("");
|
||||||
|
|||||||
Reference in new issue
Block a user