Merge commit '05b4f4dc5' into HEAD

This commit is contained in:
Thomas Camlong committed 2026-08-10 19:17:26 +02:00
commit 3c8fe54618
1 file changed
+28 -7
@@ -1,14 +1,21 @@
"use client"; "use client";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import dynamic from "next/dynamic";
import { useHotkeys } from "@mantine/hooks"; import { useHotkeys } from "@mantine/hooks";
import { hotkeys } from "@homarr/definitions"; import { hotkeys } from "@homarr/definitions";
import { Spotlight } from "@homarr/spotlight/component";
import { hasPendingSpotlightOpen, openSpotlight, spotlightOpenEvent } from "@homarr/spotlight/open"; import { hasPendingSpotlightOpen, openSpotlight, spotlightOpenEvent } from "@homarr/spotlight/open";
const loadSpotlight = () => import("@homarr/spotlight/component");
const Spotlight = dynamic(() => loadSpotlight().then(({ Spotlight: Component }) => Component), {
ssr: false,
loading: () => null,
});
export const LazySpotlight = () => { export const LazySpotlight = () => {
const [isMounted, setIsMounted] = useState(false); const [isMounted, setIsMounted] = useState(false);
const [isPreloaded, setIsPreloaded] = useState(false);
useHotkeys( useHotkeys(
isMounted isMounted
@@ -25,27 +32,41 @@ export const LazySpotlight = () => {
); );
useEffect(() => { useEffect(() => {
let isActive = true;
const mount = () => setIsMounted(true); const mount = () => setIsMounted(true);
window.addEventListener(spotlightOpenEvent, mount); window.addEventListener(spotlightOpenEvent, mount);
if (hasPendingSpotlightOpen()) mount(); if (hasPendingSpotlightOpen()) mount();
const preload = () => {
void loadSpotlight().then(
() => {
if (isActive) setIsPreloaded(true);
},
() => undefined,
);
};
let idleId: number | undefined; let idleId: number | undefined;
const delayId = window.setTimeout(() => { const delayId = window.setTimeout(() => {
idleId = window.requestIdleCallback?.(mount, { timeout: 1_000 }); idleId = window.requestIdleCallback?.(preload, { timeout: 1_000 });
if (idleId === undefined) mount(); if (idleId === undefined) preload();
}, 1_000); }, 1_000);
return () => { return () => {
isActive = false;
window.removeEventListener(spotlightOpenEvent, mount); window.removeEventListener(spotlightOpenEvent, mount);
window.clearTimeout(delayId); window.clearTimeout(delayId);
if (idleId !== undefined) window.cancelIdleCallback?.(idleId); if (idleId !== undefined) window.cancelIdleCallback?.(idleId);
}; };
}, []); }, []);
return isMounted ? ( return isMounted || isPreloaded ? (
<> <>
<span hidden data-homarr-dev-benchmark-spotlight-preloaded /> {isPreloaded && <span hidden data-homarr-dev-benchmark-spotlight-preloaded />}
<span hidden data-homarr-dev-benchmark-spotlight-mounted /> {isMounted && (
<Spotlight /> <>
<span hidden data-homarr-dev-benchmark-spotlight-mounted />
<Spotlight />
</>
)}
</> </>
) : null; ) : null;
}; };