Skip to content

Commit cee7797

Browse files
committed
Refactor Chartifact integration by exporting convertToChartifact and openChartifactViewer functions; remove ChartifactDialog component usage in ReportView
1 parent 94da1f0 commit cee7797

2 files changed

Lines changed: 68 additions & 16 deletions

File tree

src/views/ChartifactDialog.tsx

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ strong {
486486
};
487487

488488
// Function to convert report markdown to Chartifact format
489-
const convertToChartifact = async (reportMarkdown: string, reportStyle: string, charts: Chart[], tables: DictTable[], conceptShelfItems: FieldItem[], config: ClientConfig): Promise<string> => {
489+
export const convertToChartifact = (reportMarkdown: string, reportStyle: string, charts: Chart[], tables: DictTable[], conceptShelfItems: FieldItem[], config: ClientConfig) => {
490490
try {
491491
// Extract chart IDs from the report markdown images
492492
// Images are in format: [IMAGE(chart-id)]
@@ -590,3 +590,57 @@ ${JSON.stringify(modifiedSpec, null, 2)}
590590
}
591591
};
592592

593+
594+
// Function to open Chartifact in a new tab and send markdown via postMessage
595+
export const openChartifactViewer = async (chartifactMarkdown: string) => {
596+
try {
597+
// Open the Chartifact viewer in a new tab
598+
const chartifactWindow = window.open(
599+
'https://microsoft.github.io/chartifact/view/',
600+
'_blank'
601+
);
602+
603+
if (!chartifactWindow) {
604+
//showMessage('Failed to open Chartifact viewer. Please allow popups.', 'error');
605+
return;
606+
}
607+
608+
// Listen for hostStatus messages from the Chartifact viewer
609+
const handleMessage = (event: MessageEvent) => {
610+
// Verify the message is from the Chartifact viewer
611+
if (event.origin !== 'https://microsoft.github.io') {
612+
return;
613+
}
614+
615+
const message = event.data;
616+
617+
// Check if this is a hostStatus message
618+
if (message.type === 'hostStatus' && message.hostStatus === 'ready') {
619+
// Send the render request when the host is ready
620+
const renderRequest: {
621+
type: 'hostRenderRequest';
622+
title: string;
623+
markdown?: string;
624+
interactiveDocument?: any;
625+
} = {
626+
type: 'hostRenderRequest',
627+
title: 'Data Formulator Report',
628+
markdown: chartifactMarkdown
629+
};
630+
631+
chartifactWindow.postMessage(renderRequest, 'https://microsoft.github.io');
632+
633+
// Remove the event listener after sending
634+
window.removeEventListener('message', handleMessage);
635+
}
636+
};
637+
638+
// Add event listener for messages from the Chartifact viewer
639+
window.addEventListener('message', handleMessage);
640+
641+
//showMessage('Opened Chartifact viewer in a new tab', 'success');
642+
} catch (error) {
643+
console.error('Error opening Chartifact viewer:', error);
644+
//showMessage('Failed to prepare Chartifact report', 'error');
645+
}
646+
};

src/views/ReportView.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import TableRowsIcon from '@mui/icons-material/TableRows';
5050
import { Collapse } from '@mui/material';
5151
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
5252
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
53-
import { ChartifactDialog } from './ChartifactDialog';
53+
import { convertToChartifact, openChartifactViewer } from './ChartifactDialog';
5454

5555
// Typography constants
5656
const FONT_FAMILY_SYSTEM = '-apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, "Apple Color Emoji", Arial, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"';
@@ -233,7 +233,6 @@ export const ReportView: FC = () => {
233233
const [error, setError] = useState<string>('');
234234
const [style, setStyle] = useState<string>('short note');
235235
const [mode, setMode] = useState<'compose' | 'post'>(allGeneratedReports.length > 0 ? 'post' : 'compose');
236-
const [chartifactDialogOpen, setChartifactDialogOpen] = useState(false);
237236

238237
// Local state for current report
239238
const [currentReportId, setCurrentReportId] = useState<string | undefined>(undefined);
@@ -1118,7 +1117,18 @@ export const ReportView: FC = () => {
11181117
<Button
11191118
variant="contained"
11201119
size="small"
1121-
onClick={() => setChartifactDialogOpen(true)}
1120+
onClick={() => {
1121+
// Convert report to Chartifact markdown format
1122+
const chartifactMarkdown = convertToChartifact(
1123+
generatedReport,
1124+
generatedStyle,
1125+
charts,
1126+
tables,
1127+
conceptShelfItems,
1128+
config
1129+
);
1130+
openChartifactViewer(chartifactMarkdown);
1131+
}}
11221132
sx={{
11231133
textTransform: 'none',
11241134
backgroundColor: 'primary.main',
@@ -1249,18 +1259,6 @@ export const ReportView: FC = () => {
12491259
</Box>
12501260
</Box>
12511261
) : null}
1252-
1253-
{/* Chartifact Dialog */}
1254-
<ChartifactDialog
1255-
open={chartifactDialogOpen}
1256-
onClose={() => setChartifactDialogOpen(false)}
1257-
reportContent={generatedReport}
1258-
reportStyle={generatedStyle}
1259-
charts={charts}
1260-
tables={tables}
1261-
conceptShelfItems={conceptShelfItems}
1262-
config={config}
1263-
/>
12641262
</Box>
12651263
);
12661264
};

0 commit comments

Comments
 (0)