-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
ArthurKnaus
wants to merge
10
commits into
master
Choose a base branch
from
aknaus/ref/onboarding/introduce-content-blocks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c98989b
ref(onboarding): Introduce content blocks
ArthurKnaus 3ad0b8a
remove heading styles from text block
ArthurKnaus c33ce48
explain casting on the RendererComponent
ArthurKnaus d9e8a64
support content blocks in performance and agent monitoring onboarding
ArthurKnaus 3a38cd0
split content block related code into separate folder
ArthurKnaus 4cab177
use css var enum
ArthurKnaus c856a91
adjust comment
ArthurKnaus 7f322b3
remove String() from key
ArthurKnaus 01529f0
apply review feedback
ArthurKnaus 0e589ae
fix typo
ArthurKnaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
static/app/components/onboarding/gettingStartedDoc/contentBlocks/defaultRenderers.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
67 changes: 67 additions & 0 deletions
67
static/app/components/onboarding/gettingStartedDoc/contentBlocks/renderer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
`; |
18 changes: 18 additions & 0 deletions
18
static/app/components/onboarding/gettingStartedDoc/contentBlocks/rendererContext.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
70 changes: 70 additions & 0 deletions
70
static/app/components/onboarding/gettingStartedDoc/contentBlocks/types.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
27 changes: 27 additions & 0 deletions
27
static/app/components/onboarding/gettingStartedDoc/contentBlocks/utils.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} />; | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.