Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for highlight.new #1487

Merged
merged 8 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>
</div>
</CollapsibleTrigger>
Expand Down Expand Up @@ -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"
/>
</div>
<TypeWriterTextArea
Expand Down Expand Up @@ -337,6 +339,7 @@ const HighlightInputForm = ({ refreshCallback }: HighlightInputFormProps): JSX.E
<div
onClick={() => setIsFormOpenMobile(true)}
className="p-3 text-white rounded-full shadow-lg bg-light-orange-10"
id="mobile-highlight-create-button"
>
<RxPencil1 className="text-3xl" />
</div>
Expand Down
10 changes: 8 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"],
};
47 changes: 45 additions & 2 deletions pages/feed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface HighlightSSRProps {
}

const Feeds: WithPageLayout<HighlightSSRProps> = (props: HighlightSSRProps) => {
const { user } = useSupabaseAuth();
const { user, signIn } = useSupabaseAuth();
const { data: repos } = useFetchHighlightRepos();

const { data: featuredHighlights } = useFetchFeaturedHighlights();
Expand Down Expand Up @@ -85,6 +85,46 @@ const Feeds: WithPageLayout<HighlightSSRProps> = (props: HighlightSSRProps) => {
{ name: "Highlights", count: highlights_count ?? 0 },
];

useEffect(() => {
const queryParams = new URLSearchParams(window.location.search);
diivi marked this conversation as resolved.
Show resolved Hide resolved
const newHighlight = queryParams.get("new");
diivi marked this conversation as resolved.
Show resolved Hide resolved
const signInRequired = queryParams.get("signIn");
diivi marked this conversation as resolved.
Show resolved Hide resolved

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) {
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
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);
brandonroberts marked this conversation as resolved.
Show resolved Hide resolved
} 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);
diivi marked this conversation as resolved.
Show resolved Hide resolved
}
}, 1000);
}
return () => {
clearInterval(focusOnHighlighCreationInput);
};
}, []);

useEffect(() => {
if (activeTab === "home") {
setRepoList(repoTofilterList(repos));
Expand Down Expand Up @@ -287,7 +327,10 @@ const Feeds: WithPageLayout<HighlightSSRProps> = (props: HighlightSSRProps) => {
selectedFilter={selectedRepo}
setSelected={(repo) => {
if (!openSingleHighlight) {
router.push(`/feed${repo ? `?repo=${repo}` : ""}`);
const queryParams = new URLSearchParams(window.location.search);
diivi marked this conversation as resolved.
Show resolved Hide resolved
router.push(
`/feed${repo ? `?repo=${repo}` : queryParams.toString() ? `?${queryParams.toString()}` : ""}`
);
setPage(1);
setSelectedRepo(repo);
}
Expand Down