Skip to content
Open
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
25 changes: 24 additions & 1 deletion library/src/containers/Messages/MessageExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ interface Props {
message: MessageInterface;
}

interface SchemaWithFormat {
schemaFormat: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: any;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function isMultiFormatSchema(schema: any): schema is SchemaWithFormat {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return (
schema &&
typeof schema === 'object' &&
'schemaFormat' in schema &&
'schema' in schema
);
}

export const MessageExample: React.FunctionComponent<Props> = ({ message }) => {
if (!message) {
return null;
Expand Down Expand Up @@ -54,9 +71,15 @@ export const Example: React.FunctionComponent<ExampleProps> = ({
const [expanded, setExpanded] = useState(
config?.expand?.messageExamples ?? false,
);

useEffect(() => {
setExpanded(config?.expand?.messageExamples ?? false);
// Detect multi-format schema and restructure the schema property
// to ensure compatibility with the sample generator
const schemaData = schema.json();
if (isMultiFormatSchema(schemaData)) {
Object.assign(schemaData, schemaData.schema);
delete schemaData.schema;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [config.expand]);

Expand Down