Skip to content

Commit ee106c7

Browse files
committed
fix(image): fetch images only when list item renders
1 parent af8f122 commit ee106c7

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

src/components/common/list/ListItem.vue

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<script setup lang="ts">
22
import { NFlex, NImage, NSkeleton, NTime, NTimelineItem } from 'naive-ui';
33
4-
import { computed, type PropType, toRefs } from 'vue';
4+
import { computed, type PropType, toRefs, watch } from 'vue';
55
66
import type { ListScrollItem } from '~/components/common/list/ListScroll.model';
77
88
import PosterPlaceholder from '~/assets/images/poster-placholder.webp';
99
import ListItemPanel from '~/components/common/list/ListItemPanel.vue';
10+
import { useImageStore, useImageStoreRefs } from '~/stores/data/image.store';
1011
import { Colors } from '~/styles/colors.style';
12+
import { findClosestMatch } from '~/utils/math.utils';
1113
1214
const props = defineProps({
1315
item: {
@@ -76,6 +78,39 @@ const sameYear = computed(() => date.value?.getFullYear() === year);
7678
const loading = computed(() => item?.value?.loading);
7779
7880
const resolvedPoster = computed(() => item?.value?.poster?.value || poster.value);
81+
82+
const { getImageUrl } = useImageStore();
83+
const { imageSizes } = useImageStoreRefs();
84+
85+
const imageSize = computed(() =>
86+
imageSizes.value?.poster?.length
87+
? findClosestMatch(200, imageSizes.value.poster)
88+
: 'original',
89+
);
90+
91+
watch(
92+
item,
93+
_item => {
94+
if (!_item?.poster && _item?.type && _item?.movie?.ids?.tmdb) {
95+
_item.poster = getImageUrl(
96+
{
97+
id: _item.movie.ids.tmdb,
98+
type: _item.type,
99+
},
100+
imageSize.value,
101+
);
102+
} else if (!_item?.poster && _item?.type && _item?.show?.ids?.tmdb) {
103+
_item.poster = getImageUrl(
104+
{
105+
id: _item.show.ids.tmdb,
106+
type: 'show',
107+
},
108+
imageSize.value,
109+
);
110+
}
111+
},
112+
{ immediate: true },
113+
);
79114
</script>
80115

81116
<template>

src/components/views/history/HistoryComponent.vue

-29
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import type {
1111
import FloatingButton from '~/components/common/buttons/FloatingButton.vue';
1212
import ListScroll from '~/components/common/list/ListScroll.vue';
1313
import { useHistoryStore, useHistoryStoreRefs } from '~/stores/data/history.store';
14-
import { useImageStore, useImageStoreRefs } from '~/stores/data/image.store';
1514
import { useUserSettingsStoreRefs } from '~/stores/settings/user.store';
1615
import { useI18n } from '~/utils';
17-
import { findClosestMatch } from '~/utils/math.utils';
1816
1917
const { filteredHistory, pagination, loading, pageSize, belowThreshold } =
2018
useHistoryStoreRefs();
@@ -42,15 +40,6 @@ onMounted(() => {
4240
});
4341
});
4442
45-
const { getImageUrl } = useImageStore();
46-
const { imageSizes } = useImageStoreRefs();
47-
48-
const size = computed(() =>
49-
imageSizes.value?.poster?.length
50-
? findClosestMatch(200, imageSizes.value.poster)
51-
: 'original',
52-
);
53-
5443
const history = computed<ListScrollItem[]>(() => {
5544
const array = filteredHistory.value;
5645
if (!array.length) return [];
@@ -62,24 +51,6 @@ const history = computed<ListScrollItem[]>(() => {
6251
else if ('season' in _item) _item.type = 'season';
6352
else if ('show' in _item) _item.type = 'show';
6453
65-
if (!_item?.poster && _item.type && _item?.movie?.ids?.tmdb) {
66-
_item.poster = getImageUrl(
67-
{
68-
id: _item.movie.ids.tmdb,
69-
type: _item.type,
70-
},
71-
size.value,
72-
);
73-
} else if (!_item?.poster && _item.type && _item?.show?.ids?.tmdb) {
74-
_item.poster = getImageUrl(
75-
{
76-
id: _item.show.ids.tmdb,
77-
type: 'show',
78-
},
79-
size.value,
80-
);
81-
}
82-
8354
if (!item.watched_at) return _item;
8455
8556
const date: ListScrollItem['date'] = { current: new Date(item.watched_at) };

0 commit comments

Comments
 (0)