perf: lazy load home image editor
This commit is contained in:
parent
f7dba9cb41
commit
aabd492300
@ -1,7 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useMemo, useRef, useState } from "react";
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
import dynamic from "next/dynamic";
|
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { signIn, signOut } from "next-auth/react";
|
import { signIn, signOut } from "next-auth/react";
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "next-themes";
|
||||||
@ -74,16 +73,7 @@ import {
|
|||||||
type LovartHomeSidebarItemKey,
|
type LovartHomeSidebarItemKey,
|
||||||
} from "@/lib/home/lovart-sidebar-config";
|
} from "@/lib/home/lovart-sidebar-config";
|
||||||
|
|
||||||
const ImageAnnotator = dynamic(
|
type ImageAnnotatorComponent = typeof import("@/components/image-annotator").ImageAnnotator;
|
||||||
() =>
|
|
||||||
import("@/components/image-annotator").then(
|
|
||||||
(module) => module.ImageAnnotator
|
|
||||||
),
|
|
||||||
{
|
|
||||||
loading: () => null,
|
|
||||||
ssr: false,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
type HomeUser = {
|
type HomeUser = {
|
||||||
id: string;
|
id: string;
|
||||||
@ -481,6 +471,8 @@ export function LovartHome({
|
|||||||
const [showDingTalkOrgMenu, setShowDingTalkOrgMenu] = useState(false);
|
const [showDingTalkOrgMenu, setShowDingTalkOrgMenu] = useState(false);
|
||||||
const [showImageEditor, setShowImageEditor] = useState(false);
|
const [showImageEditor, setShowImageEditor] = useState(false);
|
||||||
const [editorImageUrl, setEditorImageUrl] = useState("");
|
const [editorImageUrl, setEditorImageUrl] = useState("");
|
||||||
|
const [ImageAnnotatorLazy, setImageAnnotatorLazy] =
|
||||||
|
useState<ImageAnnotatorComponent | null>(null);
|
||||||
const [tutorialDialogOpen, setTutorialDialogOpen] = useState(false);
|
const [tutorialDialogOpen, setTutorialDialogOpen] = useState(false);
|
||||||
const [currentTutorialVideo, setCurrentTutorialVideo] = useState<TutorialVideo | null>(null);
|
const [currentTutorialVideo, setCurrentTutorialVideo] = useState<TutorialVideo | null>(null);
|
||||||
const [contactDialogOpen, setContactDialogOpen] = useState(false);
|
const [contactDialogOpen, setContactDialogOpen] = useState(false);
|
||||||
@ -945,6 +937,23 @@ export function LovartHome({
|
|||||||
filesRef.current = files;
|
filesRef.current = files;
|
||||||
}, [files]);
|
}, [files]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!showImageEditor || ImageAnnotatorLazy) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let disposed = false;
|
||||||
|
void import("@/components/image-annotator").then((module) => {
|
||||||
|
if (!disposed) {
|
||||||
|
setImageAnnotatorLazy(() => module.ImageAnnotator);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
disposed = true;
|
||||||
|
};
|
||||||
|
}, [ImageAnnotatorLazy, showImageEditor]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return () => {
|
return () => {
|
||||||
filesRef.current.forEach((item) => URL.revokeObjectURL(item.previewUrl));
|
filesRef.current.forEach((item) => URL.revokeObjectURL(item.previewUrl));
|
||||||
@ -2374,14 +2383,19 @@ export function LovartHome({
|
|||||||
<ArrowUp className="h-4 w-4" />
|
<ArrowUp className="h-4 w-4" />
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
{showImageEditor ? (
|
{showImageEditor && ImageAnnotatorLazy ? (
|
||||||
<ImageAnnotator
|
<ImageAnnotatorLazy
|
||||||
imageUrl={editorImageUrl}
|
imageUrl={editorImageUrl}
|
||||||
onConfirm={handleImageEditorConfirm}
|
onConfirm={handleImageEditorConfirm}
|
||||||
submitMode="publish-new-canvas"
|
submitMode="publish-new-canvas"
|
||||||
onCancel={handleImageEditorCancel}
|
onCancel={handleImageEditorCancel}
|
||||||
onHomeClick={handleImageEditorCancel}
|
onHomeClick={handleImageEditorCancel}
|
||||||
/>
|
/>
|
||||||
|
) : showImageEditor ? (
|
||||||
|
<div className="fixed inset-0 z-[90] flex items-center justify-center bg-black/40 text-white backdrop-blur-sm">
|
||||||
|
<Loader2 className="mr-2 h-5 w-5 animate-spin" />
|
||||||
|
正在加载图片编辑器...
|
||||||
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
<Dialog
|
<Dialog
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user