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
100 changes: 100 additions & 0 deletions src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React from "react";
import styled, { css } from "styled-components";

import { CopyButton } from "./CopyButton";

const CodeContainer = styled.div`
position: relative;
width: 100%;
min-width: 0;
`;

const Pre = styled.pre<{ $maxHeight?: string; $copyable?: boolean }>`
counter-reset: line;
overflow-y: auto;

max-height: ${({ $maxHeight = "none" }) => $maxHeight};
margin: 0;
padding: ${({ theme }) => theme.spacing.sm} 0;
padding-right: ${({ $copyable }) => ($copyable ? "2.5rem" : 0)};
border: 1px solid ${({ theme }) => theme.colors.border.light};
border-radius: ${({ theme }) => theme.radius.md};

font: ${({ theme }) => theme.typography.code.font};
color: ${({ theme }) => theme.colors.text.primary};

background: ${({ theme }) => theme.colors.background.secondary};
`;

// Long lines wrap, so the block never scrolls sideways. Numbers stay out of a copy.
const Line = styled.span<{ $numbered: boolean }>`
display: block;
padding-inline: ${({ theme }) => theme.spacing.md};
white-space: pre-wrap;
overflow-wrap: anywhere;

${({ $numbered, theme }) =>
$numbered &&
css`
counter-increment: line;
display: grid;
grid-template-columns: 2.5em minmax(0, 1fr);
padding-left: ${theme.spacing.xs};

&::before {
content: counter(line);
padding-right: ${theme.spacing.sm};
color: ${theme.colors.text.tertiary};
text-align: right;
user-select: none;
}
`}
`;

const CopyCorner = styled.div`
position: absolute;
top: ${({ theme }) => theme.spacing.xs};
right: ${({ theme }) => theme.spacing.xs};
`;

export interface CodeBlockProps extends React.HTMLAttributes<HTMLDivElement> {
$code: string;
$copyable?: boolean;
$maxHeight?: string;
}

/**
* Read-only block of code or preformatted text. Lines are numbered when there is more than one.
* @param $code - The text to show.
* @param $copyable - Shows a copy button in the top-right corner (default: false).
* @param $maxHeight - CSS max-height; longer content scrolls (default: none).
* @example
* <CodeBlock $code={script} $copyable $maxHeight="20rem" />
*/
export const CodeBlock: React.FC<CodeBlockProps> = ({
$code,
$copyable = false,
$maxHeight,
...rest
}) => {
const lines = $code.split("\n");
const numbered = lines.length > 1;
return (
<CodeContainer {...rest}>
<Pre $maxHeight={$maxHeight} $copyable={$copyable}>
{lines.map((line, index) => (
<Line key={index} $numbered={numbered}>
{line || " "}
</Line>
))}
</Pre>
{$copyable && (
<CopyCorner>
<CopyButton $value={$code} title="Copy" />
</CopyCorner>
)}
</CodeContainer>
);
};

export default CodeBlock;
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Export all styled components for easy importing
export * from "./Badge";
export * from "./Button";
export * from "./CodeBlock";
export * from "./CodeEditor";
export * from "./ConfirmDialog";
export * from "./Containers";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ export const DetailedItemSummary = styled.div`
color: ${({ theme }) => theme.colors.text.secondary};
`;

export const CodeBlock = styled.pre`
width: 100%;
background: ${({ theme }) => theme.colors.background.secondary};
border: 1px solid ${({ theme }) => theme.colors.border.light};
border-radius: ${({ theme }) => theme.radius.md};
padding: ${({ theme }) => theme.spacing.md};
font-family: ${({ theme }) => theme.fonts.family.mono};
font-size: ${({ theme }) => theme.fonts.size.sm};
color: ${({ theme }) => theme.colors.text.primary};
overflow: auto;
`;

export const VariableListContainer = styled.div`
width: 100%;
display: flex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from "react";

import {
Badge,
CodeBlock,
CopyButton,
DetailsGrid,
DetailsItem,
Expand All @@ -28,7 +29,6 @@ import {
formatDuration,
} from "@/utils";
import {
CodeBlock,
DetailedContainer,
DetailedItemSummary,
Placeholder,
Expand Down Expand Up @@ -200,12 +200,13 @@ export const DetailsTabView: React.FC<DetailsTabViewProps> = ({ steps }) => {
{Array.isArray(step.commands_b64) &&
step.commands_b64.length > 0 ? (
step.commands_b64.map((cmd, i) => (
<CodeBlock key={`cmd-${i}`}>
{decodeBase64(cmd) || cmd}
</CodeBlock>
<CodeBlock
key={`cmd-${i}`}
$code={decodeBase64(cmd) || cmd}
/>
))
) : (
<CodeBlock>// no commands</CodeBlock>
<CodeBlock $code="// no commands" />
)}
</DetailsItem>
<DetailsItem style={{ width: "100%" }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Badge, ThemeVariant } from "@/components";
import type { Command } from "@/types";

import { Description } from "./PlaybookDetailPage.styles";
import { CodeField, DetailField, Section } from "./PlaybookDetails";
import { readContent, readText } from "./stepFields";

const CommandSection = ({ command, title }: { command: Command; title: string }) => {
const body = readText(command.command, command.command_b64);
const content = readText(command.content, command.content_b64);
const formatted = content && readContent(content.text);
const headers = Object.entries(command.headers ?? {});

return (
<Section title={title}>
<DetailField label="Type">
<Badge $variant={ThemeVariant.Primary}>{command.type}</Badge>
</DetailField>
{command.version && <DetailField label="Version">{command.version}</DetailField>}
{command.playbook_activity && (
<DetailField label="Playbook activity" wide>
{command.playbook_activity}
</DetailField>
)}
{command.description && (
<DetailField label="Description" wide>
<Description>{command.description}</Description>
</DetailField>
)}
{body && (
<CodeField
label={body.decoded ? "Command (decoded from base64)" : "Command"}
code={body.text}
/>
)}
{headers.length > 0 && (
<CodeField
label="Headers"
code={headers
.map(([name, values]) => `${name}: ${values.join(", ")}`)
.join("\n")}
/>
)}
{content && formatted && (
<CodeField
label={content.decoded ? "Content (decoded from base64)" : "Content"}
code={formatted.code}
warning={formatted.invalid ? "This content is not valid JSON." : undefined}
/>
)}
</Section>
);
};

/** The commands of an action step, as they will be sent. */
export const CommandList = ({ commands }: { commands: Command[] }) => (
<>
{commands.map((command, index) => (
<CommandSection
key={index}
command={command}
title={commands.length === 1 ? "Command" : `Command ${index + 1}`}
/>
))}
</>
);
Loading
Loading