parent
0c28b9e3af
commit
ef9877a312
@ -0,0 +1,10 @@
|
||||
export { default as useUserInfo } from './useUserInfo';
|
||||
export { default as useVerify } from './useVerify';
|
||||
export { default as useDisclosure } from './useDisclosure';
|
||||
export { default as useMediaQuery } from './useMediaQuery';
|
||||
export { default as useChangeRoute } from './useChangeRoute';
|
||||
export { default as useUrlQuery } from './useUrlQuery';
|
||||
export { default as useDebounce } from './useDebounce';
|
||||
export { default as useTitle } from './useTitle';
|
||||
|
||||
export * from './useRedux';
|
@ -0,0 +1,22 @@
|
||||
import { useRouter, usePathname, useSearchParams } from 'next/navigation';
|
||||
|
||||
export default function useChangeRoute() {
|
||||
const { replace } = useRouter();
|
||||
const pathname = usePathname();
|
||||
const searchParams = useSearchParams();
|
||||
let query = {};
|
||||
searchParams.forEach((value, key) => {
|
||||
query[key] = value;
|
||||
});
|
||||
|
||||
const changeRoute = (newQueries) => {
|
||||
const queryParams = new URLSearchParams();
|
||||
Object.entries({ ...query, ...newQueries }).forEach(([key, value]) => {
|
||||
if (value) queryParams.set(key, value);
|
||||
});
|
||||
|
||||
replace(`${pathname}?${queryParams.toString()}`, { scroll: false });
|
||||
};
|
||||
|
||||
return changeRoute;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
function useDebounce(value, delay) {
|
||||
const [debouncedValue, setDebouncedValue] = useState(value);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
||||
|
||||
return () => {
|
||||
clearTimeout(timer);
|
||||
};
|
||||
}, [value, delay]);
|
||||
|
||||
return debouncedValue;
|
||||
}
|
||||
|
||||
export default useDebounce;
|
@ -0,0 +1,20 @@
|
||||
'use client';
|
||||
import { useState } from 'react';
|
||||
|
||||
export default function useDisclosure() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
|
||||
const open = () => {
|
||||
if (!isOpen) setIsOpen(true);
|
||||
};
|
||||
|
||||
const close = () => {
|
||||
if (isOpen) setIsOpen(false);
|
||||
};
|
||||
|
||||
const toggle = () => {
|
||||
isOpen ? close() : open();
|
||||
};
|
||||
|
||||
return [isOpen, { open, close, toggle }];
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
const useMediaQuery = (query) => {
|
||||
const [matches, setMatches] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const media = window.matchMedia(query);
|
||||
if (media.matches !== matches) {
|
||||
setMatches(media.matches);
|
||||
}
|
||||
const listener = () => setMatches(media.matches);
|
||||
window.addEventListener('resize', listener);
|
||||
return () => window.removeEventListener('resize', listener);
|
||||
}, [matches, query]);
|
||||
|
||||
return matches;
|
||||
};
|
||||
|
||||
export default useMediaQuery;
|
@ -0,0 +1,4 @@
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
|
||||
export const useAppDispatch = () => useDispatch();
|
||||
export const useAppSelector = useSelector;
|
@ -0,0 +1,10 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
export default function useTitle(initialTitle) {
|
||||
const [title, setTitle] = useState(initialTitle);
|
||||
const updateTitle = () => {
|
||||
document.title = title;
|
||||
};
|
||||
useEffect(updateTitle, [title]);
|
||||
return setTitle;
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
import { Hosting } from '@/utils/ua';
|
||||
|
||||
export default function useUA() {
|
||||
const [inWX, setInWX] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
setInWX(Hosting.isWX(window?.navigator?.userAgent?.toLowerCase()));
|
||||
}, []);
|
||||
|
||||
return { inWX };
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
import { useSearchParams } from 'next/navigation';
|
||||
|
||||
export default function useUrlQuery() {
|
||||
const searchParams = useSearchParams();
|
||||
let query = {};
|
||||
searchParams.forEach((value, key) => {
|
||||
query[key] = value;
|
||||
});
|
||||
|
||||
return query;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
import { useGetUserInfoQuery } from '@/store/services';
|
||||
import useVerify from './useVerify';
|
||||
import { useAppDispatch } from './useRedux';
|
||||
import { userLogin } from '@/store';
|
||||
|
||||
export default function useUserInfo() {
|
||||
const dispatch = useAppDispatch();
|
||||
const isVerify = useVerify();
|
||||
|
||||
const { data, isLoading, error, isError } = useGetUserInfoQuery(undefined, {
|
||||
skip: !isVerify,
|
||||
});
|
||||
|
||||
if (isError) dispatch(userLogin(''));
|
||||
|
||||
return { userInfo: data?.data, isVerify, isLoading, error, isError };
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
import { useSelector } from 'react-redux';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
export default function useVerify() {
|
||||
const { token } = useSelector((state) => state.user);
|
||||
let status;
|
||||
|
||||
if (!token) return false;
|
||||
|
||||
jwt.verify(token, process.env.NEXT_PUBLIC_ACCESS_TOKEN_SECRET, (err, decoded) => {
|
||||
if (err) status = false;
|
||||
if (decoded) status = true;
|
||||
});
|
||||
|
||||
if (status) return true;
|
||||
else return false;
|
||||
}
|
Loading…
Reference in new issue