From df00c24ff6368e3b94067442be954e2e4447ca9f Mon Sep 17 00:00:00 2001 From: AJAmit17 Date: Thu, 14 Mar 2024 19:06:14 +0530 Subject: [PATCH 1/3] Fix Search Bar Query Navigation Fixed the search query in Search bar --- components/searchBar/index.tsx | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/components/searchBar/index.tsx b/components/searchBar/index.tsx index beeedb19..704571c3 100644 --- a/components/searchBar/index.tsx +++ b/components/searchBar/index.tsx @@ -38,13 +38,21 @@ export default function SearchBar({ function handleInput(event: FormEvent) { setQuery((event.target as HTMLInputElement).value); + const searchValue = (event.target as HTMLInputElement).value; // When input value is null, set error & empty state to `true` and do nothing. - if (!smallScreen) - debounce(() => { - router.push(`/search?q=${(event.target as HTMLInputElement).value}`); - }); - if (!(event.target as HTMLInputElement).value) { + if (!smallScreen) { + if (searchValue) { + debounce(() => { + router.push(`/search?q=${searchValue}`); + }); + } else { + debounce(() => { + router.push(`/`); + }); + } + } + if (!searchValue) { setIsEmpty(true); setIsError(true); return; @@ -53,7 +61,7 @@ export default function SearchBar({ // When input value is not null, reset error & empty state to `false`. // And also debounce the router push. setIsError(false); - setIsEmpty(false); + setIsEmpty(false); } function handleSubmit(event?: FormEvent) { From 0b88ca281497fd7a47c718ee853c3122e243de93 Mon Sep 17 00:00:00 2001 From: AJAmit17 Date: Sat, 16 Mar 2024 16:37:05 +0530 Subject: [PATCH 2/3] [FEATURE] Added Copy button with Toast notification Added Copy Button which copies the code snippet and gives a toast notification saying Code is Copied. --- components/codePreview/index.tsx | 17 ++++++++++++++++- components/searchBar/index.tsx | 18 +++++------------- hooks/toast-provider.tsx | 7 +++++++ package.json | 1 + pages/_app.tsx | 2 ++ yarn.lock | 12 ++++++++++++ 6 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 hooks/toast-provider.tsx diff --git a/components/codePreview/index.tsx b/components/codePreview/index.tsx index 59dbed5c..2f3d4680 100644 --- a/components/codePreview/index.tsx +++ b/components/codePreview/index.tsx @@ -1,4 +1,4 @@ -import React, { useRef, useState } from "react"; +import React, { useCallback, useRef, useState } from "react"; import "highlight.js/styles/atom-one-light.css"; import { Algorithm } from "lib/models"; import { @@ -27,7 +27,9 @@ import { MoreHoriz, OpenInNew, PlayArrow, + FileCopyOutlined, } from "@material-ui/icons"; +import { toast } from "react-hot-toast"; import NextLink from "next/link"; import { StringParam, useQueryParam, withDefault } from "next-query-params"; import classes from "./style.module.css"; @@ -45,6 +47,12 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) { const fabRef = useRef(); const [mobileMoreMenuOpen, setMobileMoreMenuOpen] = useState(false); + const copyCode = useCallback(async () => { + const codeText = document.querySelector(".style_pre__k555n")?.textContent; + await navigator.clipboard.writeText(codeText); + toast.success("Code Snippet Copied!"); + }, []); + return (
@@ -86,6 +94,10 @@ export default function CodePreview({ algorithm }: { algorithm: Algorithm }) { {t("fullscreen")} + copyCode()}> + + {t("Copy Code")} +
+ + +