bugfix(gallery) Close signals properly, unwatch, gracefully fails and prevent undefined all_items

This commit is contained in:
Antonio Anaya 2026-05-10 19:14:03 -06:00 committed by Julian Stirling
parent 5f6b54247d
commit ee8e09a9e1

View file

@ -61,7 +61,7 @@
</div> </div>
</div> </div>
<PaginateLinks <PaginateLinks
:total-pages="totalPages" :total-pages="numberOfPages"
:current-page="currentPage" :current-page="currentPage"
@change-page="changePage" @change-page="changePage"
/> />
@ -109,8 +109,8 @@ export default {
// The actual property does not exist. So allowUndefined=true // The actual property does not exist. So allowUndefined=true
return this.thingPropertyUrl("smart_scan", "scans", true); return this.thingPropertyUrl("smart_scan", "scans", true);
}, },
noItmesy() { noItems() {
return this.all_items.length == 0; return !this.all_items || this.all_items?.length === 0;
}, },
selectedScanDZI() { selectedScanDZI() {
if (this.selectedScan && this.selectedScan.dzi != "") { if (this.selectedScan && this.selectedScan.dzi != "") {
@ -119,19 +119,20 @@ export default {
return null; return null;
} }
}, },
totalPages() { // name changed as paginateLinks has the prop totalPages
return Math.ceil(this.all_items.length / this.itemsPerPage); numberOfPages() {
return Math.ceil((this.all_items?.length || 0) / this.itemsPerPage);
}, },
paginatedItems() { paginatedItems() {
const start = (this.currentPage - 1) * this.itemsPerPage; const start = (this.currentPage - 1) * this.itemsPerPage;
return this.all_items.slice(start, start + this.itemsPerPage); return (this.all_items || []).slice(start, start + this.itemsPerPage);
}, },
}, },
async mounted() { async mounted() {
const store = useSettingsStore(); const store = useSettingsStore();
const { ready } = storeToRefs(store); const { ready } = storeToRefs(store);
watch(ready, (isReady) => { this.unwatchStoreFunction = watch(ready, (isReady) => {
if (isReady) { if (isReady) {
this.refreshGallery(); this.refreshGallery();
} else { } else {
@ -150,13 +151,9 @@ export default {
// Update on mount (does nothing if not connected) // Update on mount (does nothing if not connected)
await this.refreshGallery(); await this.refreshGallery();
// A global signal listener to perform a gallery refresh // A global signal listener to perform a gallery refresh
eventBus.on("globalRefreshGallery", () => { eventBus.on("globalRefreshGallery", this.refreshGallery);
this.refreshGallery(); // Handle the modal closed event here
}); eventBus.on("modalClosed", this.refreshGallery);
eventBus.on("modalClosed", () => {
// Handle the modal closed event here
this.refreshGallery();
});
}, },
beforeUnmount() { beforeUnmount() {
@ -179,8 +176,12 @@ export default {
async refreshGallery() { async refreshGallery() {
try { try {
let all_items = await this.readThingProperty("gallery", "list_data"); let all_items = await this.readThingProperty("gallery", "list_data");
if (!all_items | (all_items.length == 0)) { // if all_items is not or the number of items in all_items is zero then
this.all_items = all_items; // all_items is equal to empty list.
// stop refresh gallery as all_items is empty list.
if (!all_items || all_items?.length === 0) {
this.all_items = [];
return;
} }
all_items.forEach((item) => { all_items.forEach((item) => {
item.can_stitch = !item.stitch_available && item.number_of_images > 3; item.can_stitch = !item.stitch_available && item.number_of_images > 3;
@ -189,6 +190,7 @@ export default {
return b.created - a.created; return b.created - a.created;
}); });
this.all_items = all_items; this.all_items = all_items;
console.debug("Gallery size: %s", this.all_items.length);
} catch (err) { } catch (err) {
console.error("Failed to refresh gallery items."); console.error("Failed to refresh gallery items.");
console.error(err); console.error(err);