fix(chart): show correct chart type in previews

- Use NoteChart directly with props instead of NoteChartFromCode
- Remove markdown ticks from chartSuggestionToMarkdown output
- Export NoteChart component for direct use in previews

Now the 3 suggestions correctly show different chart types.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Antigravity
2026-05-23 10:00:44 +00:00
parent 835e1872bb
commit d1395d9b81
4 changed files with 17 additions and 16 deletions

View File

@@ -2,7 +2,7 @@
import { useState, useEffect } from 'react'
import { createPortal } from 'react-dom'
import { NoteChartFromCode } from './note-chart'
import { NoteChart } from './note-chart'
import { suggestCharts, chartSuggestionToMarkdown, type ChartSuggestion, type SuggestChartsResponse } from '@/lib/ai/services/chart-suggestion.service'
import { BarChart3, X, Search, AlertCircle, Sparkles } from 'lucide-react'
import { cn } from '@/lib/utils'
@@ -240,7 +240,13 @@ export function ChartSuggestionsDialog({
{/* Mini thumbnail */}
<div className="aspect-video bg-card rounded-lg mb-3 overflow-hidden flex items-center justify-center p-2">
<div className="w-full scale-75 origin-center">
<NoteChartFromCode code={markdown} />
<NoteChart
type={suggestion.type}
title={suggestion.title}
data={suggestion.data}
height={100}
showLegend={false}
/>
</div>
</div>

View File

@@ -49,7 +49,7 @@ function useDarkMode() {
return isDark
}
function NoteChart({ type, title, data, colors, showLegend, height = 250 }: NoteChartProps) {
export function NoteChart({ type, title, data, colors, showLegend, height = 250 }: NoteChartProps) {
const chartColors = colors ?? CHART_COLORS
const dark = useDarkMode()

View File

@@ -291,18 +291,12 @@ export const RichTextEditor = forwardRef<RichTextEditorHandle, RichTextEditorPro
setChartSuggestionsOpen(true)
}, [editor])
const handleSelectChart = useCallback((markdown: string) => {
const handleSelectChart = useCallback((chartContent: string) => {
if (!editor || !editor.isEditable) return
try {
// Convert markdown chart to HTML format for TipTap
// Input: ```chart\nbar\nTitle\nlabel: value\n```
// Output: <pre><code class="language-chart">bar\nTitle\nlabel: value</code></pre>
const chartContent = markdown
.replace(/```chart\n?/gi, '')
.replace(/```$/gi, '')
.trim()
// chartContent is now raw content without markdown ticks
// Example: "bar\nTitle\nlabel: value"
const htmlContent = `<pre><code class="language-chart">${chartContent}</code></pre>`
// Insert the chart HTML at current position
@@ -321,7 +315,7 @@ export const RichTextEditor = forwardRef<RichTextEditorHandle, RichTextEditorPro
}
}
// Insert the chart as HTML (not markdown)
// Insert the chart as HTML
editor.chain().focus().insertContent(htmlContent).run()
} catch (error) {
console.error('[handleSelectChart] Failed to insert chart:', error)

View File

@@ -71,9 +71,10 @@ export async function suggestCharts(request: SuggestChartsRequest): Promise<Sugg
}
/**
* Convert a chart suggestion to markdown code block format
* Convert a chart suggestion to raw chart format (no markdown ticks)
* This is the content that goes inside <code class="language-chart">
* @param suggestion - The chart suggestion to convert
* @returns Markdown code block string
* @returns Raw chart content string
*/
export function chartSuggestionToMarkdown(suggestion: ChartSuggestion): string {
const lines = [
@@ -81,5 +82,5 @@ export function chartSuggestionToMarkdown(suggestion: ChartSuggestion): string {
suggestion.title,
...suggestion.data.map(d => `${d.label}: ${d.value}`),
]
return `\`\`\`chart\n${lines.join('\n')}\n\`\`\``
return lines.join('\n')
}