@ienlab/react-library 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`react`),t=require(`motion/react`),n=require(`react/jsx-runtime`),r=require(`firebase/firestore`);function i({placeholder:r,src:i,onLoadError:a,...o}){let[s,c]=(0,e.useState)(()=>{if(typeof window>`u`||!i)return!1;let e=new Image;return e.src=i,e.complete&&e.naturalWidth>0}),[l,u]=(0,e.useState)(i);return l!==i&&(u(i),c(!1)),(0,e.useEffect)(()=>{if(!i)return;let e=new Image;return e.src=i,e.onload=()=>c(!0),e.onerror=e=>{a?a(e):console.error(`CrossfadeImage: Failed to load image`,i)},e.complete&&e.naturalWidth>0&&e.onload(new Event(`load`)),()=>{e.onload=null,e.onerror=null}},[i,a]),(0,n.jsx)(t.AnimatePresence,{children:s&&i?(0,n.jsx)(t.motion.img,{src:i,...o,initial:{opacity:0},animate:{opacity:1},exit:{opacity:0},style:{...o.style}},`loaded-image`):(0,n.jsx)(t.motion.div,{initial:{opacity:1},transition:{duration:.3},style:{...o.style},children:r??(0,n.jsx)(`div`,{className:`w-full h-full bg-sidebar`})},`placeholder`)})}function a(e){return{...e.data(),id:e.id}}async function o(e,t,n,i,a){let o=a.filter(e=>!!(e&&!i.has(e.path)));for(let a=0;a<o.length;a+=30){let s=o.slice(a,a+30);(await(0,r.getDocs)((0,r.query)((0,r.collection)(e,t),(0,r.where)((0,r.documentId)(),`in`,s.map(e=>e.id))))).forEach(e=>{let t=n(e);i.set(e.ref.path,t)})}}async function s(e,t,n){let i=n.filter(e=>!!(e&&!t.has(e.path)));await Promise.all(i.map(async n=>{let i=e(await(0,r.getDoc)(n));t.set(n.path,i)}))}function c(e,t,n={}){let{cache:i=!0,onError:a}=n;return(0,r.onSnapshot)(e,{includeMetadataChanges:!i},e=>{(i||!e.metadata.fromCache)&&t(e)},a)}Array.prototype.mapNotNull=function(e){return this.flatMap((t,n)=>{let r=e(t,n);return r==null?[]:[r]})};var l=()=>({updateAt:(0,r.serverTimestamp)(),delete:!0}),u=()=>({updateAt:(0,r.serverTimestamp)(),delete:!1});exports.CrossfadeImage=i,exports.deleteItem=l,exports.fetchItems=o,exports.fetchItemsByOne=s,exports.getSnapshots=c,exports.snapshotToData=a,exports.undeleteItem=u;
@@ -1,98 +1,83 @@
1
- import { useEffect, useState } from "react";
2
- import { AnimatePresence, motion } from "motion/react";
3
- import { jsx } from "react/jsx-runtime";
4
- import { collection, documentId, getDoc, getDocs, onSnapshot, query, serverTimestamp, where } from "firebase/firestore";
1
+ import { useEffect as e, useState as t } from "react";
2
+ import { AnimatePresence as n, motion as r } from "motion/react";
3
+ import { jsx as i } from "react/jsx-runtime";
4
+ import { collection as a, documentId as o, getDoc as s, getDocs as c, onSnapshot as l, query as u, serverTimestamp as d, where as f } from "firebase/firestore";
5
5
  //#region src/components/image.tsx
