Skip to content

Commit

Permalink
fix: separate business-logic from SubNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Bagautdinov committed Jul 16, 2024
1 parent f3c024a commit 8a79d8f
Showing 1 changed file with 45 additions and 33 deletions.
78 changes: 45 additions & 33 deletions src/components/SubNavigation/SubNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,11 @@ export type ShareData = {
url?: string;
};

export interface SubNavigationProps {
title: string | undefined;
hideMiniToc: boolean;
miniTocOpened: boolean;
toggleMiniTocOpen: () => void;
closeMiniToc: () => void;
}

export const SubNavigation = ({
title,
hideMiniToc,
miniTocOpened,
toggleMiniTocOpen,
closeMiniToc,
}: SubNavigationProps) => {
const useVisibility = (miniTocOpened: boolean, closeMiniToc: () => void) => {
const [visibility, setVisibility] = useState(true);
const [hiddingTimeout, setHiddingTimeout] = useState<number | undefined>(undefined);
const [lastScrollY, setLastScrollY] = useState(window.screenY);

const shareData = useMemo(() => {
return {
title,
url: window.location.href,
};
}, [title]);

const clickOutsideMiniToc = useCallback(
(event: MouseEvent) => {
event.preventDefault();
Expand Down Expand Up @@ -102,24 +81,14 @@ export const SubNavigation = ({
setHiddingTimeout,
]);

const shareHandler = useCallback(() => {
if (navigator && navigator.share) {
navigator
.share(shareData)
.then(() => {})
.catch((error) => console.error('Error sharing', error));
} else {
console.log('Share not supported', shareData);
}
}, [shareData]);

useEffect(() => {
if (window.scrollY === 0) {
return;
}

setHiddingTimeout(
window.setTimeout(() => {
setLastScrollY(window.scrollY);
setHiddingTimeout(undefined);
}, 100),
);
Expand All @@ -141,6 +110,49 @@ export const SubNavigation = ({
};
}, [clickOutsideMiniToc]);

return visibility;
};

const useShareHandler = (title: string | undefined) => {
const shareData = useMemo(() => {
return {
title,
url: window.location.href,
};
}, [title]);

const shareHandler = useCallback(() => {
if (navigator && navigator.share) {
navigator
.share(shareData)
.then(() => {})
.catch((error) => console.error('Error sharing', error));

Check warning on line 129 in src/components/SubNavigation/SubNavigation.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
} else {
console.log('Share not supported', shareData);

Check warning on line 131 in src/components/SubNavigation/SubNavigation.tsx

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected console statement
}
}, [shareData]);

return shareHandler;
};

export interface SubNavigationProps {
title: string | undefined;
hideMiniToc: boolean;
miniTocOpened: boolean;
toggleMiniTocOpen: () => void;
closeMiniToc: () => void;
}

export const SubNavigation = ({
title,
hideMiniToc,
miniTocOpened,
toggleMiniTocOpen,
closeMiniToc,
}: SubNavigationProps) => {
const visibility = useVisibility(miniTocOpened, closeMiniToc);
const shareHandler = useShareHandler(title);

return (
<div
className={b({
Expand Down

0 comments on commit 8a79d8f

Please sign in to comment.