mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
made transitional Anchor component for next/cra
This commit is contained in:
parent
7658068caa
commit
14b93d5dbb
7 changed files with 44 additions and 18 deletions
|
@ -12,6 +12,7 @@ import { useHistory } from 'react-router';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import { IFlowNode } from '~/types';
|
import { IFlowNode } from '~/types';
|
||||||
import { useWindowSize } from '~/hooks/dom/useWindowSize';
|
import { useWindowSize } from '~/hooks/dom/useWindowSize';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
SwiperCore.use([EffectFade, Lazy, Autoplay, Navigation]);
|
SwiperCore.use([EffectFade, Lazy, Autoplay, Navigation]);
|
||||||
|
|
||||||
|
@ -21,13 +22,13 @@ interface Props {
|
||||||
|
|
||||||
export const FlowSwiperHero: FC<Props> = ({ heroes }) => {
|
export const FlowSwiperHero: FC<Props> = ({ heroes }) => {
|
||||||
const { innerWidth } = useWindowSize();
|
const { innerWidth } = useWindowSize();
|
||||||
|
const { push } = useNavigation();
|
||||||
|
|
||||||
const [controlledSwiper, setControlledSwiper] = useState<SwiperClass | undefined>(undefined);
|
const [controlledSwiper, setControlledSwiper] = useState<SwiperClass | undefined>(undefined);
|
||||||
const [currentIndex, setCurrentIndex] = useState(heroes.length);
|
const [currentIndex, setCurrentIndex] = useState(heroes.length);
|
||||||
const preset = useMemo(() => (innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), [
|
const preset = useMemo(() => (innerWidth <= 768 ? PRESETS.cover : PRESETS.small_hero), [
|
||||||
innerWidth,
|
innerWidth,
|
||||||
]);
|
]);
|
||||||
const history = useHistory();
|
|
||||||
|
|
||||||
const onNext = useCallback(() => {
|
const onNext = useCallback(() => {
|
||||||
controlledSwiper?.slideNext(1);
|
controlledSwiper?.slideNext(1);
|
||||||
|
@ -63,9 +64,9 @@ export const FlowSwiperHero: FC<Props> = ({ heroes }) => {
|
||||||
|
|
||||||
const onClick = useCallback(
|
const onClick = useCallback(
|
||||||
(sw: SwiperClass) => {
|
(sw: SwiperClass) => {
|
||||||
history.push(URLS.NODE_URL(heroes[sw.realIndex]?.id));
|
push(URLS.NODE_URL(heroes[sw.realIndex]?.id));
|
||||||
},
|
},
|
||||||
[history, heroes]
|
[push, heroes]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!heroes.length) {
|
if (!heroes.length) {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { Grid } from '~/components/containers/Grid';
|
||||||
import { useHistory } from 'react-router';
|
import { useHistory } from 'react-router';
|
||||||
import { URLS } from '~/constants/urls';
|
import { URLS } from '~/constants/urls';
|
||||||
import { Placeholder } from '~/components/placeholders/Placeholder';
|
import { Placeholder } from '~/components/placeholders/Placeholder';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
node: INode;
|
node: INode;
|
||||||
|
@ -19,8 +20,8 @@ type Props = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const LabBottomPanel: FC<Props> = ({ node, hasNewComments, commentCount, isLoading }) => {
|
const LabBottomPanel: FC<Props> = ({ node, hasNewComments, commentCount, isLoading }) => {
|
||||||
const history = useHistory();
|
const { push } = useNavigation();
|
||||||
const onClick = useCallback(() => history.push(URLS.NODE_URL(node.id)), [history, node.id]);
|
const onClick = useCallback(() => push(URLS.NODE_URL(node.id)), [push, node.id]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Group horizontal className={styles.wrap} onClick={onClick}>
|
<Group horizontal className={styles.wrap} onClick={onClick}>
|
||||||
|
|
|
@ -7,6 +7,7 @@ import { INode } from '~/types';
|
||||||
import { getPrettyDate } from '~/utils/dom';
|
import { getPrettyDate } from '~/utils/dom';
|
||||||
import { URLS } from '~/constants/urls';
|
import { URLS } from '~/constants/urls';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
node?: Partial<INode>;
|
node?: Partial<INode>;
|
||||||
|
@ -14,10 +15,10 @@ interface IProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
const LabHero: FC<IProps> = ({ node, isLoading }) => {
|
const LabHero: FC<IProps> = ({ node, isLoading }) => {
|
||||||
const history = useHistory();
|
const { push } = useNavigation();
|
||||||
const onClick = useCallback(() => {
|
const onClick = useCallback(() => {
|
||||||
history.push(URLS.NODE_URL(node?.id));
|
push(URLS.NODE_URL(node?.id));
|
||||||
}, [history, node]);
|
}, [push, node]);
|
||||||
|
|
||||||
if (!node || isLoading) {
|
if (!node || isLoading) {
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -5,9 +5,9 @@ import { INode } from '~/types';
|
||||||
import { PRESETS, URLS } from '~/constants/urls';
|
import { PRESETS, URLS } from '~/constants/urls';
|
||||||
import { RouteComponentProps, withRouter } from 'react-router';
|
import { RouteComponentProps, withRouter } from 'react-router';
|
||||||
import { getURL, getURLFromString } from '~/utils/dom';
|
import { getURL, getURLFromString } from '~/utils/dom';
|
||||||
import { Avatar } from '~/components/common/Avatar';
|
|
||||||
import { useColorGradientFromString } from '~/hooks/color/useColorGradientFromString';
|
import { useColorGradientFromString } from '~/hooks/color/useColorGradientFromString';
|
||||||
import { Square } from '~/components/common/Square';
|
import { Square } from '~/components/common/Square';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
type IProps = RouteComponentProps & {
|
type IProps = RouteComponentProps & {
|
||||||
item: Partial<INode>;
|
item: Partial<INode>;
|
||||||
|
@ -29,11 +29,12 @@ const getTitleLetters = (title?: string): string => {
|
||||||
: words[0].substr(0, 2).toUpperCase();
|
: words[0].substr(0, 2).toUpperCase();
|
||||||
};
|
};
|
||||||
|
|
||||||
const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item, history }) => {
|
const NodeRelatedItemUnconnected: FC<IProps> = memo(({ item }) => {
|
||||||
|
const { push } = useNavigation();
|
||||||
const [is_loaded, setIsLoaded] = useState(false);
|
const [is_loaded, setIsLoaded] = useState(false);
|
||||||
const [width, setWidth] = useState(0);
|
const [width, setWidth] = useState(0);
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
const onClick = useCallback(() => history.push(URLS.NODE_URL(item.id)), [item, history]);
|
const onClick = useCallback(() => push(URLS.NODE_URL(item.id)), [item, push]);
|
||||||
|
|
||||||
const thumb = useMemo(
|
const thumb = useMemo(
|
||||||
() => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''),
|
() => (item.thumbnail ? getURL({ url: item.thumbnail }, PRESETS.avatar) : ''),
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useHistory } from 'react-router';
|
|
||||||
import { useModal } from '~/hooks/modal/useModal';
|
import { useModal } from '~/hooks/modal/useModal';
|
||||||
import { Dialog } from '~/constants/modal';
|
import { Dialog } from '~/constants/modal';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
/** redirects to the password redirect modal */
|
/** redirects to the password redirect modal */
|
||||||
export const useRestorePasswordRedirect = () => {
|
export const useRestorePasswordRedirect = () => {
|
||||||
const history = useHistory();
|
const { push } = useNavigation();
|
||||||
const { showModal } = useModal();
|
const { showModal } = useModal();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -15,7 +15,7 @@ export const useRestorePasswordRedirect = () => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
history.push('/');
|
push('/');
|
||||||
showModal(Dialog.RestorePassword, { code: match[1] });
|
showModal(Dialog.RestorePassword, { code: match[1] });
|
||||||
}, [showModal, history]);
|
}, [showModal, push]);
|
||||||
};
|
};
|
||||||
|
|
22
src/hooks/navigation/useNavigation.ts
Normal file
22
src/hooks/navigation/useNavigation.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
import { useCallback } from 'react';
|
||||||
|
import { CONFIG } from '~/utils/config';
|
||||||
|
import { useRouter } from 'next/router';
|
||||||
|
import { useHistory } from 'react-router';
|
||||||
|
|
||||||
|
export const useNavigation = () => {
|
||||||
|
const nextRouter = useRouter();
|
||||||
|
const craHistory = useHistory();
|
||||||
|
|
||||||
|
const push = useCallback(
|
||||||
|
(url: string) => {
|
||||||
|
if (CONFIG.isNextEnvironment) {
|
||||||
|
nextRouter.push(url);
|
||||||
|
} else {
|
||||||
|
craHistory.push(url);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[craHistory, nextRouter]
|
||||||
|
);
|
||||||
|
|
||||||
|
return { push };
|
||||||
|
};
|
|
@ -1,10 +1,10 @@
|
||||||
import { INode } from '~/types';
|
import { INode } from '~/types';
|
||||||
import { useHistory } from 'react-router';
|
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
import { URLS } from '~/constants/urls';
|
import { URLS } from '~/constants/urls';
|
||||||
|
import { useNavigation } from '~/hooks/navigation/useNavigation';
|
||||||
|
|
||||||
// useGotoNode returns fn, that navigates to node
|
// useGotoNode returns fn, that navigates to node
|
||||||
export const useGotoNode = (id: INode['id']) => {
|
export const useGotoNode = (id: INode['id']) => {
|
||||||
const history = useHistory();
|
const { push } = useNavigation();
|
||||||
return useCallback(() => history.push(URLS.NODE_URL(id)), [history, id]);
|
return useCallback(() => push(URLS.NODE_URL(id)), [push, id]);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue