Skip to content

Commit

Permalink
feat: Improve homepage perfs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxou44 committed Nov 11, 2024
1 parent f19894f commit 2ddb3c2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
14 changes: 0 additions & 14 deletions src/main/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,8 @@ console.log(`💾 Eagle Animation files will be saved in the following folder: $

const getPictureLink = (projectId, sceneIndex, filename) => `ea-data://${projectId}/${sceneIndex}/${filename}`;

const getDefaultPreview = (data) => {
for (let i = 0; i < (data?.project?.scenes?.length || 0); i++) {
for (const picture of data?.project?.scenes?.[i]?.pictures || []) {
if (!picture?.deleted) {
return getPictureLink(data._id, i, picture.filename);
}
}
}
return null;
};

const computeProject = (data) => {
const copiedData = structuredClone(data);

let preview = getDefaultPreview(copiedData);
const scenes = copiedData.project.scenes.map((scene, i) => ({
...scene,
pictures: scene.pictures.map((picture) => ({
Expand All @@ -45,7 +32,6 @@ const computeProject = (data) => {
let output = {
...copiedData,
id: copiedData._id,
preview,
project: {
...copiedData.project,
scenes,
Expand Down
13 changes: 0 additions & 13 deletions src/renderer/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,8 @@ export const sendEvent = (name, data) => {
}
};

const getDefaultPreview = async (data) => {
for (let i = 0; i < (data?.project?.scenes?.length || 0); i++) {
for (const picture of data?.project?.scenes?.[i]?.pictures || []) {
if (!picture.deleted) {
return getFrameBlobUrl(picture.filename?.split('.')?.[0]);
}
}
}
return null;
};

const computeProject = async (data, bindPictureLink = true) => {
const copiedData = structuredClone(data);
let preview = await getDefaultPreview(copiedData);
const scenes = await Promise.all(
copiedData?.project?.scenes?.map(async (scene) => {
return {
Expand All @@ -58,7 +46,6 @@ const computeProject = async (data, bindPictureLink = true) => {

let output = {
id: copiedData.id,
preview,
project: {
...copiedData?.project,
scenes,
Expand Down
40 changes: 39 additions & 1 deletion src/renderer/hooks/useProjects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { useCallback, useEffect, useState } from 'react';
import { OptimizeFrame } from '../core/Optimizer';

const getDefaultFrame = (data) => {
for (let i = 0; i < (data?.project?.scenes?.length || 0); i++) {
for (const picture of data?.project?.scenes?.[i]?.pictures || []) {
if (!picture?.deleted && !picture?.hidden) {
return { projectId: data?.id, sceneId: i, picture };
}
}
}
for (let i = 0; i < (data?.project?.scenes?.length || 0); i++) {
for (const picture of data?.project?.scenes?.[i]?.pictures || []) {
if (!picture?.deleted) {
return { projectId: data?.id, sceneId: i, picture };
}
}
}
return null;
};

// Use loop for performance issues
const countFrames = (project) => {
Expand All @@ -15,6 +34,7 @@ const countFrames = (project) => {

function useProjects(options) {
const [projectsData, setProjectsData] = useState(null);
const [previewUrls, setPreviewUrls] = useState([]);

// Initial load
useEffect(() => {
Expand All @@ -32,6 +52,24 @@ function useProjects(options) {
});
});

useEffect(() => {
if (!projectsData) {
return;
}
Promise.all(
projectsData.map(async (project) => {
const defaultFrame = await getDefaultFrame(project);
if (!defaultFrame?.picture) {
return null;
}
const optimizedFrame = await OptimizeFrame(defaultFrame.projectId, defaultFrame.sceneId, defaultFrame.picture.id, 'thumbnail', defaultFrame.picture.link);
return optimizedFrame;
})
).then((links) => {
setPreviewUrls(links);
});
}, [projectsData]);

// Action rename
const actionRename = useCallback(async (projectId, title = '') => {
setProjectsData((oldData) => {
Expand All @@ -57,7 +95,7 @@ function useProjects(options) {
});

return {
projects: projectsData?.map((e) => ({ ...(e || {}), stats: { frames: countFrames(e.project) } })) || null,
projects: projectsData?.map((e, i) => ({ ...(e || {}), stats: { frames: countFrames(e.project) }, preview: previewUrls[i] || null })) || null,
actions: {
refresh: actionRefresh,
create: actionCreate,
Expand Down

0 comments on commit 2ddb3c2

Please sign in to comment.