Skip to content

ref(onboarding): Introduce content blocks #95224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {css} from '@emotion/react';
import styled from '@emotion/styled';

import {Alert} from 'sentry/components/core/alert';
import {useRendererContext} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/rendererContext';
import type {
BlockRenderers,
ContentBlock,
} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/types';
import {
CssVariables,
renderBlocks,
} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/utils';
import {
OnboardingCodeSnippet,
TabbedCodeSnippet,
} from 'sentry/components/onboarding/gettingStartedDoc/onboardingCodeSnippet';

const baseBlockStyles = css`
:not(:last-child) {
margin-bottom: var(${CssVariables.BLOCK_SPACING});
}
`;

function AlertBlock({
alertType,
text,
showIcon,
system,
trailingItems,
}: Extract<ContentBlock, {type: 'alert'}>) {
return (
<div css={baseBlockStyles}>
<Alert
type={alertType}
showIcon={showIcon}
system={system}
trailingItems={trailingItems}
>
{text}
</Alert>
</div>
);
}

function CodeBlock(block: Extract<ContentBlock, {type: 'code'}>) {
if ('code' in block) {
return (
<div css={baseBlockStyles}>
<OnboardingCodeSnippet language={block.language}>
{block.code}
</OnboardingCodeSnippet>
</div>
);
}

const tabsWithValues = block.tabs.map(tab => ({
...tab,
value: tab.label,
}));

return (
<div css={baseBlockStyles}>
<TabbedCodeSnippet tabs={tabsWithValues} />
</div>
);
}

function ConditionalBlock({
condition,
content,
}: Extract<ContentBlock, {type: 'conditional'}>) {
const {renderers} = useRendererContext();

if (condition) {
return renderBlocks(content, renderers);
}

return null;
}

function CustomBlock(block: Extract<ContentBlock, {type: 'custom'}>) {
return <div css={baseBlockStyles}>{block.content}</div>;
}

function TextBlock(block: Extract<ContentBlock, {type: 'text'}>) {
return <TextBlockWrapper>{block.text}</TextBlockWrapper>;
}

const TextBlockWrapper = styled('div')`
${baseBlockStyles}

code:not([class*='language-']) {
color: ${p => p.theme.pink400};
}
`;

