Skip to content

Commit 6eb33b7

Browse files
committed
fix load crash
1 parent 186e62c commit 6eb33b7

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

internal/handlers/documents.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ func ListDocuments(dataStore *store.Store) http.HandlerFunc {
194194
platform.WriteError(w, r, http.StatusInternalServerError, "internal_error", "获取文档列表失败")
195195
return
196196
}
197+
if items == nil {
198+
items = []store.Document{}
199+
}
197200

198201
platform.WriteSuccess(w, r, http.StatusOK, map[string]any{
199202
"items": items,

internal/handlers/units.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func ListTranslationUnits(dataStore *store.Store) http.HandlerFunc {
4646
platform.WriteError(w, r, http.StatusInternalServerError, "internal_error", "获取翻译条目列表失败")
4747
return
4848
}
49+
if items == nil {
50+
items = []store.TranslationUnit{}
51+
}
4952

5053
platform.WriteSuccess(w, r, http.StatusOK, map[string]any{
5154
"items": items,
@@ -202,6 +205,9 @@ func ListTranslationUnitHistory(dataStore *store.Store) http.HandlerFunc {
202205
platform.WriteError(w, r, http.StatusInternalServerError, "internal_error", "获取翻译历史失败")
203206
return
204207
}
208+
if items == nil {
209+
items = []store.TranslationUnitHistoryEntry{}
210+
}
205211

206212
platform.WriteSuccess(w, r, http.StatusOK, map[string]any{
207213
"items": items,

internal/store/store.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ func (s *Store) GetProject(ctx context.Context, projectID string) (ProjectOvervi
633633
return ProjectOverview{}, err
634634
}
635635
}
636+
for _, status := range []string{"untranslated", "translated", "reviewed", "approved"} {
637+
if _, ok := item.StatusCounts[status]; !ok {
638+
item.StatusCounts[status] = 0
639+
}
640+
}
636641

637642
return item, nil
638643
}

web/src/features/translation/components/TranslationWorkbench.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,16 @@ export default function TranslationWorkbench({ projectId, documentId: propDocume
7979
queryFn: () => api.get<{ items: DocumentItem[] }>(`/projects/${projectId}/documents`),
8080
});
8181

82-
const selectedDocument = docsData?.items.find((doc) => doc.id === selectedDocId) ?? null;
82+
const documents = Array.isArray(docsData?.items) ? docsData.items : [];
83+
const selectedDocument = documents.find((doc) => doc.id === selectedDocId) ?? null;
8384

8485
const { data: unitsData, isLoading: isLoadingUnits } = useQuery({
8586
queryKey: ['workbench-units', projectId, selectedDocId],
8687
enabled: Boolean(selectedDocId),
8788
queryFn: () => api.get<UnitsResponse>(`/projects/${projectId}/units?documentId=${selectedDocId}&page=1&pageSize=1000`),
8889
});
8990

90-
const units = unitsData?.items || [];
91+
const units = Array.isArray(unitsData?.items) ? unitsData.items : [];
9192

9293
useEffect(() => {
9394
if (!selectedDocId) {

web/src/routes/project.$projectId.dashboard.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ function Dashboard() {
2323
);
2424
}
2525

26-
const totalUnits = project?.translationUnitCount || 0;
27-
const statusCounts = project?.statusCounts || { untranslated: 0, translated: 0, reviewed: 0, approved: 0 };
26+
const totalUnits = Number(project?.translationUnitCount ?? 0);
27+
const statusCounts = {
28+
untranslated: Number(project?.statusCounts?.untranslated ?? 0),
29+
translated: Number(project?.statusCounts?.translated ?? 0),
30+
reviewed: Number(project?.statusCounts?.reviewed ?? 0),
31+
approved: Number(project?.statusCounts?.approved ?? 0),
32+
};
2833

2934
const translatedPct = totalUnits > 0 ? (statusCounts.translated / totalUnits) * 100 : 0;
3035
const reviewedPct = totalUnits > 0 ? (statusCounts.reviewed / totalUnits) * 100 : 0;
@@ -131,16 +136,16 @@ function Dashboard() {
131136
}
132137

133138
function StatCard({ icon, label, value, bg, isString = false }: { icon: React.ReactNode, label: string, value: string | number, bg: string, isString?: boolean }) {
139+
const displayValue = isString ? value : Number(value ?? 0).toLocaleString();
140+
134141
return (
135142
<div className="bg-white rounded-3xl p-6 border border-slate-200 shadow-sm flex items-center gap-5">
136143
<div className={`w-14 h-14 rounded-2xl ${bg} flex items-center justify-center shrink-0`}>
137144
{icon}
138145
</div>
139146
<div>
140147
<div className="text-sm font-semibold text-slate-500 mb-1">{label}</div>
141-
<div className="text-2xl font-bold text-slate-900">
142-
{isString ? value : (value as number).toLocaleString()}
143-
</div>
148+
<div className="text-2xl font-bold text-slate-900">{displayValue}</div>
144149
</div>
145150
</div>
146151
);
@@ -153,7 +158,7 @@ function ProgressStat({ dot, label, count }: { dot: string, label: string, count
153158
<div className={`w-3 h-3 rounded-full ${dot} mr-2`}></div>
154159
{label}
155160
</div>
156-
<div className="text-2xl font-semibold text-slate-900">{count.toLocaleString()}</div>
161+
<div className="text-2xl font-semibold text-slate-900">{Number(count ?? 0).toLocaleString()}</div>
157162
</div>
158163
);
159-
}
164+
}

0 commit comments

Comments
 (0)