Skip to content
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

feat: Add parseJson Handlebars Helper to Support Processing Nested JSON Data #31998

Open
wants to merge 5 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
20 changes: 20 additions & 0 deletions superset-frontend/plugins/plugin-chart-handlebars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ more details.
└── types
└── external.d.ts
```

### Available Handlebars Helpers in Superset

Below, you will find a list of all currently registered helpers in the Handlebars plugin for Superset. These helpers are registered and managed in the file [`HandlebarsViewer.tsx`](./path/to/HandlebarsViewer.tsx).

#### List of Registered Helpers:

1. **`dateFormat`**: Formats a date using a specified format.
- **Usage**: `{{dateFormat my_date format="MMMM YYYY"}}`
- **Default format**: `YYYY-MM-DD`.

2. **`stringify`**: Converts an object into a JSON string or returns a string representation of non-object values.
- **Usage**: `{{stringify myObj}}`.

3. **`formatNumber`**: Formats a number using locale-specific formatting.
- **Usage**: `{{formatNumber number locale="en-US"}}`.
- **Default locale**: `en-US`.

4. **`parseJson`**: Parses a JSON string into a JavaScript object.
- **Usage**: `{{parseJson jsonString}}`.
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,18 @@ Handlebars.registerHelper(
},
);

// usage: {{parseJson jsonString}}
Handlebars.registerHelper('parseJson', (jsonString: string) => {
try {
return JSON.parse(jsonString);
} catch (error) {
if (error instanceof Error) {
error.message = `Invalid JSON string: ${error.message}`;
throw error;
}
throw new Error(`Invalid JSON string: ${String(error)}`);
}
});
AdrianKoszalka marked this conversation as resolved.
Show resolved Hide resolved

Helpers.registerHelpers(Handlebars);
HandlebarsGroupBy.register(Handlebars);
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
ControlSetItem,
CustomControlConfig,
sharedControls,
InfoTooltipWithTrigger,
} from '@superset-ui/chart-controls';
import { t, validateNonEmpty } from '@superset-ui/core';
import { t, validateNonEmpty, useTheme, SafeMarkdown } from '@superset-ui/core';
import { CodeEditor } from '../../components/CodeEditor/CodeEditor';
import { ControlHeader } from '../../components/ControlHeader/controlHeader';
import { debounceFunc } from '../../consts';
Expand All @@ -33,13 +34,40 @@ interface HandlebarsCustomControlProps {
const HandlebarsTemplateControl = (
props: CustomControlConfig<HandlebarsCustomControlProps>,
) => {
const theme = useTheme();

const val = String(
props?.value ? props?.value : props?.default ? props?.default : '',
);

const helperDescriptionsHeader = t('Available Handlebars Helpers in Superset:');

const helperDescriptions = [
{ key: 'dateFormat', descKey: 'Formats a date using a specified format.' },
{ key: 'stringify', descKey: 'Converts an object to a JSON string.' },
{ key: 'formatNumber', descKey: 'Formats a number using locale-specific formatting.' },
{ key: 'parseJson', descKey: 'Parses a JSON string into a JavaScript object.' },
];

const helpersTooltipContent = `
${helperDescriptionsHeader}

${helperDescriptions
.map(({ key, descKey }) => `- **${key}**: ${t(descKey)}`)
.join('\n')}
`;

return (
<div>
<ControlHeader>{props.label}</ControlHeader>
<ControlHeader>
<div>
{props.label}
<InfoTooltipWithTrigger
iconsStyle={{ marginLeft: theme.gridUnit }}
tooltip={<SafeMarkdown source={helpersTooltipContent} />}
/>
</div>
</ControlHeader>
<CodeEditor
theme="dark"
value={val}
Expand Down