Skip to content

Commit

Permalink
Add useMarkdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom committed Apr 24, 2024
1 parent 91a94a6 commit abe443f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 47 deletions.
44 changes: 44 additions & 0 deletions src/ui/hooks/useMarkdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';

import {DL} from 'ui';

import {YfmWrapper} from '../components/YfmWrapper/YfmWrapper';
import {getSdk} from '../libs/schematic-sdk';

type Props = {
value: string;
};

const MarkdownCollection = new Map();
async function renderMarkdown(value: string) {
if (!MarkdownCollection.has(value)) {
try {
const response = await getSdk().mix.renderMarkdown({text: value, lang: DL.USER_LANG});
const yfmString = response.result;
MarkdownCollection.set(value, yfmString);
} catch (e) {
console.error('useMarkdown failed ', e);
}
}

return MarkdownCollection.get(value);
}

export const useMarkdown = (props: Props) => {
const {value} = props;
const [markdown, setMarkdown] = React.useState<React.ReactNode>('');
const [isLoading, setIsLoading] = React.useState<boolean>(true);

React.useEffect(() => {
if (!value) {
return;
}

renderMarkdown(value).then((val) => {
setMarkdown(<YfmWrapper content={val} setByInnerHtml={true} />);
setIsLoading(false);
});
}, [value]);

return {markdown, isLoading};
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TableRow,
isMarkupItem,
} from 'shared';
import {useMarkdown} from 'ui/hooks/useMarkdown';

import {Markup} from '../../../../../../../../components/Markup';
import {markupToRawString} from '../../../../../../modules/table';
Expand Down Expand Up @@ -84,6 +85,13 @@ const SortIcon = ({direction}: SortIconProps) => {
);
};

const Markdown = (props: {value: string}) => {
const {value} = props;
const {markdown} = useMarkdown({value});

return <React.Fragment>{markdown}</React.Fragment>;
};

const diffFormatter = (
value: number,
{precision, diff_formatter: formatter}: Omit<DiffTableColumn, 'type'>,
Expand Down Expand Up @@ -435,7 +443,7 @@ export const getColumnsAndNames = ({
<HelpPopover
className={b('column-hint')}
size={'s'}
content={get(column, 'hint')}
content={<Markdown value={hint} />}
/>
)}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,17 @@ import {HelpPopover} from '@gravity-ui/components';
import block from 'bem-cn-lite';
import PropTypes from 'prop-types';
import {ControlQA} from 'shared';
import {YfmWrapper} from 'ui/components/YfmWrapper/YfmWrapper';
import {DL} from 'ui/constants';
import {getSdk} from 'ui/libs/schematic-sdk';
import {useMarkdown} from 'ui/hooks/useMarkdown';
import {isMobileView} from 'ui/utils/mobile';

import {CONTROL_TYPE} from '../../../modules/constants/constants';

const b = block('chartkit-control-item');

const TooltipWithMarkdown = (props) => {
const {markdown} = props;
const [tooltipText, setTooltipText] = React.useState();
React.useEffect(() => {
getSdk()
.mix.renderMarkdown({text: markdown, lang: DL.USER_LANG})
.then((response) => {
const yfmString = response.result;
setTooltipText(<YfmWrapper content={yfmString} setByInnerHtml={true} />);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return <React.Fragment>{tooltipText}</React.Fragment>;
const Markdown = (props) => {
const {markdown} = useMarkdown({value: props.value});
return <React.Fragment>{markdown}</React.Fragment>;
};

function withWrapForControls(WrappedComponent) {
Expand Down Expand Up @@ -74,7 +62,7 @@ function withWrapForControls(WrappedComponent) {
{label}
{hint && (
<HelpPopover
content={<TooltipWithMarkdown markdown={hint} />}
content={<Markdown value={hint} />}
className={b('hint')}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@

&__operation-checkbox {
margin-right: 8px;

&_top {
align-self: flex-start;
margin-top: 4px;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {Checkbox} from '@gravity-ui/uikit';
import block from 'bem-cn-lite';
import {I18n} from 'i18n';
import {useDispatch, useSelector} from 'react-redux';
import {FieldWrapper} from 'ui/components/FieldWrapper/FieldWrapper';
import {registry} from 'ui/registry';
import {setSelectorDialogItem} from 'units/dash/store/actions/dashTyped';
import {
Expand All @@ -20,7 +19,7 @@ const i18n = I18n.keyset('dash.control-dialog.edit');

export const HintRow = () => {
const dispatch = useDispatch();
const {showHint, hint, validation} = useSelector(selectSelectorDialog);
const {showHint, hint} = useSelector(selectSelectorDialog);
const isFieldDisabled = useSelector(selectIsControlConfigurationDisabled);
const datasetField = useSelector(getDatasetField);

Expand All @@ -39,21 +38,18 @@ export const HintRow = () => {
return (
<FormRow label={i18n('field_hint')}>
<div className={b('operation-container')}>
<FieldWrapper error={validation.title}>
<div style={{display: 'flex', gap: '8px'}}>
<Checkbox
disabled={isFieldDisabled}
onUpdate={handleUpdateEnable}
checked={showHint}
size={'l'}
/>
<MarkdownControl
value={hint ?? datasetField?.description ?? ''}
onChange={handleUpdateText}
disabled={isFieldDisabled || !showHint}
/>
</div>
</FieldWrapper>
<Checkbox
disabled={isFieldDisabled}
onUpdate={handleUpdateEnable}
checked={showHint}
size={'l'}
className={b('operation-checkbox', {top: true})}
/>
<MarkdownControl
value={hint ?? datasetField?.description ?? ''}
onChange={handleUpdateText}
disabled={isFieldDisabled || !showHint}
/>
</div>
</FormRow>
);
Expand Down
26 changes: 14 additions & 12 deletions src/ui/units/wizard/components/Dialogs/DialogField/DialogField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,20 @@ class DialogField extends React.PureComponent<DialogFieldInnerProps, DialogField
/>
}
/>
<DialogFieldRow
title={''}
setting={
<MarkdownControl
value={text ?? item?.description}
onChange={(value) =>
this.setState({hintSettings: {enabled, text: value}})
}
disabled={!enabled}
/>
}
/>
{enabled && (
<DialogFieldRow
title={''}
setting={
<MarkdownControl
value={text ?? item?.description}
onChange={(value) =>
this.setState({hintSettings: {enabled, text: value}})
}
disabled={!enabled}
/>
}
/>
)}
</React.Fragment>
);
}
Expand Down

0 comments on commit abe443f

Please sign in to comment.