@maas/payload-plugin-media-cloud 0.0.32 → 0.0.34

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.
@@ -22,7 +22,7 @@ const afterChangeHook = async ({ collection, doc, previousDoc, req }) => {
22
22
  const thumbnail = buildThumbnailURL({
23
23
  storage: doc.storage,
24
24
  playbackId: doc.storage === "mux" ? doc.id : void 0,
25
- s3Key: doc.storage === "s3" ? doc.path : void 0,
25
+ s3Key: doc.storage === "s3" ? doc.path ?? doc.filename : void 0,
26
26
  s3Store
27
27
  });
28
28
  req.payload.update({
@@ -1 +1 @@
1
- {"version":3,"file":"afterChange.mjs","names":["afterChangeHook: CollectionAfterChangeHook"],"sources":["../../src/collectionHooks/afterChange.ts"],"sourcesContent":["import { buildThumbnailURL } from '../utils/buildThumbnailURL'\nimport { s3Store } from '../plugin'\nimport { MediaCloudErrors } from '../types/errors'\nimport { useErrorHandler } from '../hooks/useErrorHandler'\n\nimport type { CollectionAfterChangeHook } from 'payload'\n\nexport const afterChangeHook: CollectionAfterChangeHook = async ({\n collection,\n doc,\n previousDoc,\n req,\n}) => {\n const { throwError } = useErrorHandler()\n\n // Move asset in S3 if path has changed\n if (doc.path !== previousDoc?.path) {\n if (doc.storage === 's3' && s3Store) {\n try {\n const oldKey = previousDoc?.path ?? previousDoc?.filename\n const newKey = doc.path\n\n await s3Store.copy(oldKey, newKey)\n } catch (error) {\n throwError({ ...MediaCloudErrors.S3_MOVE_ERROR, cause: error })\n }\n }\n }\n\n // Handle thumbnail\n if (!doc.thumbnail || previousDoc?.path !== doc.path) {\n const thumbnail = buildThumbnailURL({\n storage: doc.storage,\n playbackId: doc.storage === 'mux' ? doc.id : undefined,\n s3Key: doc.storage === 's3' ? doc.path : undefined,\n s3Store,\n })\n\n req.payload.update({\n collection: collection.slug,\n id: doc.id,\n data: { thumbnail },\n })\n }\n\n return doc\n}\n"],"mappings":";;;;;;AAOA,MAAaA,kBAA6C,OAAO,EAC/D,YACA,KACA,aACA,UACI;CACJ,MAAM,EAAE,eAAe,iBAAiB;AAGxC,KAAI,IAAI,SAAS,aAAa,MAC5B;MAAI,IAAI,YAAY,QAAQ,QAC1B,KAAI;GACF,MAAM,SAAS,aAAa,QAAQ,aAAa;GACjD,MAAM,SAAS,IAAI;AAEnB,SAAM,QAAQ,KAAK,QAAQ,OAAO;WAC3B,OAAO;AACd,cAAW;IAAE,GAAG,iBAAiB;IAAe,OAAO;IAAO,CAAC;;;AAMrE,KAAI,CAAC,IAAI,aAAa,aAAa,SAAS,IAAI,MAAM;EACpD,MAAM,YAAY,kBAAkB;GAClC,SAAS,IAAI;GACb,YAAY,IAAI,YAAY,QAAQ,IAAI,KAAK;GAC7C,OAAO,IAAI,YAAY,OAAO,IAAI,OAAO;GACzC;GACD,CAAC;AAEF,MAAI,QAAQ,OAAO;GACjB,YAAY,WAAW;GACvB,IAAI,IAAI;GACR,MAAM,EAAE,WAAW;GACpB,CAAC;;AAGJ,QAAO"}
1
+ {"version":3,"file":"afterChange.mjs","names":["afterChangeHook: CollectionAfterChangeHook"],"sources":["../../src/collectionHooks/afterChange.ts"],"sourcesContent":["import { buildThumbnailURL } from '../utils/buildThumbnailURL'\nimport { s3Store } from '../plugin'\nimport { MediaCloudErrors } from '../types/errors'\nimport { useErrorHandler } from '../hooks/useErrorHandler'\n\nimport type { CollectionAfterChangeHook } from 'payload'\n\nexport const afterChangeHook: CollectionAfterChangeHook = async ({\n collection,\n doc,\n previousDoc,\n req,\n}) => {\n const { throwError } = useErrorHandler()\n\n // Move asset in S3 if path has changed\n if (doc.path !== previousDoc?.path) {\n if (doc.storage === 's3' && s3Store) {\n try {\n const oldKey = previousDoc?.path ?? previousDoc?.filename\n const newKey = doc.path\n\n await s3Store.copy(oldKey, newKey)\n } catch (error) {\n throwError({ ...MediaCloudErrors.S3_MOVE_ERROR, cause: error })\n }\n }\n }\n\n // Handle thumbnail\n if (!doc.thumbnail || previousDoc?.path !== doc.path) {\n const thumbnail = buildThumbnailURL({\n storage: doc.storage,\n playbackId: doc.storage === 'mux' ? doc.id : undefined,\n s3Key: doc.storage === 's3' ? (doc.path ?? doc.filename) : undefined,\n s3Store,\n })\n\n req.payload.update({\n collection: collection.slug,\n id: doc.id,\n data: { thumbnail },\n })\n }\n\n return doc\n}\n"],"mappings":";;;;;;AAOA,MAAaA,kBAA6C,OAAO,EAC/D,YACA,KACA,aACA,UACI;CACJ,MAAM,EAAE,eAAe,iBAAiB;AAGxC,KAAI,IAAI,SAAS,aAAa,MAC5B;MAAI,IAAI,YAAY,QAAQ,QAC1B,KAAI;GACF,MAAM,SAAS,aAAa,QAAQ,aAAa;GACjD,MAAM,SAAS,IAAI;AAEnB,SAAM,QAAQ,KAAK,QAAQ,OAAO;WAC3B,OAAO;AACd,cAAW;IAAE,GAAG,iBAAiB;IAAe,OAAO;IAAO,CAAC;;;AAMrE,KAAI,CAAC,IAAI,aAAa,aAAa,SAAS,IAAI,MAAM;EACpD,MAAM,YAAY,kBAAkB;GAClC,SAAS,IAAI;GACb,YAAY,IAAI,YAAY,QAAQ,IAAI,KAAK;GAC7C,OAAO,IAAI,YAAY,OAAQ,IAAI,QAAQ,IAAI,WAAY;GAC3D;GACD,CAAC;AAEF,MAAI,QAAQ,OAAO;GACjB,YAAY,WAAW;GACvB,IAAI,IAAI;GACR,MAAM,EAAE,WAAW;GACpB,CAAC;;AAGJ,QAAO"}
@@ -1,28 +1,73 @@
1
1
  import { useGrid } from "../gridContext/gridContext.mjs";
2
- import { useMemo } from "react";
2
+ import { c } from "react/compiler-runtime";
3
3
  import { useSelection } from "@payloadcms/ui";
4
4
  import { FolderFileCard } from "@payloadcms/ui/elements/FolderView/FolderFileCard";
5
5
 
6
6
  //#region src/components/folderFileCard/folderFileCard.tsx
7
7
  function ContextFolderFileCard(props) {
8
+ const $ = c(23);
8
9
  const { className, index, item } = props;
9
10
  const { focusedItemIndex, onItemClick, onItemKeyPress } = useGrid();
10
11
  const { selectedIDs } = useSelection();
11
- const isSelected = useMemo(() => selectedIDs.includes(item.id), [selectedIDs, item.id]);
12
- const isFocused = useMemo(() => focusedItemIndex === index, [focusedItemIndex, index]);
13
- return <FolderFileCard className={className} id={item.id} isFocused={isFocused} isSelected={isSelected} itemKey={item.id.toString()} previewUrl={item.thumbnailURL} title={item.title} type={"file"} onClick={(event) => {
14
- onItemClick({
15
- event,
16
- index,
17
- item
18
- });
19
- }} onKeyDown={(event) => {
20
- onItemKeyPress({
21
- event,
22
- index,
23
- item
24
- });
25
- }} />;
12
+ let t0;
13
+ if ($[0] !== item.id || $[1] !== selectedIDs) {
14
+ t0 = selectedIDs.includes(item.id);
15
+ $[0] = item.id;
16
+ $[1] = selectedIDs;
17
+ $[2] = t0;
18
+ } else t0 = $[2];
19
+ const isSelected = t0;
20
+ const isFocused = focusedItemIndex === index;
21
+ const t1 = item.id;
22
+ let t2;
23
+ if ($[3] !== item.id) {
24
+ t2 = item.id.toString();
25
+ $[3] = item.id;
26
+ $[4] = t2;
27
+ } else t2 = $[4];
28
+ let t3;
29
+ if ($[5] !== index || $[6] !== item || $[7] !== onItemClick) {
30
+ t3 = (event) => {
31
+ onItemClick({
32
+ event,
33
+ index,
34
+ item
35
+ });
36
+ };
37
+ $[5] = index;
38
+ $[6] = item;
39
+ $[7] = onItemClick;
40
+ $[8] = t3;
41
+ } else t3 = $[8];
42
+ let t4;
43
+ if ($[9] !== index || $[10] !== item || $[11] !== onItemKeyPress) {
44
+ t4 = (event_0) => {
45
+ onItemKeyPress({
46
+ event: event_0,
47
+ index,
48
+ item
49
+ });
50
+ };
51
+ $[9] = index;
52
+ $[10] = item;
53
+ $[11] = onItemKeyPress;
54
+ $[12] = t4;
55
+ } else t4 = $[12];
56
+ let t5;
57
+ if ($[13] !== className || $[14] !== isFocused || $[15] !== isSelected || $[16] !== item.id || $[17] !== item.thumbnailURL || $[18] !== item.title || $[19] !== t2 || $[20] !== t3 || $[21] !== t4) {
58
+ t5 = <FolderFileCard className={className} id={t1} isFocused={isFocused} isSelected={isSelected} itemKey={t2} previewUrl={item.thumbnailURL} title={item.title} type="file" onClick={t3} onKeyDown={t4} />;
59
+ $[13] = className;
60
+ $[14] = isFocused;
61
+ $[15] = isSelected;
62
+ $[16] = item.id;
63
+ $[17] = item.thumbnailURL;
64
+ $[18] = item.title;
65
+ $[19] = t2;
66
+ $[20] = t3;
67
+ $[21] = t4;
68
+ $[22] = t5;
69
+ } else t5 = $[22];
70
+ return t5;
26
71
  }
27
72
 
28
73
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"folderFileCard.mjs","names":[],"sources":["../../../src/components/folderFileCard/folderFileCard.tsx"],"sourcesContent":["import { useMemo } from 'react'\nimport { FolderFileCard } from '@payloadcms/ui/elements/FolderView/FolderFileCard'\nimport { useSelection } from '@payloadcms/ui'\nimport { useGrid } from '../gridContext/gridContext'\n\nimport type { MappedDocument } from '../gridView/gridView'\n\ntype ContextCardProps = {\n readonly className?: string\n readonly index: number\n readonly item: MappedDocument\n}\n\nexport function ContextFolderFileCard(props: ContextCardProps) {\n const { className, index, item } = props\n\n const { focusedItemIndex, onItemClick, onItemKeyPress } = useGrid()\n const { selectedIDs } = useSelection()\n\n const isSelected = useMemo(\n () => selectedIDs.includes(item.id),\n [selectedIDs, item.id]\n )\n\n const isFocused = useMemo(\n () => focusedItemIndex === index,\n [focusedItemIndex, index]\n )\n\n return (\n <FolderFileCard\n className={className}\n id={item.id}\n isFocused={isFocused}\n isSelected={isSelected}\n itemKey={item.id.toString()}\n previewUrl={item.thumbnailURL}\n title={item.title}\n type={'file'}\n onClick={(event) => {\n void onItemClick({ event, index, item })\n }}\n onKeyDown={(event) => {\n void onItemKeyPress({ event, index, item })\n }}\n />\n )\n}\n"],"mappings":";;;;;;AAaA,SAAgB,sBAAsB,OAAyB;CAC7D,MAAM,EAAE,WAAW,OAAO,SAAS;CAEnC,MAAM,EAAE,kBAAkB,aAAa,mBAAmB,SAAS;CACnE,MAAM,EAAE,gBAAgB,cAAc;CAEtC,MAAM,aAAa,cACX,YAAY,SAAS,KAAK,GAAG,EACnC,CAAC,aAAa,KAAK,GAAG,CACvB;CAED,MAAM,YAAY,cACV,qBAAqB,OAC3B,CAAC,kBAAkB,MAAM,CAC1B;AAED,QACE,CAAC,eACC,WAAW,WACX,IAAI,KAAK,IACT,WAAW,WACX,YAAY,YACZ,SAAS,KAAK,GAAG,UAAU,EAC3B,YAAY,KAAK,cACjB,OAAO,KAAK,OACZ,MAAM,QACN,UAAU,UAAU;AAClB,EAAK,YAAY;GAAE;GAAO;GAAO;GAAM,CAAC;IAE1C,YAAY,UAAU;AACpB,EAAK,eAAe;GAAE;GAAO;GAAO;GAAM,CAAC"}
1
+ {"version":3,"file":"folderFileCard.mjs","names":["useMemo","FolderFileCard","useSelection","useGrid","MappedDocument","ContextCardProps","className","index","item","ContextFolderFileCard","props","$","_c","focusedItemIndex","onItemClick","onItemKeyPress","selectedIDs","t0","id","includes","isSelected","isFocused","t1","t2","toString","t3","event","t4","event_0","t5","thumbnailURL","title"],"sources":["../../../src/components/folderFileCard/folderFileCard.tsx"],"sourcesContent":["import { useMemo } from 'react'\nimport { FolderFileCard } from '@payloadcms/ui/elements/FolderView/FolderFileCard'\nimport { useSelection } from '@payloadcms/ui'\nimport { useGrid } from '../gridContext/gridContext'\n\nimport type { MappedDocument } from '../gridView/gridView'\n\ntype ContextCardProps = {\n readonly className?: string\n readonly index: number\n readonly item: MappedDocument\n}\n\nexport function ContextFolderFileCard(props: ContextCardProps) {\n const { className, index, item } = props\n\n const { focusedItemIndex, onItemClick, onItemKeyPress } = useGrid()\n const { selectedIDs } = useSelection()\n\n const isSelected = useMemo(\n () => selectedIDs.includes(item.id),\n [selectedIDs, item.id]\n )\n\n const isFocused = useMemo(\n () => focusedItemIndex === index,\n [focusedItemIndex, index]\n )\n\n return (\n <FolderFileCard\n className={className}\n id={item.id}\n isFocused={isFocused}\n isSelected={isSelected}\n itemKey={item.id.toString()}\n previewUrl={item.thumbnailURL}\n title={item.title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,IAAA,MAAAI;AAAAJ,IAAA,MAAAgB;OAAAA,MAAAhB,EAAA;CAAA,IAAAkB;AAAA,KAAAlB,EAAA,QAAAL,aAAAK,EAAA,QAAAU,aAAAV,EAAA,QAAAS,cAAAT,EAAA,QAAAH,KAAAU,MAAAP,EAAA,QAAAH,KAAAsB,gBAAAnB,EAAA,QAAAH,KAAAuB,SAAApB,EAAA,QAAAY,MAAAZ,EAAA,QAAAc,MAAAd,EAAA,QAAAgB,IAAA;AAdHE,OAAA,CAAC,eACYvB,WAAAA,WACP,IAAAgB,IACOD,WAAAA,WACCD,YAAAA,YACH,SAAAG,IACG,YAAAf,KAAIsB,cACT,OAAAtB,KAAIuB,OACL,YACG,SAAAN,IAGE,WAAAE;AAGXhB,IAAA,MAAAL;AAAAK,IAAA,MAAAU;AAAAV,IAAA,MAAAS;AAAAT,IAAA,MAAAH,KAAAU;AAAAP,IAAA,MAAAH,KAAAsB;AAAAnB,IAAA,MAAAH,KAAAuB;AAAApB,IAAA,MAAAY;AAAAZ,IAAA,MAAAc;AAAAd,IAAA,MAAAgB;AAAAhB,IAAA,MAAAkB;OAAAA,MAAAlB,EAAA;AAAA,QAfFkB"}
@@ -74,16 +74,16 @@ function GridProvider(props) {
74
74
  }
75
75
  }, [docs, selectedIDs]);
76
76
  const updateSelections = React.useCallback(({ indexes }) => {
77
- const { newSelectedIDs } = docs.reduce((acc, item, index) => {
78
- if (indexes.includes(index)) acc.newSelectedIDs.add(item.id);
79
- return acc;
77
+ const { newSelectedIDs } = docs.reduce((acc_0, item_0, index) => {
78
+ if (indexes.includes(index)) acc_0.newSelectedIDs.add(item_0.id);
79
+ return acc_0;
80
80
  }, { newSelectedIDs: /* @__PURE__ */ new Set() });
81
- selectedIDs.forEach((id) => {
82
- setSelection(id);
81
+ selectedIDs.forEach((id_0) => {
82
+ setSelection(id_0);
83
83
  });
84
- newSelectedIDs.forEach((id) => {
85
- const item = getItem(id);
86
- if (item) setSelection(item.id);
84
+ newSelectedIDs.forEach((id_1) => {
85
+ const item_1 = getItem(id_1);
86
+ if (item_1) setSelection(item_1.id);
87
87
  });
88
88
  }, [
89
89
  docs,
@@ -95,7 +95,7 @@ function GridProvider(props) {
95
95
  const isShiftPressed = shiftKey;
96
96
  const isCtrlPressed = ctrlKey || metaKey;
97
97
  const isCurrentlySelected = selectedIDs.includes(currentItem.id);
98
- const currentItemIndex = docs.findIndex((item) => item.id === currentItem.id);
98
+ const currentItemIndex = docs.findIndex((item_2) => item_2.id === currentItem.id);
99
99
  switch (code) {
100
100
  case "ArrowDown":
101
101
  case "ArrowLeft":
@@ -130,17 +130,17 @@ function GridProvider(props) {
130
130
  if (allowMultiSelection && isCtrlPressed) {
131
131
  event.preventDefault();
132
132
  setFocusedItemIndex(totalCount - 1);
133
- updateSelections({ indexes: Array.from({ length: totalCount }, (_, i) => i) });
133
+ updateSelections({ indexes: Array.from({ length: totalCount }, (__0, i_0) => i_0) });
134
134
  }
135
135
  break;
136
136
  case "Space":
137
137
  if (allowMultiSelection && isShiftPressed) {
138
138
  event.preventDefault();
139
- updateSelections({ indexes: docs.reduce((acc, item, idx) => {
140
- if (item.id === currentItem.id) if (isCurrentlySelected) return acc;
141
- else acc.push(idx);
142
- else if (selectedIDs.includes(item.id)) acc.push(idx);
143
- return acc;
139
+ updateSelections({ indexes: docs.reduce((acc_1, item_3, idx_0) => {
140
+ if (item_3.id === currentItem.id) if (isCurrentlySelected) return acc_1;
141
+ else acc_1.push(idx_0);
142
+ else if (selectedIDs.includes(item_3.id)) acc_1.push(idx_0);
143
+ return acc_1;
144
144
  }, []) });
145
145
  } else {
146
146
  event.preventDefault();
@@ -165,30 +165,30 @@ function GridProvider(props) {
165
165
  totalCount
166
166
  ]);
167
167
  const [lastClickedItem, setLastClickedItem] = React.useState();
168
- const onItemClick = React.useCallback(({ event, item: clickedItem }) => {
168
+ const onItemClick = React.useCallback(({ event: event_0, item: clickedItem }) => {
169
169
  let doubleClicked = false;
170
- const isCtrlPressed = event.ctrlKey || event.metaKey;
171
- const isShiftPressed = event.shiftKey;
172
- const isCurrentlySelected = selectedIDs.includes(clickedItem.id);
173
- const currentItemIndex = docs.findIndex((item) => item.id === clickedItem.id);
170
+ const isCtrlPressed_0 = event_0.ctrlKey || event_0.metaKey;
171
+ const isShiftPressed_0 = event_0.shiftKey;
172
+ const isCurrentlySelected_0 = selectedIDs.includes(clickedItem.id);
173
+ const currentItemIndex_0 = docs.findIndex((item_4) => item_4.id === clickedItem.id);
174
174
  switch (true) {
175
- case allowMultiSelection && isCtrlPressed:
176
- event.preventDefault();
177
- updateSelections({ indexes: docs.reduce((acc, item, idx) => {
178
- if (item.id === clickedItem.id) {
179
- if (!isCurrentlySelected) acc.push(idx);
180
- } else if (selectedIDs.includes(item.id)) acc.push(idx);
181
- return acc;
175
+ case allowMultiSelection && isCtrlPressed_0:
176
+ event_0.preventDefault();
177
+ updateSelections({ indexes: docs.reduce((acc_2, item_5, idx_1) => {
178
+ if (item_5.id === clickedItem.id) {
179
+ if (!isCurrentlySelected_0) acc_2.push(idx_1);
180
+ } else if (selectedIDs.includes(item_5.id)) acc_2.push(idx_1);
181
+ return acc_2;
182
182
  }, []) });
183
183
  break;
184
- case allowMultiSelection && isShiftPressed:
185
- if (currentItemIndex !== -1) updateSelections({ indexes: handleShiftSelection(currentItemIndex) });
184
+ case allowMultiSelection && isShiftPressed_0:
185
+ if (currentItemIndex_0 !== -1) updateSelections({ indexes: handleShiftSelection(currentItemIndex_0) });
186
186
  break;
187
187
  default: {
188
188
  const now = Date.now();
189
189
  doubleClicked = now - (lastClickTime.current ?? 0) < 400 && lastClickedItem?.id === clickedItem.id;
190
190
  lastClickTime.current = now;
191
- if (!doubleClicked) updateSelections({ indexes: isCurrentlySelected && selectedIDs.length === 1 ? [] : [currentItemIndex] });
191
+ if (!doubleClicked) updateSelections({ indexes: isCurrentlySelected_0 && selectedIDs.length === 1 ? [] : [currentItemIndex_0] });
192
192
  break;
193
193
  }
194
194
  }
@@ -1 +1 @@
1
- {"version":3,"file":"gridContext.mjs","names":["onItemKeyPress: GridContextValue['onItemKeyPress']","onItemClick: GridContextValue['onItemClick']","doubleClicked: boolean"],"sources":["../../../src/components/gridContext/gridContext.tsx"],"sourcesContent":["'use client'\n\nimport React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { formatAdminURL } from 'payload/shared'\n\nimport { useDrawerDepth } from '@payloadcms/ui/elements/Drawer'\nimport { useConfig, useRouteTransition, useSelection } from '@payloadcms/ui'\n\nimport type { FolderSortKeys } from 'payload'\nimport type { MappedDocument } from '../gridView/gridView'\ninterface OnItemClickArgs {\n event: React.MouseEvent\n index: number\n item: MappedDocument\n}\n\ninterface OnItemKeyPressArgs {\n event: React.KeyboardEvent\n index: number\n item: MappedDocument\n}\n\nexport type GridContextValue = {\n docs?: MappedDocument[]\n clearSelections: () => void\n onItemClick: (args: OnItemClickArgs) => void\n onItemKeyPress: (args: OnItemKeyPressArgs) => void\n setFocusedItemIndex: React.Dispatch<React.SetStateAction<number>>\n focusedItemIndex: number\n}\n\nconst Context = React.createContext<GridContextValue>({\n docs: [],\n clearSelections: () => {},\n onItemClick: () => undefined,\n onItemKeyPress: () => undefined,\n setFocusedItemIndex: () => -1,\n focusedItemIndex: -1,\n})\n\nexport type GridProviderProps = {\n readonly allowMultiSelection?: boolean\n /**\n * Children to render inside the provider\n */\n readonly children: React.ReactNode\n /**\n * All documents in the current folder\n */\n readonly docs: MappedDocument[]\n /**\n * The intial search query\n */\n readonly search?: string\n /**\n * The sort order of the documents\n *\n * @example\n * `name` for descending\n * `-name` for ascending\n */\n readonly sort?: FolderSortKeys\n}\nexport function GridProvider(props: GridProviderProps) {\n const { allowMultiSelection = true, children, docs } = props\n\n const { config } = useConfig()\n const drawerDepth = useDrawerDepth()\n const router = useRouter()\n const { startRouteTransition } = useRouteTransition()\n\n const { setSelection, selectedIDs } = useSelection()\n const currentlySelectedIndexes = React.useRef(new Set<number>())\n\n const [focusedItemIndex, setFocusedItemIndex] = React.useState(-1)\n const lastClickTime = React.useRef<null | number>(null)\n const totalCount = docs.length\n\n const clearSelections = React.useCallback(() => {\n setFocusedItemIndex(-1)\n\n currentlySelectedIndexes.current = new Set()\n }, [])\n\n const getItem = React.useCallback(\n (id: string | number) => {\n return docs.find((doc) => doc.id === id)\n },\n [docs]\n )\n\n const navigateAfterSelection = React.useCallback(\n ({\n collectionSlug,\n docID,\n }: {\n collectionSlug: string\n docID?: number | string\n }) => {\n if (drawerDepth === 1) {\n // not in a drawer (default is 1)\n if (collectionSlug) {\n // clicked on document, take the user to the documet view\n startRouteTransition(() => {\n router.push(\n formatAdminURL({\n adminRoute: config.routes.admin,\n path: `/collections/${collectionSlug}/${docID}`,\n })\n )\n clearSelections()\n })\n }\n } else {\n clearSelections()\n }\n },\n [\n clearSelections,\n config.routes.admin,\n drawerDepth,\n getItem,\n router,\n startRouteTransition,\n ]\n )\n\n const handleShiftSelection = React.useCallback(\n (targetIndex: number) => {\n // Find existing selection boundaries\n const existingIndexes = docs.reduce((acc, item, idx) => {\n if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[])\n\n if (existingIndexes.length === 0) {\n // No existing selection, just select target\n return [targetIndex]\n }\n\n const firstSelectedIndex = Math.min(...existingIndexes)\n const lastSelectedIndex = Math.max(...existingIndexes)\n const isWithinBounds =\n targetIndex >= firstSelectedIndex && targetIndex <= lastSelectedIndex\n\n // Choose anchor based on whether we're contracting or extending\n let anchorIndex = targetIndex\n if (isWithinBounds) {\n // Contracting: if target is at a boundary, use target as anchor\n // Otherwise, use furthest boundary to maintain opposite edge\n if (\n targetIndex === firstSelectedIndex ||\n targetIndex === lastSelectedIndex\n ) {\n anchorIndex = targetIndex\n } else {\n const distanceToFirst = Math.abs(targetIndex - firstSelectedIndex)\n const distanceToLast = Math.abs(targetIndex - lastSelectedIndex)\n anchorIndex =\n distanceToFirst >= distanceToLast\n ? firstSelectedIndex\n : lastSelectedIndex\n }\n } else {\n // Extending: use closest boundary\n const distanceToFirst = Math.abs(targetIndex - firstSelectedIndex)\n const distanceToLast = Math.abs(targetIndex - lastSelectedIndex)\n anchorIndex =\n distanceToFirst <= distanceToLast\n ? firstSelectedIndex\n : lastSelectedIndex\n }\n\n // Create range from anchor to target\n const startIndex = Math.min(anchorIndex, targetIndex)\n const endIndex = Math.max(anchorIndex, targetIndex)\n const newRangeIndexes = Array.from(\n { length: endIndex - startIndex + 1 },\n (_, i) => startIndex + i\n )\n\n if (isWithinBounds) {\n // Contracting: replace with new range\n return newRangeIndexes\n } else {\n // Extending: union with existing\n const mappedSet = new Set([...existingIndexes, ...newRangeIndexes])\n return Array.from(mappedSet)\n }\n },\n [docs, selectedIDs]\n )\n\n const updateSelections = React.useCallback(\n ({ indexes }: { indexes: number[] }) => {\n const { newSelectedIDs } = docs.reduce(\n (acc, item, index) => {\n if (indexes.includes(index)) {\n acc.newSelectedIDs.add(item.id)\n }\n return acc\n },\n {\n newSelectedIDs: new Set<string | number>(),\n }\n )\n\n // Clear previous selection in global selection context\n selectedIDs.forEach((id) => {\n setSelection(id)\n })\n\n // Update selection in global selection context\n newSelectedIDs.forEach((id) => {\n const item = getItem(id)\n if (item) {\n setSelection(item.id)\n }\n })\n },\n [docs, getItem, setSelection]\n )\n\n const onItemKeyPress: GridContextValue['onItemKeyPress'] = React.useCallback(\n ({ event, item: currentItem }) => {\n const { code, ctrlKey, metaKey, shiftKey } = event\n\n const isShiftPressed = shiftKey\n const isCtrlPressed = ctrlKey || metaKey\n const isCurrentlySelected = selectedIDs.includes(currentItem.id)\n const currentItemIndex = docs.findIndex(\n (item) => item.id === currentItem.id\n )\n\n switch (code) {\n case 'ArrowDown':\n case 'ArrowLeft':\n case 'ArrowRight':\n case 'ArrowUp': {\n event.preventDefault()\n\n if (currentItemIndex === -1) {\n break\n }\n\n const isBackward = code === 'ArrowLeft' || code === 'ArrowUp'\n const newItemIndex = isBackward\n ? currentItemIndex - 1\n : currentItemIndex + 1\n\n if (newItemIndex < 0 || newItemIndex > totalCount - 1) {\n // out of bounds, keep current selection\n return\n }\n\n setFocusedItemIndex(newItemIndex)\n\n if (isCtrlPressed) {\n break\n }\n\n if (isShiftPressed && allowMultiSelection) {\n const selectedIndexes = handleShiftSelection(newItemIndex)\n updateSelections({ indexes: selectedIndexes })\n return\n }\n\n // Single selection without shift\n if (!isShiftPressed) {\n // const newItem = allItems[newItemIndex]\n updateSelections({ indexes: [newItemIndex] })\n }\n\n break\n }\n case 'Enter': {\n if (selectedIDs.length === 1) {\n // setFocusedItemIndex(undefined)\n navigateAfterSelection({\n collectionSlug: currentItem.relationTo,\n docID: currentItem.id,\n })\n return\n }\n break\n }\n case 'Escape': {\n clearSelections()\n break\n }\n case 'KeyA': {\n if (allowMultiSelection && isCtrlPressed) {\n event.preventDefault()\n setFocusedItemIndex(totalCount - 1)\n updateSelections({\n indexes: Array.from({ length: totalCount }, (_, i) => i),\n })\n }\n break\n }\n case 'Space': {\n if (allowMultiSelection && isShiftPressed) {\n event.preventDefault()\n updateSelections({\n indexes: docs.reduce((acc, item, idx) => {\n if (item.id === currentItem.id) {\n if (isCurrentlySelected) {\n return acc\n } else {\n acc.push(idx)\n }\n } else if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[]),\n })\n } else {\n event.preventDefault()\n updateSelections({\n indexes: isCurrentlySelected ? [] : [currentItemIndex],\n })\n }\n break\n }\n case 'Tab': {\n if (allowMultiSelection && isShiftPressed) {\n const prevIndex = currentItemIndex - 1\n if (prevIndex < 0 && selectedIDs.length > 0) {\n setFocusedItemIndex(prevIndex)\n }\n } else {\n const nextIndex = currentItemIndex + 1\n if (nextIndex === totalCount && selectedIDs.length > 0) {\n setFocusedItemIndex(totalCount - 1)\n }\n }\n break\n }\n }\n },\n [\n selectedIDs,\n docs,\n allowMultiSelection,\n handleShiftSelection,\n updateSelections,\n navigateAfterSelection,\n clearSelections,\n totalCount,\n ]\n )\n\n // Track last clicked item for double-click detection\n const [lastClickedItem, setLastClickedItem] = React.useState<\n MappedDocument | undefined\n >()\n\n const onItemClick: GridContextValue['onItemClick'] = React.useCallback(\n ({ event, item: clickedItem }) => {\n let doubleClicked: boolean = false\n const isCtrlPressed = event.ctrlKey || event.metaKey\n const isShiftPressed = event.shiftKey\n const isCurrentlySelected = selectedIDs.includes(clickedItem.id)\n const currentItemIndex = docs.findIndex(\n (item) => item.id === clickedItem.id\n )\n\n switch (true) {\n case allowMultiSelection && isCtrlPressed: {\n event.preventDefault()\n const indexes = docs.reduce((acc, item, idx) => {\n if (item.id === clickedItem.id) {\n if (!isCurrentlySelected) {\n acc.push(idx)\n }\n } else if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[])\n\n updateSelections({ indexes })\n break\n }\n case allowMultiSelection && isShiftPressed: {\n if (currentItemIndex !== -1) {\n const selectedIndexes = handleShiftSelection(currentItemIndex)\n updateSelections({ indexes: selectedIndexes })\n }\n break\n }\n default: {\n const now = Date.now()\n doubleClicked =\n now - (lastClickTime.current ?? 0) < 400 &&\n lastClickedItem?.id === clickedItem.id\n lastClickTime.current = now\n\n if (!doubleClicked) {\n updateSelections({\n indexes:\n isCurrentlySelected && selectedIDs.length === 1\n ? []\n : [currentItemIndex],\n })\n }\n break\n }\n }\n\n // Update last clicked item to determine double clicks\n setLastClickedItem(clickedItem)\n\n if (doubleClicked) {\n navigateAfterSelection({\n collectionSlug: clickedItem.relationTo,\n docID: clickedItem.id,\n })\n }\n },\n [\n docs,\n allowMultiSelection,\n getItem,\n updateSelections,\n navigateAfterSelection,\n handleShiftSelection,\n ]\n )\n\n return (\n <Context\n value={{\n clearSelections,\n docs,\n focusedItemIndex,\n onItemClick,\n onItemKeyPress,\n setFocusedItemIndex,\n }}\n >\n {children}\n </Context>\n )\n}\n\nexport function useGrid(): GridContextValue {\n const context = React.use(Context)\n\n if (context === undefined) {\n throw new Error('useGrid must be used within a GridProvider')\n }\n\n return context\n}\n"],"mappings":";;;;;;;;;AAgCA,MAAM,UAAU,MAA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ACf,SACE,uBAAuB,YAAY,WAAW,IAC1C,EAAE,GACF,CAAC,iBAAiB,EACzB,CAAC;AAEJ;;;AAKJ,qBAAmB,YAAY;AAE/B,MAAI,cACF,wBAAuB;GACrB,gBAAgB,YAAY;GAC5B,OAAO,YAAY;GACpB,CAAC;IAGN;EACE;EACA;EACA;EACA;EACA;EACA;EACD,CACF;AAED,QACE,CAAC,QACC,OAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACD,EACF;OACE,SAAS;IACZ,EAAE;;AAIN,SAAgB,UAA4B;CAC1C,MAAM,UAAU,MAAM,IAAI,QAAQ;AAElC,KAAI,YAAY,OACd,OAAM,IAAI,MAAM,6CAA6C;AAG/D,QAAO"}
1
+ {"version":3,"file":"gridContext.mjs","names":["React","useRouter","formatAdminURL","useDrawerDepth","useConfig","useRouteTransition","useSelection","FolderSortKeys","MappedDocument","OnItemClickArgs","event","MouseEvent","index","item","OnItemKeyPressArgs","KeyboardEvent","GridContextValue","docs","clearSelections","onItemClick","args","onItemKeyPress","setFocusedItemIndex","Dispatch","SetStateAction","focusedItemIndex","Context","createContext","undefined","GridProviderProps","allowMultiSelection","children","ReactNode","search","sort","GridProvider","props","config","drawerDepth","router","startRouteTransition","setSelection","selectedIDs","currentlySelectedIndexes","useRef","Set","useState","lastClickTime","totalCount","length","useCallback","current","getItem","id","find","doc","navigateAfterSelection","collectionSlug","docID","push","adminRoute","routes","admin","path","handleShiftSelection","targetIndex","existingIndexes","reduce","acc","idx","includes","firstSelectedIndex","Math","min","lastSelectedIndex","max","isWithinBounds","anchorIndex","distanceToFirst","abs","distanceToLast","startIndex","endIndex","newRangeIndexes","Array","from","_","i","mappedSet","updateSelections","indexes","newSelectedIDs","add","forEach","currentItem","code","ctrlKey","metaKey","shiftKey","isShiftPressed","isCtrlPressed","isCurrentlySelected","currentItemIndex","findIndex","preventDefault","isBackward","newItemIndex","selectedIndexes","relationTo","prevIndex","nextIndex","lastClickedItem","setLastClickedItem","clickedItem","doubleClicked","now","Date","useGrid","context","use","Error"],"sources":["../../../src/components/gridContext/gridContext.tsx"],"sourcesContent":["'use client'\n\nimport React from 'react'\nimport { useRouter } from 'next/navigation'\nimport { formatAdminURL } from 'payload/shared'\n\nimport { useDrawerDepth } from '@payloadcms/ui/elements/Drawer'\nimport { useConfig, useRouteTransition, useSelection } from '@payloadcms/ui'\n\nimport type { FolderSortKeys } from 'payload'\nimport type { MappedDocument } from '../gridView/gridView'\ninterface OnItemClickArgs {\n event: React.MouseEvent\n index: number\n item: MappedDocument\n}\n\ninterface OnItemKeyPressArgs {\n event: React.KeyboardEvent\n index: number\n item: MappedDocument\n}\n\nexport type GridContextValue = {\n docs?: MappedDocument[]\n clearSelections: () => void\n onItemClick: (args: OnItemClickArgs) => void\n onItemKeyPress: (args: OnItemKeyPressArgs) => void\n setFocusedItemIndex: React.Dispatch<React.SetStateAction<number>>\n focusedItemIndex: number\n}\n\nconst Context = React.createContext<GridContextValue>({\n docs: [],\n clearSelections: () => {},\n onItemClick: () => undefined,\n onItemKeyPress: () => undefined,\n setFocusedItemIndex: () => -1,\n focusedItemIndex: -1,\n})\n\nexport type GridProviderProps = {\n readonly allowMultiSelection?: boolean\n /**\n * Children to render inside the provider\n */\n readonly children: React.ReactNode\n /**\n * All documents in the current folder\n */\n readonly docs: MappedDocument[]\n /**\n * The intial search query\n */\n readonly search?: string\n /**\n * The sort order of the documents\n *\n * @example\n * `name` for descending\n * `-name` for ascending\n */\n readonly sort?: FolderSortKeys\n}\nexport function GridProvider(props: GridProviderProps) {\n const { allowMultiSelection = true, children, docs } = props\n\n const { config } = useConfig()\n const drawerDepth = useDrawerDepth()\n const router = useRouter()\n const { startRouteTransition } = useRouteTransition()\n\n const { setSelection, selectedIDs } = useSelection()\n const currentlySelectedIndexes = React.useRef(new Set<number>())\n\n const [focusedItemIndex, setFocusedItemIndex] = React.useState(-1)\n const lastClickTime = React.useRef<null | number>(null)\n const totalCount = docs.length\n\n const clearSelections = React.useCallback(() => {\n setFocusedItemIndex(-1)\n\n currentlySelectedIndexes.current = new Set()\n }, [])\n\n const getItem = React.useCallback(\n (id: string | number) => {\n return docs.find((doc) => doc.id === id)\n },\n [docs]\n )\n\n const navigateAfterSelection = React.useCallback(\n ({\n collectionSlug,\n docID,\n }: {\n collectionSlug: string\n docID?: number | string\n }) => {\n if (drawerDepth === 1) {\n // not in a drawer (default is 1)\n if (collectionSlug) {\n // clicked on document, take the user to the documet view\n startRouteTransition(() => {\n router.push(\n formatAdminURL({\n adminRoute: config.routes.admin,\n path: `/collections/${collectionSlug}/${docID}`,\n })\n )\n clearSelections()\n })\n }\n } else {\n clearSelections()\n }\n },\n [\n clearSelections,\n config.routes.admin,\n drawerDepth,\n getItem,\n router,\n startRouteTransition,\n ]\n )\n\n const handleShiftSelection = React.useCallback(\n (targetIndex: number) => {\n // Find existing selection boundaries\n const existingIndexes = docs.reduce((acc, item, idx) => {\n if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[])\n\n if (existingIndexes.length === 0) {\n // No existing selection, just select target\n return [targetIndex]\n }\n\n const firstSelectedIndex = Math.min(...existingIndexes)\n const lastSelectedIndex = Math.max(...existingIndexes)\n const isWithinBounds =\n targetIndex >= firstSelectedIndex && targetIndex <= lastSelectedIndex\n\n // Choose anchor based on whether we're contracting or extending\n let anchorIndex = targetIndex\n if (isWithinBounds) {\n // Contracting: if target is at a boundary, use target as anchor\n // Otherwise, use furthest boundary to maintain opposite edge\n if (\n targetIndex === firstSelectedIndex ||\n targetIndex === lastSelectedIndex\n ) {\n anchorIndex = targetIndex\n } else {\n const distanceToFirst = Math.abs(targetIndex - firstSelectedIndex)\n const distanceToLast = Math.abs(targetIndex - lastSelectedIndex)\n anchorIndex =\n distanceToFirst >= distanceToLast\n ? firstSelectedIndex\n : lastSelectedIndex\n }\n } else {\n // Extending: use closest boundary\n const distanceToFirst = Math.abs(targetIndex - firstSelectedIndex)\n const distanceToLast = Math.abs(targetIndex - lastSelectedIndex)\n anchorIndex =\n distanceToFirst <= distanceToLast\n ? firstSelectedIndex\n : lastSelectedIndex\n }\n\n // Create range from anchor to target\n const startIndex = Math.min(anchorIndex, targetIndex)\n const endIndex = Math.max(anchorIndex, targetIndex)\n const newRangeIndexes = Array.from(\n { length: endIndex - startIndex + 1 },\n (_, i) => startIndex + i\n )\n\n if (isWithinBounds) {\n // Contracting: replace with new range\n return newRangeIndexes\n } else {\n // Extending: union with existing\n const mappedSet = new Set([...existingIndexes, ...newRangeIndexes])\n return Array.from(mappedSet)\n }\n },\n [docs, selectedIDs]\n )\n\n const updateSelections = React.useCallback(\n ({ indexes }: { indexes: number[] }) => {\n const { newSelectedIDs } = docs.reduce(\n (acc, item, index) => {\n if (indexes.includes(index)) {\n acc.newSelectedIDs.add(item.id)\n }\n return acc\n },\n {\n newSelectedIDs: new Set<string | number>(),\n }\n )\n\n // Clear previous selection in global selection context\n selectedIDs.forEach((id) => {\n setSelection(id)\n })\n\n // Update selection in global selection context\n newSelectedIDs.forEach((id) => {\n const item = getItem(id)\n if (item) {\n setSelection(item.id)\n }\n })\n },\n [docs, getItem, setSelection]\n )\n\n const onItemKeyPress: GridContextValue['onItemKeyPress'] = React.useCallback(\n ({ event, item: currentItem }) => {\n const { code, ctrlKey, metaKey, shiftKey } = event\n\n const isShiftPressed = shiftKey\n const isCtrlPressed = ctrlKey || metaKey\n const isCurrentlySelected = selectedIDs.includes(currentItem.id)\n const currentItemIndex = docs.findIndex(\n (item) => item.id === currentItem.id\n )\n\n switch (code) {\n case 'ArrowDown':\n case 'ArrowLeft':\n case 'ArrowRight':\n case 'ArrowUp': {\n event.preventDefault()\n\n if (currentItemIndex === -1) {\n break\n }\n\n const isBackward = code === 'ArrowLeft' || code === 'ArrowUp'\n const newItemIndex = isBackward\n ? currentItemIndex - 1\n : currentItemIndex + 1\n\n if (newItemIndex < 0 || newItemIndex > totalCount - 1) {\n // out of bounds, keep current selection\n return\n }\n\n setFocusedItemIndex(newItemIndex)\n\n if (isCtrlPressed) {\n break\n }\n\n if (isShiftPressed && allowMultiSelection) {\n const selectedIndexes = handleShiftSelection(newItemIndex)\n updateSelections({ indexes: selectedIndexes })\n return\n }\n\n // Single selection without shift\n if (!isShiftPressed) {\n // const newItem = allItems[newItemIndex]\n updateSelections({ indexes: [newItemIndex] })\n }\n\n break\n }\n case 'Enter': {\n if (selectedIDs.length === 1) {\n // setFocusedItemIndex(undefined)\n navigateAfterSelection({\n collectionSlug: currentItem.relationTo,\n docID: currentItem.id,\n })\n return\n }\n break\n }\n case 'Escape': {\n clearSelections()\n break\n }\n case 'KeyA': {\n if (allowMultiSelection && isCtrlPressed) {\n event.preventDefault()\n setFocusedItemIndex(totalCount - 1)\n updateSelections({\n indexes: Array.from({ length: totalCount }, (_, i) => i),\n })\n }\n break\n }\n case 'Space': {\n if (allowMultiSelection && isShiftPressed) {\n event.preventDefault()\n updateSelections({\n indexes: docs.reduce((acc, item, idx) => {\n if (item.id === currentItem.id) {\n if (isCurrentlySelected) {\n return acc\n } else {\n acc.push(idx)\n }\n } else if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[]),\n })\n } else {\n event.preventDefault()\n updateSelections({\n indexes: isCurrentlySelected ? [] : [currentItemIndex],\n })\n }\n break\n }\n case 'Tab': {\n if (allowMultiSelection && isShiftPressed) {\n const prevIndex = currentItemIndex - 1\n if (prevIndex < 0 && selectedIDs.length > 0) {\n setFocusedItemIndex(prevIndex)\n }\n } else {\n const nextIndex = currentItemIndex + 1\n if (nextIndex === totalCount && selectedIDs.length > 0) {\n setFocusedItemIndex(totalCount - 1)\n }\n }\n break\n }\n }\n },\n [\n selectedIDs,\n docs,\n allowMultiSelection,\n handleShiftSelection,\n updateSelections,\n navigateAfterSelection,\n clearSelections,\n totalCount,\n ]\n )\n\n // Track last clicked item for double-click detection\n const [lastClickedItem, setLastClickedItem] = React.useState<\n MappedDocument | undefined\n >()\n\n const onItemClick: GridContextValue['onItemClick'] = React.useCallback(\n ({ event, item: clickedItem }) => {\n let doubleClicked: boolean = false\n const isCtrlPressed = event.ctrlKey || event.metaKey\n const isShiftPressed = event.shiftKey\n const isCurrentlySelected = selectedIDs.includes(clickedItem.id)\n const currentItemIndex = docs.findIndex(\n (item) => item.id === clickedItem.id\n )\n\n switch (true) {\n case allowMultiSelection && isCtrlPressed: {\n event.preventDefault()\n const indexes = docs.reduce((acc, item, idx) => {\n if (item.id === clickedItem.id) {\n if (!isCurrentlySelected) {\n acc.push(idx)\n }\n } else if (selectedIDs.includes(item.id)) {\n acc.push(idx)\n }\n return acc\n }, [] as number[])\n\n updateSelections({ indexes })\n break\n }\n case allowMultiSelection && isShiftPressed: {\n if (currentItemIndex !== -1) {\n const selectedIndexes = handleShiftSelection(currentItemIndex)\n updateSelections({ indexes: selectedIndexes })\n }\n break\n }\n default: {\n const now = Date.now()\n doubleClicked =\n now - (lastClickTime.current ?? 0) < 400 &&\n lastClickedItem?.id === clickedItem.id\n lastClickTime.current = now\n\n if (!doubleClicked) {\n updateSelections({\n indexes:\n isCurrentlySelected && selectedIDs.length === 1\n ? []\n : [currentItemIndex],\n })\n }\n break\n }\n }\n\n // Update last clicked item to determine double clicks\n setLastClickedItem(clickedItem)\n\n if (doubleClicked) {\n navigateAfterSelection({\n collectionSlug: clickedItem.relationTo,\n docID: clickedItem.id,\n })\n }\n },\n [\n docs,\n allowMultiSelection,\n getItem,\n updateSelections,\n navigateAfterSelection,\n handleShiftSelection,\n ]\n )\n\n return (\n <Context\n value={{\n clearSelections,\n docs,\n focusedItemIndex,\n onItemClick,\n onItemKeyPress,\n setFocusedItemIndex,\n }}\n >\n {children}\n </Context>\n )\n}\n\nexport function useGrid(): GridContextValue {\n const context = React.use(Context)\n\n if (context === undefined) {\n throw new Error('useGrid must be used within a GridProvider')\n }\n\n return context\n}\n"],"mappings":";;;;;;;;;AAgCA,MAAM0B,UAAU1B,MAAM2B,cAAgC;CACpDV,MAAM,EAAE;CACRC,uBAAuB;CACvBC,mBAAmBS;CACnBP,s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