Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MS] Properly handle errors in spreadsheet viewer #9273

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,10 @@
"pagination": {
"page": "Page"
}
},
"spreadsheet": {
"loadSheetError": "This sheet could not be loaded.",
"loadDocumentError": "Could not load this document."
}
}
}
4 changes: 4 additions & 0 deletions client/src/locales/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -1756,6 +1756,10 @@
"pagination": {
"page": "Page"
}
},
"spreadsheet": {
"loadSheetError": "Impossible de charger cette feuille.",
"loadDocumentError": "Impossible de charger ce document."
}
}
}
38 changes: 29 additions & 9 deletions client/src/views/viewers/SpreadsheetViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
<template>
<file-viewer-wrapper>
<template #viewer>
<ms-report-text
class="spreadsheet-error"
:theme="MsReportTheme.Error"
v-if="error"
>
{{ $msTranslate(error) }}
</ms-report-text>
<ms-spinner v-show="loading" />
<div
v-show="!loading"
Expand All @@ -11,7 +18,7 @@
/>
</template>
<template #controls>
<file-controls>
<file-controls v-show="!loading && workbook">
<file-controls-button
v-for="(action, key) in actions"
:key="key"
Expand All @@ -24,7 +31,7 @@
</template>

<script setup lang="ts">
import { MsSpinner, I18n, Translatable } from 'megashark-lib';
import { MsSpinner, I18n, Translatable, MsReportText, MsReportTheme } from 'megashark-lib';
import { onBeforeMount, onMounted, Ref, ref } from 'vue';
import XLSX from 'xlsx';
import { FileControls, FileControlsButton } from '@/components/viewers';
Expand All @@ -40,16 +47,22 @@ const htmlContent = ref('');
const pages = ref<Array<string>>([]);
const currentPage = ref('');
const loading = ref(true);
const error = ref('');
let worker: Worker | null = null;
const actions: Ref<Array<{ icon?: string; text?: Translatable; handler: () => void }>> = ref([]);

onMounted(async () => {
workbook = XLSX.read(props.contentInfo.data, { type: 'array' });
pages.value = workbook.SheetNames;
await switchToPage(workbook.SheetNames[0]);
for (const page of pages.value) {
console.log(page);
actions.value.push({ text: I18n.valueAsTranslatable(page), handler: () => switchToPage(page) });
try {
workbook = XLSX.read(props.contentInfo.data, { type: 'array' });
pages.value = workbook.SheetNames;
await switchToPage(workbook.SheetNames[0]);
for (const page of pages.value) {
actions.value.push({ text: I18n.valueAsTranslatable(page), handler: () => switchToPage(page) });
}
} catch (e: any) {
window.electronAPI.log('error', `Failed to load spreadsheet document: ${e}`);
error.value = 'fileViewers.spreadsheet.loadDocumentError';
loading.value = false;
}
});

Expand All @@ -65,10 +78,13 @@ async function switchToPage(page: string): Promise<void> {
}
const ws = workbook.Sheets[page];
if (!ws) {
window.electronAPI.log('error', 'Spreadsheet page not found');
error.value = 'fileViewers.spreadsheet.loadSheetError';
currentPage.value = '';
htmlContent.value = '';
return;
}
loading.value = true;
error.value = '';

if (worker) {
worker.terminate();
Expand Down Expand Up @@ -103,4 +119,8 @@ async function switchToPage(page: string): Promise<void> {
}
}
}

.spreadsheet-error {
width: 100%;
}
</style>
Loading