disco
/
backend
/
views
/
components
/
Analytics
/
components
/
layout
/
date-range
/
MonthScroller.jsx
MonthScroller.jsx in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.23, at backend/views/components/Analytics/components/layout/date-range/MonthScroller.jsx
| 1 | import { cn } from '@/lib/utils'; |
| 2 | import { useEffect, useRef } from 'react'; |
| 3 | |
| 4 | const CELL = 26; |
| 5 | const SCROLLER_VISIBLE = 5; |
| 6 | const MONTH_LABELS = [ |
| 7 | 'Jan', |
| 8 | 'Feb', |
| 9 | 'Mar', |
| 10 | 'Apr', |
| 11 | 'May', |
| 12 | 'Jun', |
| 13 | 'Jul', |
| 14 | 'Aug', |
| 15 | 'Sep', |
| 16 | 'Oct', |
| 17 | 'Nov', |
| 18 | 'Dec', |
| 19 | ]; |
| 20 | |
| 21 | export default function MonthScroller({ month, onMonthChange }) { |
| 22 | const scrollRef = useRef(null); |
| 23 | |
| 24 | const items = []; |
| 25 | for (let y = month.getFullYear() - 2; y <= month.getFullYear() + 2; y++) { |
| 26 | for (let m = 0; m < 12; m++) { |
| 27 | items.push({ year: y, month: m }); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | const currentIdx = items.findIndex( |
| 32 | (it) => it.year === month.getFullYear() && it.month === month.getMonth() |
| 33 | ); |
| 34 | |
| 35 | useEffect(() => { |
| 36 | if (scrollRef.current && currentIdx !== -1) { |
| 37 | scrollRef.current.scrollTop = |
| 38 | (currentIdx - Math.floor(SCROLLER_VISIBLE / 2)) * CELL; |
| 39 | } |
| 40 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 41 | }, [month.getFullYear(), month.getMonth()]); |
| 42 | |
| 43 | return ( |
| 44 | <div |
| 45 | ref={scrollRef} |
| 46 | className="disco:shrink-0 disco:overflow-y-auto" |
| 47 | style={{ |
| 48 | width: 28, |
| 49 | height: CELL * SCROLLER_VISIBLE, |
| 50 | scrollbarWidth: 'none', |
| 51 | msOverflowStyle: 'none', |
| 52 | }} |
| 53 | > |
| 54 | {items.map((it, i) => { |
| 55 | const isCurrent = |
| 56 | it.year === month.getFullYear() && |
| 57 | it.month === month.getMonth(); |
| 58 | return ( |
| 59 | <div |
| 60 | key={i} |
| 61 | onClick={() => |
| 62 | onMonthChange(new Date(it.year, it.month, 1)) |
| 63 | } |
| 64 | style={{ height: CELL }} |
| 65 | className={cn( |
| 66 | 'disco:flex disco:cursor-pointer disco:items-center disco:justify-center disco:rounded disco:text-[9px] disco:transition-colors', |
| 67 | isCurrent |
| 68 | ? 'disco:font-bold disco:text-[rgba(74,74,74,0.9)]' |
| 69 | : 'disco:text-[rgba(74,74,74,0.3)] disco:hover:text-[rgba(74,74,74,0.7)]' |
| 70 | )} |
| 71 | > |
| 72 | {MONTH_LABELS[it.month]} |
| 73 | </div> |
| 74 | ); |
| 75 | })} |
| 76 | </div> |
| 77 | ); |
| 78 | } |
| 79 |