From 3daa5c0a0963f2dd4c8facd8e309cc66952f69d7 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Tue, 8 Aug 2023 21:09:40 +0530 Subject: [PATCH] feat: add support for highlight.new (#1487) Co-authored-by: Brandon Roberts --- .../HighlightInput/highlight-input-form.tsx | 3 ++ middleware.ts | 10 +++- pages/feed/index.tsx | 47 ++++++++++++++++++- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/components/molecules/HighlightInput/highlight-input-form.tsx b/components/molecules/HighlightInput/highlight-input-form.tsx index a4f46c3dd8..8239e5e493 100644 --- a/components/molecules/HighlightInput/highlight-input-form.tsx +++ b/components/molecules/HighlightInput/highlight-input-form.tsx @@ -158,6 +158,7 @@ const HighlightInputForm = ({ refreshCallback }: HighlightInputFormProps): JSX.E className="flex-1 font-normal placeholder:text-sm focus:outline-none" type="text" placeholder={isDivFocused ? "Add title (optional)" : "Post a highlight to show your work!"} + id="highlight-create-input" /> @@ -264,6 +265,7 @@ const HighlightInputForm = ({ refreshCallback }: HighlightInputFormProps): JSX.E className="flex-1 focus:outline-none" type="text" placeholder={"Add title (optional)"} + id="highlight-create-input" /> setIsFormOpenMobile(true)} className="p-3 text-white rounded-full shadow-lg bg-light-orange-10" + id="mobile-highlight-create-button" > diff --git a/middleware.ts b/middleware.ts index b31db5ad9e..6f77043456 100644 --- a/middleware.ts +++ b/middleware.ts @@ -20,10 +20,16 @@ export async function middleware(req: NextRequest) { // Auth condition not met, redirect to home page. const redirectUrl = req.nextUrl.clone(); redirectUrl.pathname = "/feed"; + if (req.nextUrl.pathname === "/feed" && req.nextUrl.searchParams.has("new")) { + redirectUrl.searchParams.set("signIn", "true"); + } redirectUrl.searchParams.set("redirectedFrom", req.nextUrl.pathname); - return NextResponse.redirect(redirectUrl); + + if (!req.nextUrl.searchParams.has("redirectedFrom")) { + return NextResponse.redirect(redirectUrl); + } } export const config = { - matcher: ["/hub/insights/:path*", "/user/:path"], + matcher: ["/hub/insights/:path*", "/user/:path", "/feed/"], }; diff --git a/pages/feed/index.tsx b/pages/feed/index.tsx index 8889e8e9a8..b2a2c5d884 100644 --- a/pages/feed/index.tsx +++ b/pages/feed/index.tsx @@ -44,7 +44,7 @@ interface HighlightSSRProps { } const Feeds: WithPageLayout = (props: HighlightSSRProps) => { - const { user } = useSupabaseAuth(); + const { user, signIn } = useSupabaseAuth(); const { data: repos } = useFetchHighlightRepos(); const { data: featuredHighlights } = useFetchFeaturedHighlights(); @@ -85,6 +85,46 @@ const Feeds: WithPageLayout = (props: HighlightSSRProps) => { { name: "Highlights", count: highlights_count ?? 0 }, ]; + useEffect(() => { + const queryParams = new URLSearchParams(window.location.search); + const newHighlight = queryParams.get("new"); + const signInRequired = queryParams.get("signIn"); + + if (newHighlight && signInRequired) { + signIn({ provider: "github", options: { redirectTo: `${window.location.origin}/feed?new=${newHighlight}` } }); + } + + // no need to create intervals for checking the highlight creation input if there is no new highlight + if (!newHighlight) { + return; + } + + let focusOnHighlighCreationInput: NodeJS.Timeout; + + if (window.innerWidth > 768) { + focusOnHighlighCreationInput = setInterval(() => { + const highlightCreationInput = document.getElementById("highlight-create-input"); + if (newHighlight && highlightCreationInput) { + highlightCreationInput.click(); + highlightCreationInput.focus(); + clearInterval(focusOnHighlighCreationInput); + } + }, 1000); + } else { + // for mobile. No need to focus on input, just click on the button as it opens up a form anyway. + focusOnHighlighCreationInput = setInterval(() => { + const mobileHighlightCreateButton = document.getElementById("mobile-highlight-create-button"); + if (newHighlight && mobileHighlightCreateButton) { + mobileHighlightCreateButton.click(); + clearInterval(focusOnHighlighCreationInput); + } + }, 1000); + } + return () => { + clearInterval(focusOnHighlighCreationInput); + }; + }, []); + useEffect(() => { if (activeTab === "home") { setRepoList(repoTofilterList(repos)); @@ -287,7 +327,10 @@ const Feeds: WithPageLayout = (props: HighlightSSRProps) => { selectedFilter={selectedRepo} setSelected={(repo) => { if (!openSingleHighlight) { - router.push(`/feed${repo ? `?repo=${repo}` : ""}`); + const queryParams = new URLSearchParams(window.location.search); + router.push( + `/feed${repo ? `?repo=${repo}` : queryParams.toString() ? `?${queryParams.toString()}` : ""}` + ); setPage(1); setSelectedRepo(repo); }