UNPKG

@expofp/floorplan

Version:

Interactive floor plan library for expos and events

2 lines (1 loc) • 958 B
import b from"debug";import{useCallback as k,useEffect as w,useState as d}from"react";import{getLogoUrl as x}from"../../utils/getLogoUrl.js";import{loadImagesInBatchesById as I}from"../../utils/loadImagesInBatches.js";const L=b("efp:header-logos");export function logoUrls(t){const r=x(t)===t.url?t.url:t.smallUrl??t.url;return r===t.url?{fallback:t.url}:{preferred:r,fallback:t.url}}export function useLogoPreload(t,r){const[f,i]=d(()=>new Map),[e,p]=d(0),[l,h]=d(()=>Math.min(r,t.length));w(()=>{if(e>=l)return;let n=!1;const u=t.slice(e,l),g=new Map(u.flatMap(o=>o.logo?[[o.id,logoUrls(o.logo)]]:[]));return I(g).then(o=>{if(!n){for(const a of u)o.has(a.id)||L("logo did not load",a.id,a.logo?.url);i(a=>{const c=new Map(a);for(const[M,s]of o)c.set(M,{src:s.src,aspectRatio:s.naturalWidth/s.naturalHeight});return c}),p(l)}}),()=>{n=!0}},[t,e,l]);const m=k(()=>{h(n=>n>e?n:Math.min(t.length,e+r))},[e,r,t.length]);return{loaded:f,attempted:e,loadMore:m}}