Skip to content

Commit 6b215b5

Browse files
committed
feat: Refactor crosstab data handling in StepFlow and crosstabStore to improve data merging and UI updates, ensuring real-time display of generated table data
1 parent 1696231 commit 6b215b5

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

src/renderer/src/components/pages/crosstab/CrosstabChat.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,13 @@ export default function CrosstabChat({ chatId }: CrosstabChatProps) {
674674
.replace('[DIMENSION_NAME]', dimension.name)
675675
.replace('[DIMENSION_DESCRIPTION]', dimension.description || '')
676676

677-
const aiService = createAIService(llmConfig)
677+
const modelConfig = getModelConfigForLLM(llmConfig.id)
678+
if (!modelConfig) {
679+
message.error('请先在设置中配置模型参数')
680+
return
681+
}
682+
683+
const aiService = createAIService(llmConfig, modelConfig)
678684
const result = await new Promise<string>((resolve, reject) => {
679685
aiService.sendMessage(
680686
[{ id: 'temp', role: 'user', content: prompt, timestamp: Date.now() }],

src/renderer/src/components/pages/crosstab/StepFlow.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,10 @@ export default function StepFlow({ chat, userInput, onStepComplete, getLLMConfig
432432
let completedCells = 0
433433
const totalCells = horizontalCombinations.length * verticalCombinations.length
434434

435-
// 维护本地的tableData状态,避免异步状态更新问题
436-
const currentTableData = { ...chat.crosstabData.tableData }
435+
// 收集所有生成的数据,最后一次性更新
436+
const newTableData: { [key: string]: { [key: string]: string } } = {}
437437

438-
// 为每个交叉点生成值
438+
// 为每个交叉点生成值(串行执行)
439439
for (const hCombination of horizontalCombinations) {
440440
for (const vCombination of verticalCombinations) {
441441
const hPath = generateDimensionPath(hCombination)
@@ -529,11 +529,13 @@ export default function StepFlow({ chat, userInput, onStepComplete, getLLMConfig
529529
validatedCellValues[dim.id] = processedCellValues[dim.id] || ''
530530
})
531531

532-
// 更新本地tableData状态
533-
currentTableData[cellKey] = validatedCellValues
532+
// 将生成的数据添加到收集器中
533+
newTableData[cellKey] = validatedCellValues
534534

535-
// 立即更新当前单元格数据到UI
536-
stores.crosstab.updateCrosstabData(chat.id, { tableData: currentTableData })
535+
// 每生成1个单元格就更新一次UI,实时显示变化
536+
const currentTableData = stores.crosstab.getCrosstabData(chat.id)?.tableData || {}
537+
const updatedTableData = { ...currentTableData, ...newTableData }
538+
stores.crosstab.updateCrosstabData(chat.id, { tableData: updatedTableData })
537539

538540
completedCells++
539541
} catch (error) {
@@ -547,6 +549,11 @@ export default function StepFlow({ chat, userInput, onStepComplete, getLLMConfig
547549
}
548550
}
549551

552+
// 最终更新,确保所有数据都已保存
553+
const finalTableData = stores.crosstab.getCrosstabData(chat.id)?.tableData || {}
554+
const finalUpdatedTableData = { ...finalTableData, ...newTableData }
555+
stores.crosstab.updateCrosstabData(chat.id, { tableData: finalUpdatedTableData })
556+
550557
message.success(`表格数据生成完成 (${completedCells}/${totalCells})`)
551558
} catch (error) {
552559
console.error('表格数据生成失败:', error)

src/renderer/src/stores/crosstabStore.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,19 @@ export const useCrosstabStore = create<CrosstabState & CrosstabActions>()(
4848
const page = usePagesStore.getState().findPageById(chatId)
4949

5050
if (page && page.type === 'crosstab') {
51-
const updatedCrosstabData = { ...page.crosstabData, ...data }
51+
let updatedCrosstabData = { ...page.crosstabData }
52+
53+
// 特殊处理tableData的合并,避免对象不可扩展的问题
54+
if (data.tableData && page.crosstabData.tableData) {
55+
updatedCrosstabData = {
56+
...updatedCrosstabData,
57+
...data,
58+
tableData: { ...page.crosstabData.tableData, ...data.tableData }
59+
}
60+
} else {
61+
updatedCrosstabData = { ...updatedCrosstabData, ...data }
62+
}
63+
5264
updatePage(chatId, { crosstabData: updatedCrosstabData })
5365
}
5466
} catch (error) {

0 commit comments

Comments
 (0)