6
- function CrossfadeImage({ placeholder, src, onLoadError, ...imgProps }) {
7
- const [isLoaded, setIsLoaded] = useState(() => {
8
- if (typeof window === "undefined" || !src) return false;
9
- const img = new Image();
10
- img.src = src;
11
- return img.complete && img.naturalWidth > 0;
12
- });
13
- const [prevSrc, setPrevSrc] = useState(src);
14
- if (prevSrc !== src) {
15
- setPrevSrc(src);
16
- setIsLoaded(false);
17
- }
18
- useEffect(() => {
19
- if (!src) return;
20
- const img = new Image();
21
- img.src = src;
22
- img.onload = () => setIsLoaded(true);
23
- img.onerror = (e) => {
24
- if (onLoadError) onLoadError(e);
25
- else console.error("CrossfadeImage: Failed to load image", src);
6
+ function p({ placeholder: a, src: o, onLoadError: s, ...c }) {
7
+ let [l, u] = t(() => {
8
+ if (typeof window > "u" || !o) return !1;
9
+ let e = new Image();
10
+ return e.src = o, e.complete && e.naturalWidth > 0;
11
+ }), [d, f] = t(o);
12
+ return d !== o && (f(o), u(!1)), e(() => {
13
+ if (!o) return;
14
+ let e = new Image();
15
+ return e.src = o, e.onload = () => u(!0), e.onerror = (e) => {
16
+ s ? s(e) : console.error("CrossfadeImage: Failed to load image", o);
17
+ }, e.complete && e.naturalWidth > 0 && e.onload(new Event("load")), () => {
18
+ e.onload = null, e.onerror = null;
26
19
  };
27
- if (img.complete && img.naturalWidth > 0) img.onload(new Event("load"));
28
- return () => {
29
- img.onload = null;
30
- img.onerror = null;
31
- };
32
- }, [src, onLoadError]);
33
- return /* @__PURE__ */ jsx(AnimatePresence, { children: isLoaded && src ? /* @__PURE__ */ jsx(motion.img, {
34
- src,
35
- ...imgProps,
20
+ }, [o, s]), /* @__PURE__ */ i(n, { children: l && o ? /* @__PURE__ */ i(r.img, {
21
+ src: o,
22
+ ...c,
36
23
  initial: { opacity: 0 },
37
24
  animate: { opacity: 1 },
38
25
  exit: { opacity: 0 },
39
- style: { ...imgProps.style }
40
- }, "loaded-image") : /* @__PURE__ */ jsx(motion.div, {
26
+ style: { ...c.style }
27
+ }, "loaded-image") : /* @__PURE__ */ i(r.div, {
41
28
  initial: { opacity: 1 },
42
29
  transition: { duration: .3 },
43
- style: { ...imgProps.style },
44
- children: placeholder ?? /* @__PURE__ */ jsx("div", { className: "w-full h-full bg-sidebar" })
30
+ style: { ...c.style },
31
+ children: a ?? /* @__PURE__ */ i("div", { className: "w-full h-full bg-sidebar" })
45
32
  }, "placeholder") });
46
33
  }
47
34
  //#endregion
48
35
  //#region src/utils/firestore.ts
49
- function snapshotToData(snapshot) {
36
+ function m(e) {
50
37
  return {
51
- ...snapshot.data(),
52
- id: snapshot.id
38
+ ...e.data(),
39
+ id: e.id
53
40
  };
54
41
  }
55
- async function fetchItems(firestore, colName, changeMethod, cache, referenceArray) {
56
- const missingRefs = referenceArray.filter((ref) => Boolean(ref && !cache.has(ref.path)));
57
- const chunkSize = 30;
58
- for (let i = 0; i < missingRefs.length; i += chunkSize) {
59
- const chunk = missingRefs.slice(i, i + chunkSize);
60
- (await getDocs(query(collection(firestore, colName), where(documentId(), "in", chunk.map((ref) => ref.id))))).forEach((snapshot) => {
61
- const item = changeMethod(snapshot);
62
- cache.set(snapshot.ref.path, item);
42
+ async function h(e, t, n, r, i) {
43
+ let s = i.filter((e) => !!(e && !r.has(e.path)));
44
+ for (let i = 0; i < s.length; i += 30) {
45
+ let l = s.slice(i, i + 30);
46
+ (await c(u(a(e, t), f(o(), "in", l.map((e) => e.id))))).forEach((e) => {
47
+ let t = n(e);
48
+ r.set(e.ref.path, t);
63
49
  });
64
50
  }
65
51
  }
66
- async function fetchItemsByOne(changeMethod, cache, referenceArray) {
67
- const missingRefs = referenceArray.filter((ref) => Boolean(ref && !cache.has(ref.path)));
68
- await Promise.all(missingRefs.map(async (ref) => {
69
- const item = changeMethod(await getDoc(ref));
70
- cache.set(ref.path, item);
52
+ async function g(e, t, n) {
53
+ let r = n.filter((e) => !!(e && !t.has(e.path)));
54
+ await Promise.all(r.map(async (n) => {
55
+ let r = e(await s(n));
56
+ t.set(n.path, r);
71
57
  }));
72
58
  }
73
- function getSnapshots(ref, callback, options = {}) {
74
- const { cache = true, onError } = options;
75
- return onSnapshot(ref, { includeMetadataChanges: !cache }, (snapshot) => {
76
- if (cache || !snapshot.metadata.fromCache) callback(snapshot);
77
- }, onError);
59
+ function _(e, t, n = {}) {
60
+ let { cache: r = !0, onError: i } = n;
61
+ return l(e, { includeMetadataChanges: !r }, (e) => {
62
+ (r || !e.metadata.fromCache) && t(e);
63
+ }, i);
78
64
  }
79
65
  //#endregion
80
66
  //#region src/utils/array.ts
81
- Array.prototype.mapNotNull = function(fn) {
82
- return this.flatMap((item, index) => {
83
- const result = fn(item, index);
84
- return result != null ? [result] : [];
67
+ Array.prototype.mapNotNull = function(e) {
68
+ return this.flatMap((t, n) => {
69
+ let r = e(t, n);
70
+ return r == null ? [] : [r];
85
71
  });
86
72
  };
87
73
  //#endregion
88
74
  //#region src/constant/firestore.ts
89
- var deleteItem = () => ({
90
- "updateAt": serverTimestamp(),
91
- "delete": true
92
- });
93
- var undeleteItem = () => ({
94
- "updateAt": serverTimestamp(),
95
- "delete": false
75
+ var v = () => ({
76
+ updateAt: d(),
77
+ delete: !0
78
+ }), y = () => ({
79
+ updateAt: d(),
80
+ delete: !1
96
81
  });
97
82
  //#endregion
98
- export { CrossfadeImage, deleteItem, fetchItems, fetchItemsByOne, getSnapshots, snapshotToData, undeleteItem };
83
+ export { p as CrossfadeImage, v as deleteItem, h as fetchItems, g as fetchItemsByOne, _ as getSnapshots, m as snapshotToData, y as undeleteItem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ienlab/react-library",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "My React component library",
5
5
  "main": "./dist/my-library.cjs.js",
6
6
  "module": "./dist/my-library.es.js",