Skip to content

Commit 7b486dc

Browse files
authored
feat(party): preload images (#18)
* feat(party): prepare branch * preload next image in sequence
1 parent 3730542 commit 7b486dc

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

frontend/src/party/view/ViewParty.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import ViewPartyStartPage from './ViewPartyStartPage';
2020
import ViewPartyReveal from './ViewPartyReveal';
2121
import EmojiBar from '../../components/EmojiBar';
2222
import ReactionPicker from '../../components/ReactionPicker';
23+
import { usePreloadNextImage } from './util';
2324

2425
const ViewPartyContent = ({
2526
partyStatus,
@@ -45,6 +46,7 @@ const ViewPartyContent = ({
4546
},
4647
[mutateReactionAsync, partyId, session]
4748
);
49+
usePreloadNextImage(party, partyStatus);
4850

4951
if (!partyStatus || !party) {
5052
return <div>Unknown party or party status</div>;

frontend/src/party/view/util.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { PartyStatusResponse, PartySubmissionResponse, Vote } from '../../api/api-models';
1+
import { useEffect } from 'react';
2+
import { getImageUrl } from '../../api/api';
3+
import { PartyResponse, PartyStatusResponse, PartySubmissionResponse, Vote } from '../../api/api-models';
24

35
export const totalRating = (votes: Vote[]) => votes.reduce((x1, x2) => x1 + x2.rating, 0);
46

@@ -29,3 +31,22 @@ export const getSubmissionVotes = (
2931
) => {
3032
return partyStatus.votes.filter((vote) => vote.submissionId === submission.id && (!userId || vote.userId === userId));
3133
};
34+
35+
const preloadImage = (imageURL: string) => {
36+
new Image().src = imageURL;
37+
};
38+
39+
export const usePreloadNextImage = (party: PartyResponse, partyStatus: PartyStatusResponse) => {
40+
const currentIndex = partyStatus.current ? partyStatus.sequence[partyStatus.current.index] : -1;
41+
const nextIndex = Math.min(currentIndex + 1, party.submissions.length - 1);
42+
const nextIndexInSequence = partyStatus.sequence?.[nextIndex];
43+
const nextSubmissionImageId = party.submissions?.[nextIndexInSequence]?.imageId;
44+
45+
useEffect(() => {
46+
if (!nextSubmissionImageId) {
47+
return;
48+
}
49+
const nextImageURL = getImageUrl(nextSubmissionImageId);
50+
preloadImage(nextImageURL);
51+
}, [nextSubmissionImageId]);
52+
};

frontend/src/version.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ interface Change {
99
}
1010

1111
export const CHANGES: Change[] = [
12+
{
13+
name: '0.10.0',
14+
changes: [{ description: 'Preload images', type: 'feature' }],
15+
},
1216
{
1317
name: '0.9.1',
1418
changes: [{ description: 'Prevent Emoji Picker from loosing focus on state change', type: 'fix' }],

0 commit comments

Comments
 (0)