-
-
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 8 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,108 @@ | ||||||||||
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 { | ||||||||||
BlockRenderer, | ||||||||||
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 {renderer} = useRendererContext(); | ||||||||||
|
||||||||||
if (condition) { | ||||||||||
return renderBlocks(content, renderer); | ||||||||||
} | ||||||||||
|
||||||||||
return null; | ||||||||||
} | ||||||||||
|
||||||||||
function CustomBlock(block: Extract<ContentBlock, {type: 'custom'}>) { | ||||||||||
return <CustomBlockWrapper>{block.content}</CustomBlockWrapper>; | ||||||||||
} | ||||||||||
|
||||||||||
const CustomBlockWrapper = styled('div')` | ||||||||||
${baseBlockStyles} | ||||||||||
`; | ||||||||||
|
||||||||||
function TextBlock(block: Extract<ContentBlock, {type: 'text'}>) { | ||||||||||
return <TextBlockWrapper>{block.text}</TextBlockWrapper>; | ||||||||||
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.
Suggested change
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. 👍 though I will move the style definition out of the render function so they are not serialized on every render 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. ah no, it needs the theme so I cant transform it to 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. you can use 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. tbh i find
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. I don't have have a strong opinion here, but I am always in favour of having less code 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. Out of curiosity, I checked the implementation of |
||||||||||
} | ||||||||||
|
||||||||||
const TextBlockWrapper = styled('div')` | ||||||||||
${baseBlockStyles} | ||||||||||
|
||||||||||
code:not([class*='language-']) { | ||||||||||
color: ${p => p.theme.pink400}; | ||||||||||
} | ||||||||||
`; | ||||||||||
|
||||||||||
export const defaultBlocks: BlockRenderer = { | ||||||||||
text: TextBlock, | ||||||||||
code: CodeBlock, | ||||||||||
custom: CustomBlock, | ||||||||||
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. I am a bit confused about having a customBlock inside of defaultBlocks. is it a default custom block? 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. If i get this correctly it is a block component that is used by default when the type of the block is custom. 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. yes, it is the default renderer for the block with type the block type /**
* 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;
}; 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. yes I get that, just the name is confusing 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. maybe it helps renaming |
||||||||||
alert: AlertBlock, | ||||||||||
conditional: ConditionalBlock, | ||||||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {useMemo} from 'react'; | ||
import styled from '@emotion/styled'; | ||
|
||
import {defaultBlocks} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/defaultBlocks'; | ||
import {RendererContext} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/rendererContext'; | ||
import type { | ||
BlockRenderer, | ||
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 renderer object must have a key for each content block type. | ||
*/ | ||
renderer?: Partial<BlockRenderer>; | ||
/** | ||
* The spacing between the content blocks. | ||
* Available as a CSS variable `var(${CssVariables.BLOCK_SPACING})` for styling of child elements. | ||
*/ | ||
spacing?: string; | ||
} | ||
|
||
const NO_RENDERER = {}; | ||
const DEFAULT_SPACING = space(2); | ||
|
||
export function ContentBlocksRenderer({ | ||
contentBlocks, | ||
renderer: customRenderer = NO_RENDERER, | ||
spacing = DEFAULT_SPACING, | ||
className, | ||
}: Props) { | ||
const renderer = useMemo( | ||
() => ({ | ||
...defaultBlocks, | ||
...customRenderer, | ||
}), | ||
[customRenderer] | ||
); | ||
return ( | ||
<RendererContext value={{renderer}}> | ||
<Wrapper className={className} spacing={spacing}> | ||
{renderBlocks(contentBlocks, renderer)} | ||
</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 {BlockRenderer} from 'sentry/components/onboarding/gettingStartedDoc/contentBlocks/types'; | ||
|
||
export const RendererContext = createContext< | ||
| undefined | ||
| { | ||
renderer: BlockRenderer; | ||
} | ||
>(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 BlockRenderer = { | ||
[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 { | ||
BlockRenderer, | ||
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>, | ||
renderer: BlockRenderer | ||
) { | ||
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 = renderer[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} | ||
> | ||
|
Uh oh!
There was an error while loading. Please reload this page.