From 97b9ea27e7b452a1113b5f2bc00c65e67d2441d4 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 6 Jun 2023 06:11:16 +0800 Subject: [PATCH] feat: hide post side actions if footer actions showing (#605) --- src/components/site/PostActions.tsx | 9 ++- src/components/site/PostFooter.tsx | 25 +++++++++ src/components/ui/MarkdownHeading.tsx | 80 --------------------------- 3 files changed, 33 insertions(+), 81 deletions(-) delete mode 100644 src/components/ui/MarkdownHeading.tsx diff --git a/src/components/site/PostActions.tsx b/src/components/site/PostActions.tsx index d391b0ee..98f42040 100644 --- a/src/components/site/PostActions.tsx +++ b/src/components/site/PostActions.tsx @@ -1,3 +1,5 @@ +"use client" + import React, { useEffect, useRef, useState } from "react" import { ReactionComment } from "~/components/common/ReactionComment" @@ -6,6 +8,8 @@ import { ReactionMint } from "~/components/common/ReactionMint" import { ReactionTip } from "~/components/common/ReactionTip" import { ExpandedCharacter, ExpandedNote } from "~/lib/types" +import { usePostFooterInView } from "./PostFooter" + export const PostActions: React.FC<{ page?: ExpandedNote site?: ExpandedCharacter @@ -23,12 +27,15 @@ export const PostActions: React.FC<{ ) } }, [containerRef]) + const isPostFooterInView = usePostFooterInView() return (
60 ? "block" : "none", + opacity: isPostFooterInView ? 0 : 1, + visibility: isPostFooterInView ? "hidden" : "visible", }} ref={containerRef} > diff --git a/src/components/site/PostFooter.tsx b/src/components/site/PostFooter.tsx index 01da75f6..c546952c 100644 --- a/src/components/site/PostFooter.tsx +++ b/src/components/site/PostFooter.tsx @@ -1,16 +1,41 @@ +"use client" + +import { useInViewport } from "ahooks" +import { useEffect, useRef } from "react" + +import { useQuery, useQueryClient } from "@tanstack/react-query" + import { Comment } from "~/components/common/Comment" import { ReactionLike } from "~/components/common/ReactionLike" import { ReactionMint } from "~/components/common/ReactionMint" import { ReactionTip } from "~/components/common/ReactionTip" import { ExpandedCharacter, ExpandedNote } from "~/lib/types" +const key = ["PostFooterInView"] + +export const usePostFooterInView = () => { + const { data } = useQuery({ + queryKey: key, + enabled: false, + }) + return data +} export const PostFooter: React.FC<{ page?: ExpandedNote site?: ExpandedCharacter }> = ({ page, site }) => { + const actionElRef = useRef(null) + const [isInView] = useInViewport(actionElRef) + + const queryClient = useQueryClient() + useEffect(() => { + queryClient.setQueryData(key, () => isInView) + }, [isInView]) + return ( <>
diff --git a/src/components/ui/MarkdownHeading.tsx b/src/components/ui/MarkdownHeading.tsx deleted file mode 100644 index 9829b4b5..00000000 --- a/src/components/ui/MarkdownHeading.tsx +++ /dev/null @@ -1,80 +0,0 @@ -"use client" - -import { FC, PropsWithChildren, useEffect, useRef, useState } from "react" -import { create } from "zustand" - -import { useGetState } from "~/hooks/useGetState" - -const targetQueue = [] as string[] -interface AnchorStoreState { - currentTarget: string - setTarget: (target: string) => void - pop: () => void -} -const useAnchorStore = create((setState) => ({ - currentTarget: "", - - setTarget(target: string) { - if (target === targetQueue.at(-1)) return - const payload = { - currentTarget: target, - } - - targetQueue.push(target) - setState(payload) - }, - - pop() { - const prevTarget = targetQueue.pop() - if (prevTarget) setState({ currentTarget: prevTarget }) - }, -})) - -export const HeadingAnchor: FC> = ( - props, -) => { - const getName = useGetState(props.name) - - const ref = useRef(null) - - const [inViewport, setInViewport] = useState(false) - useEffect(() => { - const $el = ref.current - if (!$el) return - - const options = { - rootMargin: "-33% 0% -33% 0%", - } - - const observer = new IntersectionObserver(function (entries, observer) { - entries.forEach((entry) => { - setInViewport(entry.isIntersecting) - }) - }, options) - - observer.observe($el) - - return () => { - observer.unobserve($el) - observer.disconnect() - } - }, []) - - const isTriggered = useRef(false) - - useEffect(() => { - const store = useAnchorStore.getState() - console.log(inViewport, "inViewport", getName()) - if (inViewport) { - store.setTarget(getName()) - isTriggered.current = true - } else { - if (isTriggered.current) { - store.pop() - } - isTriggered.current = false - } - }, [inViewport]) - - return -}