-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooks.ts
30 lines (26 loc) · 1.02 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) 2023 Alteryx, Inc. All rights reserved.
import { useEffect, useState } from 'react';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';
import type { RootState, AppDispatch } from './store/store';
import { TImgRatio } from './types';
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export const useCurrentImg = (): TImgRatio => {
const [imgRect, setImgRect] = useState({ height: 0, width: 0 });
useEffect(() => {
const resizeListener = () => {
let newImgRect = { height: 0, width: 0 };
const annoImg = document.getElementById('anno-img')
if (annoImg) {
const { height, width } = annoImg.getBoundingClientRect();
newImgRect = { height, width };
}
setImgRect(newImgRect);
};
window.addEventListener('resize', resizeListener);
return () => {
window.removeEventListener('resize', resizeListener);
};
}, []);
return imgRect;
};