Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/Terrabuild.UI/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ const App = () => {
const fitAddon = useRef<FitAddon | null>(null);
const logAbort = useRef<AbortController | null>(null);
const terminalReady = useRef(false);
const activeProjectRef = useRef<string | null>(null);
const selectedTargetKeyRef = useRef<string | null>(null);
const pendingTargetRef = useRef<{ key: string; target: GraphNode } | null>(
null
);
Expand Down Expand Up @@ -785,10 +787,15 @@ const App = () => {
]);

const loadProjectResults = async (project: ProjectNode) => {
const shouldSyncLog = showTerminal && selectedTargetKey !== null;
const previousTargetKey = selectedTargetKeyRef.current;
const shouldSyncLog = showTerminal && previousTargetKey !== null;
activeProjectRef.current = project.id;
setSelectedProject(project);
setSelectedNodeId(project.id);
setSelectedTargetKey(null);
if (!showTerminal) {
setSelectedTargetKey(null);
selectedTargetKeyRef.current = null;
}
const freshResults: Record<string, TargetSummary> = {};
await Promise.all(
project.targets.map(async (node) => {
Expand All @@ -809,10 +816,13 @@ const App = () => {
if (Object.keys(freshResults).length > 0) {
setNodeResults((prev) => ({ ...prev, ...freshResults }));
}
if (activeProjectRef.current !== project.id) {
return;
}
if (!shouldSyncLog || project.targets.length === 0) {
return;
}
const [_, selectedTargetName] = selectedTargetKey.split("/", 3);
const [_, selectedTargetName] = previousTargetKey.split("/", 3);
const matchingTarget = project.targets.find(
(target) => target.target === selectedTargetName
);
Expand Down Expand Up @@ -846,7 +856,7 @@ const App = () => {
}
const nextKey =
`${nextTarget.projectHash}/${nextTarget.target}/${nextTarget.targetHash}`;
await showTargetLog(nextKey, nextTarget);
await showTargetLog(nextKey, nextTarget, true);
};

const loadTargetLog = async (key: string, target: GraphNode) => {
Expand All @@ -860,29 +870,44 @@ const App = () => {
setBuildEndedAt(null);
}
setSelectedTargetKey(key);
selectedTargetKeyRef.current = key;
terminal.current.reset();
setShowTerminal(true);
try {
const response = await fetch(
`/api/build/target-log/${target.projectHash}/${target.target}/${target.targetHash}`
);
if (!response.ok) {
if (selectedTargetKeyRef.current !== key) {
return;
}
terminal.current.write("No cached log available.\n");
terminal.current.scrollToBottom();
return;
}
const log = await response.text();
if (selectedTargetKeyRef.current !== key) {
return;
}
terminal.current.write(log.length > 0 ? log : "No cached log available.\n");
terminal.current.scrollToBottom();
} catch {
if (selectedTargetKeyRef.current !== key) {
return;
}
terminal.current.write("Failed to load cached log.\n");
terminal.current.scrollToBottom();
}
};

const showTargetLog = async (key: string, target: GraphNode) => {
if (selectedTargetKey === key) {
const showTargetLog = async (
key: string,
target: GraphNode,
forceShow: boolean = false
) => {
if (!forceShow && selectedTargetKey === key) {
setSelectedTargetKey(null);
selectedTargetKeyRef.current = null;
setShowTerminal(false);
setBuildEndedAt(null);
return;
Expand All @@ -896,6 +921,7 @@ const App = () => {
};

useEffect(() => {
selectedTargetKeyRef.current = selectedTargetKey;
if (!selectedTargetKey) {
return;
}
Expand Down Expand Up @@ -1040,7 +1066,10 @@ const App = () => {
onNodeClick={(_, node) =>
loadProjectResults(node.data.meta as ProjectNode)
}
onNodeDragStart={(_, node) => setDraggedNodeId(node.id)}
onNodeDragStart={(_, node) => {
setDraggedNodeId(node.id);
loadProjectResults(node.data.meta as ProjectNode);
}}
onNodeDragStop={() => setDraggedNodeId(null)}
onReflow={() => {
setManualPositions({});
Expand All @@ -1053,7 +1082,12 @@ const App = () => {
showTerminal={showTerminal}
buildRunning={buildRunning}
title={buildLogTitle}
onHide={() => setShowTerminal(false)}
onHide={() => {
setShowTerminal(false);
setSelectedTargetKey(null);
selectedTargetKeyRef.current = null;
setBuildEndedAt(null);
}}
terminalRef={terminalRef}
background={terminalBackground}
/>
Expand Down
1 change: 1 addition & 0 deletions src/Terrabuild.UI/src/components/BuildLogPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const BuildLogPanel = ({
size="lg"
variant="subtle"
onClick={onHide}
disabled={buildRunning}
aria-label="Hide terminal"
>
<IconSquareRoundedChevronDown size={18} />
Expand Down