1- import React from 'react'
1+ import { useLayoutEffect , useRef } from 'react'
2+ import SortableList , { SortableItem } from 'react-easy-sort'
23import { SUPPORTED_CARDS } from 'src/config'
34import { CustomRssCard } from 'src/features/cards'
5+ import { trackPageDrag } from 'src/lib/analytics'
6+ import { useUserPreferences } from 'src/stores/preferences'
47import { SelectedCard , SupportedCardType } from 'src/types'
58
69export const DesktopCards = ( {
@@ -11,20 +14,47 @@ export const DesktopCards = ({
1114 userCustomCards : SupportedCardType [ ]
1215} ) => {
1316 const AVAILABLE_CARDS = [ ...SUPPORTED_CARDS , ...userCustomCards ]
17+ const { updateCardOrder } = useUserPreferences ( )
18+ const scrollHolderRef = useRef < HTMLElement | null > ( null )
19+
20+ const onSortEnd = ( oldIndex : number , newIndex : number ) => {
21+ updateCardOrder ( oldIndex , newIndex )
22+ trackPageDrag ( )
23+ if ( newIndex === 0 || ( oldIndex > 3 && newIndex < 3 ) ) {
24+ scrollHolderRef . current ?. scrollTo ( 0 , 0 )
25+ }
26+ }
27+
28+ useLayoutEffect ( ( ) => {
29+ scrollHolderRef . current = document . querySelector ( '.Cards' )
30+ } , [ ] )
31+
1432 return (
15- < >
16- { cards . map ( ( card , index ) => {
17- const constantCard = AVAILABLE_CARDS . find ( ( c ) => c . value === card . name )
18- if ( ! constantCard ) {
19- return null
20- }
33+ < SortableList
34+ as = "div"
35+ onSortEnd = { onSortEnd }
36+ lockAxis = "x"
37+ customHolderRef = { scrollHolderRef }
38+ className = "Cards HorizontalScroll"
39+ draggedItemClassName = "draggedBlock" >
40+ { cards
41+ . sort ( ( a , b ) => a . id - b . id )
42+ . map ( ( card , index ) => {
43+ const constantCard = AVAILABLE_CARDS . find ( ( c ) => c . value === card . name )
44+ if ( ! constantCard ) {
45+ return null
46+ }
47+
48+ const Component = constantCard ?. component || CustomRssCard
2149
22- return React . createElement ( constantCard ?. component || CustomRssCard , {
23- key : card . name ,
24- meta : constantCard ,
25- withAds : index === 0 ,
26- } )
27- } ) }
28- </ >
50+ return (
51+ < SortableItem key = { card . name } >
52+ < div >
53+ < Component key = { card . name } meta = { constantCard } withAds = { index === 0 } />
54+ </ div >
55+ </ SortableItem >
56+ )
57+ } ) }
58+ </ SortableList >
2959 )
3060}
0 commit comments