npm-pkg-hook 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +13 -13
- package/.env +1 -0
- package/.eslintrc.js +132 -0
- package/.eslintrc.json +107 -107
- package/.github/pull_request_template.md +17 -17
- package/.github/workflows/pepeline.yaml +29 -29
- package/.vscode/extensions.json +6 -0
- package/.vscode/settings.json +12 -0
- package/README.md +1 -1
- package/jsconfig.json +27 -27
- package/next.config.js +128 -128
- package/package.json +8 -6
- package/script.txt +7 -0
- package/src/cookies/index.ts +3 -3
- package/src/hooks/index.js +46 -19
- package/src/hooks/updateExtProductFoodsOptional/index.js +38 -0
- package/src/hooks/useAcumulateDate/index.js +16 -16
- package/src/hooks/useAnimationText/index.jsx +31 -30
- package/src/hooks/useBanner/index.js +19 -0
- package/src/hooks/useCatWithProduct/index.js +42 -0
- package/src/hooks/useCatWithProduct/queries.js +172 -0
- package/src/hooks/useCategoryInStore/index.js +94 -0
- package/src/hooks/{useGetCategorieStore → useCategoryInStore}/queries.js +77 -77
- package/src/hooks/useCategoryStore/index.js +7 -7
- package/src/hooks/useCategoryStore/queries.js +16 -16
- package/src/hooks/useChartData/index.js +168 -0
- package/src/hooks/useCheckbox/index.js +114 -114
- package/src/hooks/useClients/index.js +13 -13
- package/src/hooks/useClients/queries.js +117 -117
- package/src/hooks/useConnection/index.js +23 -0
- package/src/hooks/useCreateProduct/helpers/useEditImageProduct/index.js +165 -0
- package/src/hooks/useCreateProduct/index.js +268 -0
- package/src/hooks/useDessert/index.js +141 -0
- package/src/hooks/useDrag/index.js +62 -57
- package/src/hooks/useEvent/index.js +33 -33
- package/src/hooks/useFetchJson/index.js +24 -24
- package/src/hooks/useFetchMoreInteractions/index.jsx +37 -34
- package/src/hooks/useFormTools/index.js +83 -70
- package/src/hooks/useFormatDate/index.js +34 -0
- package/src/hooks/useFullScreenMode/index.js +65 -65
- package/src/hooks/useHover/index.js +28 -28
- package/src/hooks/useImageOptimization/index.js +28 -0
- package/src/hooks/useImageWeight/index.js +52 -0
- package/src/hooks/useImagesStore/index.js +171 -0
- package/src/hooks/useImagesStore/queries.js +216 -0
- package/src/hooks/useInnerHtml/index.js +37 -37
- package/src/hooks/useIntersection/index.js +84 -31
- package/src/hooks/useKeypress/index.js +27 -27
- package/src/hooks/useLazyScript/index.js +72 -0
- package/src/hooks/useLocalSorage/index.js +35 -35
- package/src/hooks/useLocationNavigate/index.js +54 -54
- package/src/hooks/useMobile/index.js +37 -37
- package/src/hooks/useMutateHeight/index.js +37 -0
- package/src/hooks/useProductsFood/index.js +190 -0
- package/src/hooks/{useGetProductsFood → useProductsFood}/queriesStore.js +780 -765
- package/src/hooks/useProductsFood/usetagsProducts.js +57 -0
- package/src/hooks/useReport/index.js +35 -0
- package/src/hooks/useReport/queries.js +122 -0
- package/src/hooks/useRestaurant/index.js +19 -19
- package/src/hooks/useRestaurant/queries.js +79 -69
- package/src/hooks/useSales/index.js +589 -489
- package/src/hooks/useSales/queries.js +290 -229
- package/src/hooks/useSales/useGetSale.js +12 -0
- package/src/hooks/useSales/useTotalSales.js +17 -0
- package/src/hooks/useSaveAvailableProduct/helpers/index.js +30 -0
- package/src/hooks/useSaveAvailableProduct/index.js +26 -0
- package/src/hooks/useSaveAvailableProduct/queries.js +10 -0
- package/src/hooks/useSchedule/index.jsx +23 -0
- package/src/hooks/useSetState/index.js +24 -24
- package/src/hooks/useStore/index.js +17 -17
- package/src/hooks/useStore/queries.js +135 -135
- package/src/hooks/useStoreCalendar/index.js +7 -0
- package/src/hooks/useTimeAgo/useTimeAgo.js +39 -39
- package/src/hooks/useUpdateCart/index.js +124 -124
- package/src/hooks/useUpdateExtProductFoodsSubOptional/index.js +37 -0
- package/src/hooks/useUser/index.js +7 -3
- package/src/hooks/useUser/queries.js +69 -0
- package/src/hooks/useWindowSize/index.js +37 -37
- package/src/index.jsx +2 -1
- package/src/mock/dessert/index.js +16 -0
- package/src/mock/index.js +2 -0
- package/src/utils/index.js +106 -54
- package/src/hooks/useGetCategorieStore/index.js +0 -21
- package/src/hooks/useGetProductsFood/index.js +0 -46
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
import React, {
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
1
|
+
import React, {
|
|
2
|
+
useEffect,
|
|
3
|
+
useRef, useState
|
|
4
|
+
} from 'react'
|
|
5
|
+
|
|
6
|
+
export const useFetchMoreInteractions = ({
|
|
7
|
+
render,
|
|
8
|
+
fetchMore = true,
|
|
9
|
+
callback = () => {}
|
|
10
|
+
}) => {
|
|
11
|
+
const loadingRef = useRef()
|
|
12
|
+
|
|
13
|
+
const useOnScreen = ref => {
|
|
14
|
+
const [isIntersecting, setIsIntersecting] = useState(false)
|
|
15
|
+
|
|
16
|
+
const observer = new IntersectionObserver(
|
|
17
|
+
([entry]) => setIsIntersecting(entry.isIntersecting)
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (ref.current) observer.observe(ref.current)
|
|
22
|
+
// Remove the observer as soon as the component is unmounted
|
|
23
|
+
return () => { observer.disconnect() }
|
|
24
|
+
}, [ref, observer])
|
|
25
|
+
|
|
26
|
+
return isIntersecting
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const isVisible = useOnScreen(loadingRef)
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (isVisible && fetchMore) callback()
|
|
33
|
+
}, [isVisible, callback, fetchMore])
|
|
34
|
+
|
|
35
|
+
return <div ref={loadingRef}>
|
|
36
|
+
{isVisible && fetchMore ? render || <div style={{ background: 'red' }} >Loading...{isVisible && 'lol'}</div> : <></>}
|
|
37
|
+
</div>
|
|
35
38
|
}
|
|
@@ -1,71 +1,84 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useCallback,
|
|
3
|
-
useEffect,
|
|
4
|
-
useState
|
|
5
|
-
|
|
6
|
-
import { validationSubmitHooks } from '../../utils'
|
|
7
|
-
/**
|
|
8
|
-
* @version 0.0.1
|
|
9
|
-
* @description Hook con herramientas de validación y eventos de cambio
|
|
10
|
-
* @return {Array} devuelve la función onChange a ejecutar y el estado de error de cada input
|
|
11
|
-
*/
|
|
12
|
-
export const useFormTools = () => {
|
|
13
|
-
const [dataForm, setDataForm] = useState({})
|
|
14
|
-
const [errorForm, setErrorForm] = useState({})
|
|
15
|
-
const [errorSubmit, setErrorSubmit] = useState(false)
|
|
16
|
-
const [calledSubmit, setCalledSubmit] = useState(false)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useState
|
|
5
|
+
} from 'react'
|
|
6
|
+
import { validationSubmitHooks } from '../../utils'
|
|
7
|
+
/**
|
|
8
|
+
* @version 0.0.1
|
|
9
|
+
* @description Hook con herramientas de validación y eventos de cambio
|
|
10
|
+
* @return {Array} devuelve la función onChange a ejecutar y el estado de error de cada input
|
|
11
|
+
*/
|
|
12
|
+
export const useFormTools = () => {
|
|
13
|
+
const [dataForm, setDataForm] = useState({})
|
|
14
|
+
const [errorForm, setErrorForm] = useState({})
|
|
15
|
+
const [errorSubmit, setErrorSubmit] = useState(false)
|
|
16
|
+
const [calledSubmit, setCalledSubmit] = useState(false)
|
|
17
|
+
// Handle Change
|
|
18
|
+
const handleChange = useCallback((e, error) => {
|
|
19
|
+
setDataForm({ ...dataForm, [e.target.name]: e.target.value })
|
|
20
|
+
setErrorForm({ ...errorForm, [e.target.name]: error })
|
|
21
|
+
}, [setDataForm, dataForm, errorForm, setErrorForm])
|
|
22
|
+
|
|
23
|
+
// Forzar datos desde una ventana externa
|
|
24
|
+
const handleForcedData = useCallback(data => {
|
|
25
|
+
setDataForm(data)
|
|
26
|
+
}, [setDataForm])
|
|
27
|
+
|
|
28
|
+
// Forzar datos de error desde una ventana externa
|
|
29
|
+
const setForcedError = useCallback(errors => {
|
|
30
|
+
setErrorForm(errors)
|
|
31
|
+
}, [setErrorForm])
|
|
32
|
+
|
|
33
|
+
// Handle submit, al enviar formulario
|
|
34
|
+
const listErrors = Object.values(errorForm)
|
|
35
|
+
const errors = listErrors.find((error) => {
|
|
36
|
+
return error ===true
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const handleSubmit = useCallback(({ event, action, msgSuccess, msgError, actionAfterSuccess }) => {
|
|
40
|
+
!!event && event.preventDefault()
|
|
41
|
+
setCalledSubmit(true)
|
|
42
|
+
let errSub = false
|
|
43
|
+
|
|
44
|
+
// Valida los errores locales
|
|
45
|
+
for (const x in errorForm) {
|
|
46
|
+
if (errorForm[x]) errSub = true
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (errors) {
|
|
50
|
+
setErrorSubmit(true)
|
|
51
|
+
return setForcedError({ ...errorForm })
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (errSub) return setErrorSubmit(errSub)
|
|
55
|
+
|
|
56
|
+
// Valida los errores desde el evento
|
|
57
|
+
const errores = validationSubmitHooks(event.target.elements)
|
|
58
|
+
setErrorForm(errores)
|
|
59
|
+
console.log("🚀 ~ file: index.js ~ line 50 ~ handleSubmit ~ errores", errores)
|
|
60
|
+
for (const x in errores) {
|
|
61
|
+
if (errores[x]) errSub = true
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Ejecuta la petición si es válido
|
|
65
|
+
if (!errSub && action) {
|
|
66
|
+
action().then(res => {
|
|
67
|
+
if (res) {
|
|
68
|
+
// setAlertBox({ message: msgSuccess || 'Operación exitosa', color: PColor })
|
|
69
|
+
!!actionAfterSuccess && actionAfterSuccess()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
}).catch(e => {return console.log({ message: msgError || e?.message || 'Ha ocurrido un error', color: WColor })})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setErrorSubmit(errSub)
|
|
76
|
+
}, [errorForm, setErrorForm])
|
|
77
|
+
|
|
78
|
+
useEffect(() => {return setCalledSubmit(false)}, [calledSubmit])
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
return setCalledSubmit(false)},
|
|
81
|
+
[])
|
|
82
|
+
|
|
83
|
+
return [handleChange, handleSubmit, handleForcedData, { dataForm, errorForm, errorSubmit, calledSubmit, setForcedError }]
|
|
71
84
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const useFormatDate = ({
|
|
2
|
+
date,
|
|
3
|
+
local = 'ES'
|
|
4
|
+
}) => {
|
|
5
|
+
const dateToFormat = new Date(date ?? Date.now())
|
|
6
|
+
const fullDate = dateToFormat.toLocaleDateString(local, { year: 'numeric', month: '2-digit', day: '2-digit' })
|
|
7
|
+
const day = fullDate.split('/')[0]
|
|
8
|
+
const month = fullDate.split('/')[1]
|
|
9
|
+
const year = fullDate.split('/')[2]
|
|
10
|
+
const yearMonthDay = dateToFormat.toLocaleDateString('en-CA')
|
|
11
|
+
const numberDay = dateToFormat.getDay()
|
|
12
|
+
const shortDayName = dateToFormat.toLocaleDateString(local, { weekday: 'short' })
|
|
13
|
+
const longDayName = dateToFormat.toLocaleDateString(local, { weekday: 'long' })
|
|
14
|
+
const hourMinutes12 = dateToFormat.toLocaleTimeString('ES-CO', { hour: '2-digit', minute: '2-digit' })
|
|
15
|
+
const hourMinutes24 = dateToFormat.toLocaleTimeString('ES-CO', { hour: '2-digit', minute: '2-digit', hour12: false })
|
|
16
|
+
const handleHourPmAM = (hour) => {
|
|
17
|
+
const hourPmAm = new Date(`1/1/1999 ${hour}`).toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', hour12: true })
|
|
18
|
+
return hour ? hourPmAm : ''
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
day,
|
|
22
|
+
fullDate,
|
|
23
|
+
hourMinutes12,
|
|
24
|
+
numberDay,
|
|
25
|
+
yearMonthDay,
|
|
26
|
+
hourMinutes24,
|
|
27
|
+
longDayName,
|
|
28
|
+
shortDayName,
|
|
29
|
+
month,
|
|
30
|
+
year,
|
|
31
|
+
handleHourPmAM,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import React, { useState, useRef, useEffect } from 'react'
|
|
2
|
-
import styled from 'styled-components'
|
|
3
|
-
|
|
4
|
-
const useFullscreenMode = () => {
|
|
5
|
-
const [isFullscreen, setFullscreen] = useState(false)
|
|
6
|
-
const elementRef = useRef()
|
|
7
|
-
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
const changeHandler = () => {return setFullscreen(mode => {return !mode})}
|
|
10
|
-
|
|
11
|
-
document.addEventListener('fullscreenchange', changeHandler, false)
|
|
12
|
-
document.addEventListener('mozfullscreenchange', changeHandler, false)
|
|
13
|
-
document.addEventListener('MSFullScreenChange', changeHandler, false)
|
|
14
|
-
document.addEventListener(
|
|
15
|
-
'webkitfullscreenchange',
|
|
16
|
-
changeHandler,
|
|
17
|
-
false
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
return () => {
|
|
21
|
-
document.removeEventListener('fullscreenchange', changeHandler)
|
|
22
|
-
document.removeEventListener('mozfullscreenchange', changeHandler)
|
|
23
|
-
document.removeEventListener('MSFullScreenChange', changeHandler)
|
|
24
|
-
document.removeEventListener(
|
|
25
|
-
'webkitfullscreenchange',
|
|
26
|
-
changeHandler
|
|
27
|
-
)
|
|
28
|
-
}
|
|
29
|
-
}, [])
|
|
30
|
-
|
|
31
|
-
const goFullscreen = () => {
|
|
32
|
-
if (elementRef.current.requestFullscreen) {
|
|
33
|
-
elementRef.current.requestFullscreen()
|
|
34
|
-
} else if (elementRef.current.mozRequestFullscreen) {
|
|
35
|
-
//Firefox
|
|
36
|
-
elementRef.current.mozRequestFullscreen()
|
|
37
|
-
} else if (elementRef.current.webkitRequestFullscreen) {
|
|
38
|
-
//Chrome, safari, opera
|
|
39
|
-
elementRef.current.webkitRequestFullscreen()
|
|
40
|
-
} else if (elementRef.current.msRequestFullscreen) {
|
|
41
|
-
//IE, edge
|
|
42
|
-
elementRef.current.msRequestFullscreen()
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const exitFullScreen = () => {
|
|
47
|
-
if (document.exitFullscreen) {
|
|
48
|
-
document.exitFullscreen()
|
|
49
|
-
} else if (document.mozCancelFullScreen) {
|
|
50
|
-
document.mozCancelFullScreen()
|
|
51
|
-
} else if (document.webkitExitFullscreen) {
|
|
52
|
-
document.webkitExitFullscreen()
|
|
53
|
-
} else if (document.msExitFullscreen) {
|
|
54
|
-
document.msExitFullscreen()
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const ToggleIcon = (
|
|
59
|
-
<Button onDoubleClick={() => {return (!isFullscreen ? goFullscreen() : exitFullScreen())}}>{!isFullscreen ? 'FullScreen' : 'Normal'}</Button>
|
|
60
|
-
)
|
|
61
|
-
return [elementRef, ToggleIcon] //Icon, ref
|
|
62
|
-
}
|
|
63
|
-
const Button = styled.button`
|
|
64
|
-
|
|
65
|
-
`
|
|
1
|
+
import React, { useState, useRef, useEffect } from 'react'
|
|
2
|
+
import styled from 'styled-components'
|
|
3
|
+
|
|
4
|
+
const useFullscreenMode = () => {
|
|
5
|
+
const [isFullscreen, setFullscreen] = useState(false)
|
|
6
|
+
const elementRef = useRef()
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
const changeHandler = () => {return setFullscreen(mode => {return !mode})}
|
|
10
|
+
|
|
11
|
+
document.addEventListener('fullscreenchange', changeHandler, false)
|
|
12
|
+
document.addEventListener('mozfullscreenchange', changeHandler, false)
|
|
13
|
+
document.addEventListener('MSFullScreenChange', changeHandler, false)
|
|
14
|
+
document.addEventListener(
|
|
15
|
+
'webkitfullscreenchange',
|
|
16
|
+
changeHandler,
|
|
17
|
+
false
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
document.removeEventListener('fullscreenchange', changeHandler)
|
|
22
|
+
document.removeEventListener('mozfullscreenchange', changeHandler)
|
|
23
|
+
document.removeEventListener('MSFullScreenChange', changeHandler)
|
|
24
|
+
document.removeEventListener(
|
|
25
|
+
'webkitfullscreenchange',
|
|
26
|
+
changeHandler
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
}, [])
|
|
30
|
+
|
|
31
|
+
const goFullscreen = () => {
|
|
32
|
+
if (elementRef.current.requestFullscreen) {
|
|
33
|
+
elementRef.current.requestFullscreen()
|
|
34
|
+
} else if (elementRef.current.mozRequestFullscreen) {
|
|
35
|
+
//Firefox
|
|
36
|
+
elementRef.current.mozRequestFullscreen()
|
|
37
|
+
} else if (elementRef.current.webkitRequestFullscreen) {
|
|
38
|
+
//Chrome, safari, opera
|
|
39
|
+
elementRef.current.webkitRequestFullscreen()
|
|
40
|
+
} else if (elementRef.current.msRequestFullscreen) {
|
|
41
|
+
//IE, edge
|
|
42
|
+
elementRef.current.msRequestFullscreen()
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const exitFullScreen = () => {
|
|
47
|
+
if (document.exitFullscreen) {
|
|
48
|
+
document.exitFullscreen()
|
|
49
|
+
} else if (document.mozCancelFullScreen) {
|
|
50
|
+
document.mozCancelFullScreen()
|
|
51
|
+
} else if (document.webkitExitFullscreen) {
|
|
52
|
+
document.webkitExitFullscreen()
|
|
53
|
+
} else if (document.msExitFullscreen) {
|
|
54
|
+
document.msExitFullscreen()
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const ToggleIcon = (
|
|
59
|
+
<Button onDoubleClick={() => {return (!isFullscreen ? goFullscreen() : exitFullScreen())}}>{!isFullscreen ? 'FullScreen' : 'Normal'}</Button>
|
|
60
|
+
)
|
|
61
|
+
return [elementRef, ToggleIcon] //Icon, ref
|
|
62
|
+
}
|
|
63
|
+
const Button = styled.button`
|
|
64
|
+
|
|
65
|
+
`
|
|
66
66
|
export default useFullscreenMode
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { useRef, useState, useEffect } from 'react'
|
|
2
|
-
|
|
3
|
-
export default function useHover() {
|
|
4
|
-
const [value, setValue] = useState(false)
|
|
5
|
-
|
|
6
|
-
const ref = useRef(null)
|
|
7
|
-
|
|
8
|
-
const handleMouseOver = () => {return setValue(true)}
|
|
9
|
-
const handleMouseOut = () => {return setValue(false)}
|
|
10
|
-
|
|
11
|
-
useEffect(
|
|
12
|
-
() => {
|
|
13
|
-
const node = ref.current
|
|
14
|
-
if (node) {
|
|
15
|
-
node.addEventListener('mouseover', handleMouseOver)
|
|
16
|
-
node.addEventListener('mouseout', handleMouseOut)
|
|
17
|
-
|
|
18
|
-
return () => {
|
|
19
|
-
node.removeEventListener('mouseover', handleMouseOver)
|
|
20
|
-
node.removeEventListener('mouseout', handleMouseOut)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return {}
|
|
24
|
-
},
|
|
25
|
-
[] // Recall only if ref changes
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
return [ref, value]
|
|
1
|
+
import { useRef, useState, useEffect } from 'react'
|
|
2
|
+
|
|
3
|
+
export default function useHover() {
|
|
4
|
+
const [value, setValue] = useState(false)
|
|
5
|
+
|
|
6
|
+
const ref = useRef(null)
|
|
7
|
+
|
|
8
|
+
const handleMouseOver = () => {return setValue(true)}
|
|
9
|
+
const handleMouseOut = () => {return setValue(false)}
|
|
10
|
+
|
|
11
|
+
useEffect(
|
|
12
|
+
() => {
|
|
13
|
+
const node = ref.current
|
|
14
|
+
if (node) {
|
|
15
|
+
node.addEventListener('mouseover', handleMouseOver)
|
|
16
|
+
node.addEventListener('mouseout', handleMouseOut)
|
|
17
|
+
|
|
18
|
+
return () => {
|
|
19
|
+
node.removeEventListener('mouseover', handleMouseOver)
|
|
20
|
+
node.removeEventListener('mouseout', handleMouseOut)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return {}
|
|
24
|
+
},
|
|
25
|
+
[] // Recall only if ref changes
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
return [ref, value]
|
|
29
29
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
// use it
|
|
3
|
+
// const imageUrl = 'https://example.com/image.jpg';
|
|
4
|
+
// const optimizedUrl = useImageOptimization(imageUrl);
|
|
5
|
+
|
|
6
|
+
export function useImageOptimization(imageUrl) {
|
|
7
|
+
const [optimizedUrl, setOptimizedUrl] = useState(null);
|
|
8
|
+
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const image = new Image();
|
|
11
|
+
image.src = imageUrl;
|
|
12
|
+
|
|
13
|
+
image.onload = () => {
|
|
14
|
+
const canvas = document.createElement('canvas');
|
|
15
|
+
const ctx = canvas.getContext('2d');
|
|
16
|
+
|
|
17
|
+
canvas.width = image.width;
|
|
18
|
+
canvas.height = image.height;
|
|
19
|
+
|
|
20
|
+
ctx.drawImage(image, 0, 0, image.width, image.height);
|
|
21
|
+
|
|
22
|
+
const optimizedUrl = canvas.toDataURL('image/jpeg', 0.8);
|
|
23
|
+
setOptimizedUrl(optimizedUrl);
|
|
24
|
+
};
|
|
25
|
+
}, [imageUrl]);
|
|
26
|
+
|
|
27
|
+
return optimizedUrl;
|
|
28
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useImageWeight(imageUrl) {
|
|
4
|
+
const [weight, setWeight] = useState(null);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const image = new Image();
|
|
8
|
+
image.src = imageUrl;
|
|
9
|
+
|
|
10
|
+
image.onload = () => {
|
|
11
|
+
const xhr = new XMLHttpRequest();
|
|
12
|
+
xhr.open('HEAD', imageUrl, true);
|
|
13
|
+
xhr.onreadystatechange = () => {
|
|
14
|
+
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
15
|
+
const contentLength = xhr.getResponseHeader('Content-Length');
|
|
16
|
+
setWeight(contentLength);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
xhr.send();
|
|
20
|
+
};
|
|
21
|
+
}, [imageUrl]);
|
|
22
|
+
|
|
23
|
+
return weight;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// I use
|
|
27
|
+
// function MyComponent() {
|
|
28
|
+
// const imageUrl = 'https://example.com/image.jpg';
|
|
29
|
+
// const weight = useImageWeight(imageUrl);
|
|
30
|
+
|
|
31
|
+
// return (
|
|
32
|
+
// <div>
|
|
33
|
+
// <img src={imageUrl} alt="My Image" />
|
|
34
|
+
// {weight ? `Peso de imagen: ${weight} bytes` : 'Cargando...'}
|
|
35
|
+
// </div>
|
|
36
|
+
// );
|
|
37
|
+
// }
|
|
38
|
+
|
|
39
|
+
// const weightInMB = weight / (1024 * 1024);
|
|
40
|
+
|
|
41
|
+
// function MyComponent() {
|
|
42
|
+
// const imageUrl = 'https://example.com/image.jpg';
|
|
43
|
+
// const weight = useImageWeight(imageUrl);
|
|
44
|
+
|
|
45
|
+
// return (
|
|
46
|
+
// <div>
|
|
47
|
+
// <img src={imageUrl} alt="My Image" />
|
|
48
|
+
// {weight ? `Peso de imagen: ${(weight / (1024 * 1024)).toFixed(2)} MB` : 'Cargando...'}
|
|
49
|
+
// </div>
|
|
50
|
+
// );
|
|
51
|
+
// }
|
|
52
|
+
|