@@ -8,16 +8,14 @@ import { swrLibreriesFetcher } from '../lib/fetcher'
88import { getLibraryListQuery } from '../lib/graphql/queries'
99import RasterImage from './Image'
1010import XMark from './icons/XMark'
11- import { arrayMove } from '@dnd-kit/sortable'
1211
1312import type { DragEndEvent , DragStartEvent , UniqueIdentifier } from '@dnd-kit/core'
1413import { DndContext , closestCenter , DragOverlay } from '@dnd-kit/core'
1514import { SortableContext , useSortable , verticalListSortingStrategy } from '@dnd-kit/sortable'
15+ import { useSelectedPhotosStore } from '../lib/store/useSelectedPhotosStore'
1616
1717type Props = {
1818 ctx : RenderModalCtx
19- selectedPhotos : any [ ]
20- setSelectedPhotos : ( photos : any ) => void
2119}
2220
2321interface SortableItemProps {
@@ -54,11 +52,17 @@ function SortableItem({ id, children, isDragging }: SortableItemProps) {
5452 )
5553}
5654
57- const Selector = ( { ctx, selectedPhotos , setSelectedPhotos } : Props ) => {
55+ const Selector = ( { ctx } : Props ) => {
5856 const [ libraries , setLibraries ] = useState < Library [ ] > ( [ ] )
5957 const [ selectedLibrary , setSelectedLibrary ] = useState < Library | null > ( null )
6058 const [ selectedVersions , setSelectedVersions ] = useState < string [ ] > ( [ ] )
6159
60+ const [ selectedPhotos , reorderPhotos , setPhoto ] = useSelectedPhotosStore ( ( state ) => [
61+ state . selectedPhotos ,
62+ state . reorderPhotos ,
63+ state . setPhoto ,
64+ ] )
65+
6266 const { orgId, apiKey } = ctx . plugin . attributes . parameters
6367
6468 const { data } = useSWR (
@@ -103,14 +107,7 @@ const Selector = ({ ctx, selectedPhotos, setSelectedPhotos }: Props) => {
103107 const handleDragEnd = ( event : DragEndEvent ) => {
104108 setActiveId ( null )
105109 setIsDragging ( false )
106- const { active, over } = event
107- if ( active && over && active . id !== over . id ) {
108- setSelectedPhotos ( ( items : Image [ ] ) => {
109- const oldIndex = items . findIndex ( ( item ) => item . id === active . id )
110- const newIndex = items . findIndex ( ( item ) => item . id === over . id )
111- return arrayMove ( [ ...items ] , oldIndex , newIndex )
112- } )
113- }
110+ reorderPhotos ( event )
114111 }
115112
116113 const handleDragStart = ( event : DragStartEvent ) => {
@@ -132,7 +129,7 @@ const Selector = ({ ctx, selectedPhotos, setSelectedPhotos }: Props) => {
132129 < button
133130 className = "w-fit h-fit bg-green hover:bg-green-dark text-white px-3 py-2 rounded font-medium transition-colors"
134131 type = "button"
135- onClick = { ( ) => ctx . resolve ( selectedPhotos ) }
132+ onClick = { ( ) => ctx . resolve ( JSON . stringify ( selectedPhotos ) ) }
136133 >
137134 Confirm
138135 </ button >
@@ -179,14 +176,10 @@ const Selector = ({ ctx, selectedPhotos, setSelectedPhotos }: Props) => {
179176 >
180177 < button
181178 type = "button"
182- onClick = { ( ) =>
183- setSelectedPhotos ( ( prev : any [ ] ) =>
184- prev . filter ( ( photo ) => photo . id !== image . id )
185- )
186- }
179+ onClick = { ( ) => setPhoto ( image ) }
187180 aria-label = "Remove image"
188181 className = { clsx (
189- 'z-10 text-white border-[3px] border-white absolute -top-2.5 -right-2.5 h-8 w-8 flex justify-center items-center bg-primary rounded-full p-1 hover:bg-primary-dark transition-colors' ,
182+ 'z-50 text-white border-[3px] border-white absolute -top-2.5 -right-2.5 h-8 w-8 flex justify-center items-center bg-primary rounded-full p-1 hover:bg-primary-dark transition-colors' ,
190183 { 'opacity-0' : isDragging }
191184 ) }
192185 >
@@ -257,7 +250,7 @@ const Selector = ({ ctx, selectedPhotos, setSelectedPhotos }: Props) => {
257250 < button
258251 className = "w-fit h-fit bg-green hover:bg-green-dark text-white px-3 py-2 rounded font-medium transition-colors"
259252 type = "button"
260- onClick = { ( ) => ctx . resolve ( selectedPhotos ) }
253+ onClick = { ( ) => ctx . resolve ( JSON . stringify ( selectedPhotos ) ) }
261254 >
262255 Confirm
263256 </ button >
0 commit comments