-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathuseImportAnalysisTableViewModel.ts
More file actions
138 lines (121 loc) · 4.39 KB
/
useImportAnalysisTableViewModel.ts
File metadata and controls
138 lines (121 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { ref, watch } from "vue";
import { useResolve } from "ts-injecty";
import { GetImportAnalysisUseCase } from "~/v1/domain/usecases/get-import-analysis-use-case";
import type { ImportAnalysisResponse, ImportStatus } from "~/v1/domain/entities/import/ImportAnalysis";
import { Workspace } from "~/v1/domain/entities/workspace/Workspace";
import { TableData } from "~/v1/domain/entities/table/TableData";
export const useImportAnalysisTableViewModel = (props: {
workspace: Workspace | null;
dataframeData: TableData | null;
pdfData: {
matchedFiles: any[];
} | null;
}) => {
const importAnalysisUseCase = useResolve(GetImportAnalysisUseCase);
// Reactive state
const isAnalyzing = ref(false);
const hasError = ref(false);
const errorMessage = ref("");
const analysisResult = ref<ImportAnalysisResponse | null>(null);
const documentActions = ref<Record<string, ImportStatus>>({});
const lastAnalysisKey = ref<string>(""); // Track last analysis to prevent duplicates
// Computed properties
const hasRequiredData = () => {
return (
props.workspace &&
props.dataframeData &&
props.dataframeData.data.length > 0 &&
props.pdfData?.matchedFiles?.length > 0
);
};
// Methods
const reset = () => {
isAnalyzing.value = false;
hasError.value = false;
errorMessage.value = "";
analysisResult.value = null;
documentActions.value = {};
lastAnalysisKey.value = "";
};
const analyzeImport = async (workspace: Workspace, dataframeData: TableData, matchedFiles: any[]) => {
if (!workspace || !dataframeData || dataframeData.data.length === 0) {
return;
}
// Create a unique key for this analysis to prevent duplicates
const analysisKey = `${workspace.id}-${dataframeData.data.length}-${matchedFiles.length}`;
// Skip if we've already analyzed this exact combination
if (lastAnalysisKey.value === analysisKey && analysisResult.value) {
return;
}
// Skip if already analyzing
if (isAnalyzing.value) {
return;
}
isAnalyzing.value = true;
hasError.value = false;
errorMessage.value = "";
try {
const result = await importAnalysisUseCase.analyzeImport(workspace.id, dataframeData, matchedFiles);
analysisResult.value = result;
lastAnalysisKey.value = analysisKey;
// Initialize document actions from analysis result
const actions: Record<string, ImportStatus> = {};
Object.entries(result.documents).forEach(([reference, docInfo]) => {
actions[reference] = docInfo.status;
});
documentActions.value = actions;
} catch (error: any) {
hasError.value = true;
errorMessage.value = error.message || "Failed to analyze import";
} finally {
isAnalyzing.value = false;
}
};
const retryAnalysis = () => {
if (props.workspace && props.dataframeData && props.pdfData?.matchedFiles) {
// Reset the last analysis key to force a retry
lastAnalysisKey.value = "";
analyzeImport(props.workspace, props.dataframeData, props.pdfData.matchedFiles);
}
};
// Auto-trigger analysis when props change - but only when we have all required data
watch(
() => ({
workspaceId: props.workspace?.id,
dataframeLength: props.dataframeData?.data?.length,
matchedFilesLength: props.pdfData?.matchedFiles?.length,
// Add deep watch on dataframe data to catch cell edits
dataframeDataHash: props.dataframeData ? JSON.stringify(props.dataframeData.data) : null,
}),
(newVal, oldVal) => {
// Only trigger if we have all required data and something actually changed
if (newVal.workspaceId && newVal.dataframeLength > 0 && newVal.matchedFilesLength > 0) {
// Check if this is a meaningful change
if (
!oldVal ||
newVal.workspaceId !== oldVal.workspaceId ||
newVal.dataframeLength !== oldVal.dataframeLength ||
newVal.matchedFilesLength !== oldVal.matchedFilesLength ||
newVal.dataframeDataHash !== oldVal.dataframeDataHash
) {
analyzeImport(props.workspace!, props.dataframeData!, props.pdfData!.matchedFiles);
}
}
},
{ immediate: true }
);
return {
// Reactive state
isAnalyzing,
hasError,
errorMessage,
analysisResult,
documentActions,
// Computed properties
hasRequiredData,
// Methods
reset,
analyzeImport,
retryAnalysis,
};
};