export const defaultRenderers: BlockRenderers = {
text: TextBlock,
code: CodeBlock,
custom: CustomBlock,
alert: AlertBlock,
conditional: ConditionalBlock,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {useMemo} from 'react';
import styled from '@emotion/styled';

import {defaultRenderers} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/defaultRenderers';
import {RendererContext} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/rendererContext';
import type {
BlockRenderers,
ContentBlock,
} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/types';
import {
CssVariables,
renderBlocks,
} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/utils';
import {space} from 'sentry/styles/space';

interface Props {
/**
* The content blocks to be rendered.
*/
contentBlocks: Array<ContentBlock | null | undefined>;
/**
* The class name to be applied to the root element.
*/
className?: string;
/**
* A custom renderer for the content blocks.
* If not provided, the default renderer will be used.
* The renderers object must have a key for each content block type.
*/
renderers?: Partial<BlockRenderers>;
/**
* The spacing between the content blocks.
* Available as a CSS variable `var(${CssVariables.BLOCK_SPACING})` for styling of child elements.
*/
spacing?: string;
}

const NO_RENDERERS = {};
const DEFAULT_SPACING = space(2);

export function ContentBlocksRenderer({
contentBlocks,
renderers: customRenderers = NO_RENDERERS,
spacing = DEFAULT_SPACING,
className,
}: Props) {
const contextValue = useMemo(
() => ({
renderers: {
...defaultRenderers,
...customRenderers,
},
}),
[customRenderers]
);
return (
<RendererContext value={contextValue}>
<Wrapper className={className} spacing={spacing}>
{renderBlocks(contentBlocks, contextValue.renderers)}
</Wrapper>
</RendererContext>
);
}

const Wrapper = styled('div')<{spacing: string}>`
${CssVariables.BLOCK_SPACING}: ${p => p.spacing};
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {createContext, useContext} from 'react';

import type {BlockRenderers} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/types';

export const RendererContext = createContext<
| undefined
| {
renderers: BlockRenderers;
}
>(undefined);

export const useRendererContext = () => {
const context = useContext(RendererContext);
if (!context) {
throw new Error('useRendererContext must be used within a RendererContext');
}
return context;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type {AlertProps} from 'sentry/components/core/alert';
import type {CodeSnippetTab} from 'sentry/components/onboarding/gettingStartedDoc/onboardingCodeSnippet';

type BaseBlock<T extends string> = {
type: T;
};

/**
* Renders the Alert component
*/
type AlertBlock = BaseBlock<'alert'> & {
alertType: AlertProps['type'];
text: React.ReactNode;
type: 'alert';
icon?: AlertProps['icon'];
showIcon?: AlertProps['showIcon'];
system?: AlertProps['system'];
trailingItems?: AlertProps['trailingItems'];
};

// The value of the tab is omitted and inferred from the label in the renderer
type CodeTabWithoutValue = Omit<CodeSnippetTab, 'value'>;
type SingleCodeBlock = BaseBlock<'code'> & Omit<CodeTabWithoutValue, 'label'>;
type MultipleCodeBlock = BaseBlock<'code'> & {
tabs: CodeTabWithoutValue[];
};
/**
* Code blocks can either render a single code snippet or multiple code snippets in a tabbed interface.
*/
type CodeBlock = SingleCodeBlock | MultipleCodeBlock;

/**
* Conditional blocks are used to render content based on a condition.
*/
type ConditionalBlock = BaseBlock<'conditional'> & {
condition: boolean;
content: ContentBlock[];
};

/**
* Text blocks are used to render one paragraph of text.
*/
type TextBlock = BaseBlock<'text'> & {
/**
* Only meant for text or return values of translation functions (t, tct, tn).
*
* **Do not** use this with custom react elements but instead use the `custom` block type.
*/
text: React.ReactNode;
};

/**
* Custom blocks can be used to render any content that is not covered by the other block types.
*/
type CustomBlock = BaseBlock<'custom'> & {
content: React.ReactNode;
};

export type ContentBlock =
| AlertBlock
| CodeBlock
| ConditionalBlock
| CustomBlock
| TextBlock;

export type BlockRenderers = {
[key in ContentBlock['type']]: (
block: Extract<ContentBlock, {type: key}>
) => React.ReactNode;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type {
BlockRenderers,
ContentBlock,
} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/types';

export enum CssVariables {
BLOCK_SPACING = '--block-spacing',
}

export function renderBlocks(
contentBlocks: Array<ContentBlock | null | undefined>,
renderers: BlockRenderers
) {
return contentBlocks.map((block, index) => {
if (!block) {
return null;
}
// Need to cast here as ts bugs out on the return type and does not allow assigning the key prop
const RendererComponent = renderers[block.type] as (
block: ContentBlock
) => React.ReactNode;

// The index actually works well as a key here
// as long as the conditional block is used instead of JS logic to edit the blocks array
return <RendererComponent {...block} key={index} />;
});
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {Fragment, useCallback, useState} from 'react';
import {Fragment, useCallback, useMemo, useState} from 'react';
import {createPortal} from 'react-dom';
import beautify from 'js-beautify';

import {CodeSnippet} from 'sentry/components/codeSnippet';
import {AuthTokenGenerator} from 'sentry/components/onboarding/gettingStartedDoc/authTokenGenerator';
import {PACKAGE_LOADING_PLACEHOLDER} from 'sentry/utils/gettingStartedDocs/getPackageVersion';

interface OnboardingCodeSnippetProps
extends Omit<React.ComponentProps<typeof CodeSnippet>, 'onAfterHighlight'> {}
Expand Down Expand Up @@ -39,11 +40,18 @@ export function OnboardingCodeSnippet({
setAuthTokenNodes(replaceTokensWithSpan(element));
}, []);

const partialLoading = useMemo(
() => children.includes(PACKAGE_LOADING_PLACEHOLDER),
[children]
);

Comment on lines +43 to +46
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we had a separate prop partiallyLoading on the configuration object, which was missing most of the time. I decided to drop it from the new format and rather autodetect the loading placeholder in the snippet. This solution is maybe a bit hacky but is much better DX when writing docs.

return (
<Fragment>
<CodeSnippet
dark
language={language}
hideCopyButton={partialLoading}
disableUserSelection={partialLoading}
{...props}
onAfterHighlight={handleAfterHighlight}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Alert} from 'sentry/components/core/alert';
import ExternalLink from 'sentry/components/links/externalLink';
import type {ContentBlock} from 'sentry/components/onboarding/gettingStartedDoc/types';
import {tct} from 'sentry/locale';

export default function TracePropagationMessage() {
Expand All @@ -16,3 +17,16 @@ export default function TracePropagationMessage() {
</Alert>
);
}

export const tracePropagationBlock: ContentBlock = {
type: 'alert',
alertType: 'info',
text: tct(
`To see replays for backend errors, ensure that you have set up trace propagation. To learn more, [link:read the docs].`,
{
link: (
<ExternalLink href="https://docs.sentry.io/product/explore/session-replay/web/getting-started/#replays-for-backend-errors" />
),
}
),
};
10 changes: 8 additions & 2 deletions static/app/components/onboarding/gettingStartedDoc/step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Fragment, useState} from 'react';
import styled from '@emotion/styled';

import {Button} from 'sentry/components/core/button';
import {ContentBlocksRenderer} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/renderer';
import {
OnboardingCodeSnippet,
TabbedCodeSnippet,
Expand Down Expand Up @@ -64,17 +65,22 @@ export function Step({
title,
type,
configurations,
content,
additionalInfo,
description,
onOptionalToggleClick,
collapsible = false,
trailingItems,
codeHeader,
...props
}: React.HTMLAttributes<HTMLDivElement> & OnboardingStep) {
}: Omit<React.HTMLAttributes<HTMLDivElement>, 'content'> & OnboardingStep) {
const [showOptionalConfig, setShowOptionalConfig] = useState(false);

const config = (
const config = content ? (
<ContentWrapper>
<ContentBlocksRenderer contentBlocks={content} />
</ContentWrapper>
) : (
<ContentWrapper>
{description && <Description>{description}</Description>}

Expand Down
Loading
Loading