(initProps?: Partial<TimezoneBuddyProps>)
| 37 | * from the props passed in to the component. |
| 38 | */ |
| 39 | export const createTimezoneBuddyStore = (initProps?: Partial<TimezoneBuddyProps>) => { |
| 40 | const DEFAULT_PROPS: TimezoneBuddyProps = { |
| 41 | timeMode: "24h", |
| 42 | browsingDate: new Date(), |
| 43 | x: 0, |
| 44 | y: 0, |
| 45 | height: 0, |
| 46 | }; |
| 47 | |
| 48 | return createStore<TimezoneBuddyState>()((set, get) => ({ |
| 49 | ...DEFAULT_PROPS, |
| 50 | ...initProps, |
| 51 | addToDate: (amount?: number) => { |
| 52 | const date = get().browsingDate; |
| 53 | date.setDate(date.getDate() + (amount || 1)); |
| 54 | set({ browsingDate: date }); |
| 55 | }, |
| 56 | subtractFromDate: (amount?: number) => { |
| 57 | const date = get().browsingDate; |
| 58 | date.setDate(date.getDate() - (amount || 1)); |
| 59 | set({ browsingDate: date }); |
| 60 | }, |
| 61 | setBrowseDate: (date: Date) => { |
| 62 | set({ browsingDate: date }); |
| 63 | }, |
| 64 | setContainerRef: (ref: React.RefObject<HTMLElement>) => { |
| 65 | set({ containerRef: ref }); |
| 66 | }, |
| 67 | emitCellPosition: (x: number) => { |
| 68 | const container = get().containerRef?.current; |
| 69 | if (x < 0) { |
| 70 | // If x is less than 0, we are outside the container |
| 71 | set({ isHover: false }); |
| 72 | } else if (container) { |
| 73 | const containerRect = container.getBoundingClientRect(); |
| 74 | set({ x: x - containerRect.left, isHover: true }); |
| 75 | } |
| 76 | }, |
| 77 | updateDimensions: () => { |
| 78 | const container = get().containerRef?.current; |
| 79 | let x = get().x; |
| 80 | if (container) { |
| 81 | const containerRect = container.getBoundingClientRect(); |
| 82 | const timeDials = container.querySelectorAll("[data-time-dial]>div"); |
| 83 | const height = |
| 84 | timeDials[timeDials.length - 1]?.getBoundingClientRect().bottom - |
| 85 | timeDials[0]?.getBoundingClientRect().top; |
| 86 | const y = timeDials[0]?.getBoundingClientRect().top - containerRect.top; |
| 87 | x = x ? x : timeDials[0]?.getBoundingClientRect().left - containerRect.left; |
| 88 | set({ height, y, x }); |
| 89 | } |
| 90 | }, |
| 91 | })); |
| 92 | }; |
| 93 | |
| 94 | export const TBContext = createContext<TimezoneBuddyStore | null>(null); |
no test coverage detected