-
-
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
Changes from 10 commits
c98989b
3ad0b8a
c33ce48
d9e8a64
3a38cd0
4cab177
c856a91
7f322b3
01529f0
0e589ae
0a55fb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use a styled component here? I would suggest finding a more specific naming here given that these are exported, you wouldn't want them to pollute LSP suggestions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is also exported for usage not just defining them. |
||
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'> {} | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we had a separate prop |
||
return ( | ||
<Fragment> | ||
<CodeSnippet | ||
dark | ||
language={language} | ||
hideCopyButton={partialLoading} | ||
disableUserSelection={partialLoading} | ||
{...props} | ||
onAfterHighlight={handleAfterHighlight} | ||
> | ||
|
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.
Can you use a <Alert.Container> here? It should add the margin for you
Uh oh!
There was an error while loading. Please reload this page.
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.
But I do not want to use the margin value which is hardcoded in
Alert.Container
. We need the margin between the content blocks to be equal and respect the css variable.