Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

typescriptify hooks and hoc

Blue Mousef3698b868e02
diff --git a/src/hoc/protected.js b/src/hoc/protected.tsx
similarity index 54%
rename from src/hoc/protected.js
rename to src/hoc/protected.tsx
index 195f63bf3..0560ffd40 100644
--- a/src/hoc/protected.js
+++ b/src/hoc/protected.tsx
@@ -1,20 +1,27 @@
import { useSelector } from 'react-redux'
import { Redirect } from 'react-router-dom'
+
+import { StateType } from 'redux/store'
import routes from 'routes'
+type AuthParamType = {
+ selector: (state: StateType) => boolean,
+ redirectPath: string
+}
+
export const auth = {
authenticated: {
- selector: state => state.auth.authenticated,
+ selector: (state: StateType) => state.auth.authenticated,
redirectPath: routes.signin,
},
notAuthenticated: {
- selector: state => !state.auth.authenticated,
+ selector: (state: StateType) => !state.auth.authenticated,
redirectPath: routes.dashboard,
},
}
-export const withAuthentication = (WrappedComponent, authParam) => {
- const WithAuthentication = (params) => {
+export const withAuthentication = (WrappedComponent: any, authParam: AuthParamType) => {
+ const WithAuthentication = (params: any) => {
const selector = useSelector(authParam.selector)
if (!selector) {
diff --git a/src/hooks/useOnClickOutside.js b/src/hooks/useOnClickOutside.ts
similarity index 60%
rename from src/hooks/useOnClickOutside.js
rename to src/hooks/useOnClickOutside.ts
index e825cfceb..021d2819d 100644
--- a/src/hooks/useOnClickOutside.js
+++ b/src/hooks/useOnClickOutside.ts
@@ -1,9 +1,9 @@
-import { useEffect } from 'react'
+import React, { useEffect } from 'react'
-const useOnClickOutside = (ref, handler) => {
+const useOnClickOutside = (ref: React.RefObject<HTMLDivElement>, handler: Function) => {
useEffect(() => {
- const func = (event) => {
- if (!ref.current || ref.current.contains(event.target)) {
+ const func = (event: MouseEvent | TouchEvent) => {
+ if (!ref.current || ref.current.contains(event.target as Node)) {
return
}
handler(event)
diff --git a/src/hooks/usePagination.jsx b/src/hooks/usePagination.tsx
similarity index 91%
rename from src/hooks/usePagination.jsx
rename to src/hooks/usePagination.tsx
index c1fe96e32..5e32a07a0 100644
--- a/src/hooks/usePagination.jsx
+++ b/src/hooks/usePagination.tsx
@@ -3,17 +3,17 @@ import { ENTRIES_PER_PAGE_DASHBOARD } from 'redux/constants'
export const DOTS = '...'
-const range = (start, end) => {
+const range = (start: number, end: number) => {
const length = end - start + 1
return Array.from({ length }, (_, idx) => idx + start)
}
export const usePagination = (
- totalCount,
- currentPage,
- siblingCount = 1,
- pageSize = ENTRIES_PER_PAGE_DASHBOARD,
-) => {
+ totalCount: number,
+ currentPage: number,
+ siblingCount: number = 1,
+ pageSize: number = ENTRIES_PER_PAGE_DASHBOARD,
+): any[] => {
const paginationRange = useMemo(() => {
const totalPageCount = Math.ceil(totalCount / pageSize)
diff --git a/src/hooks/useSize.jsx b/src/hooks/useSize.ts
similarity index 99%
rename from src/hooks/useSize.jsx
rename to src/hooks/useSize.ts
index 312a9a41c..98f201115 100644
--- a/src/hooks/useSize.jsx
+++ b/src/hooks/useSize.ts
@@ -7,6 +7,7 @@ export default () => {
width: 0, height: 0,
})
const ref = useRef(null)
+
useEffect(() => {
const DOMnode = ref.current
if (!DOMnode) {
@@ -23,5 +24,6 @@ export default () => {
resizeObserver.unobserve(DOMnode)
}
}, [])
+
return [ref, size]
}