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(analytics): Track search events #241

Merged
merged 23 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
da1b8ce
Implement new search UI
half0wl Apr 20, 2023
d07b7a7
Move "no results" into its own component
half0wl Apr 24, 2023
8020723
re-lock dependencies
half0wl Apr 24, 2023
bb09a55
Refactor: Move search logic into reusable hooks
half0wl Apr 24, 2023
0583ee8
Refactor: clean up, combine useSearchIndex
half0wl Apr 24, 2023
eec4afc
Refactor: Separate QueryInput, add ability to clear results
half0wl Apr 24, 2023
0eb5515
Cleanup
half0wl Apr 24, 2023
c5f4e0e
Tweak UI
half0wl Apr 24, 2023
3e4a77c
Cleanup: Remove unused types
half0wl Apr 24, 2023
b7d5546
feat(analytics): Track search events
half0wl Apr 24, 2023
011bdc4
Move searchParams out; lower debounce ms
half0wl Apr 24, 2023
3a36133
UI: Make "Clear" button smaller
half0wl Apr 24, 2023
035fc9a
Remove OpenSearchModalButton from mobile nav
half0wl Apr 24, 2023
fc48cae
Revert "Remove OpenSearchModalButton from mobile nav"
half0wl Apr 24, 2023
9f2ab5d
cleanup
half0wl Apr 24, 2023
427bf81
Merge branch 'feat/new-search-ui' into feat/search-analytics
half0wl Apr 24, 2023
5f05d07
Refactor to a generic response transformation method
half0wl Apr 24, 2023
c016b15
Remove redundant `Array.from`
half0wl Apr 24, 2023
82a2878
Remove baseUri from slug
half0wl Apr 24, 2023
1d7d8e8
fix: close modal on result click
half0wl Apr 24, 2023
e6494c8
Merge branch 'main' into feat/new-search-ui
half0wl Apr 25, 2023
2744558
Merge branch 'feat/new-search-ui' into feat/search-analytics
half0wl Apr 25, 2023
428f85c
Merge branch 'main' into feat/search-analytics
half0wl Apr 25, 2023
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"dayjs": "^1.10.4",
"fathom-client": "^3.1.0",
"fuse.js": "^6.4.6",
"interweave": "^13.1.0",
"meilisearch": "^0.32.3",
"nanostores": "^0.7.4",
"next": "13.2.1",
"next-contentlayer": "^0.3.0",
Expand Down
1 change: 1 addition & 0 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const ModalDialog: React.FC<PropsWithChildren<Props>> = ({
css={[
tw`fixed top-0 right-0 bottom-0 left-0 select-none z-50`,
tw`bg-black bg-opacity-50`,
tw`overflow-scroll`,
]}
>
<FocusScope contain restoreFocus autoFocus>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Home, Menu, X } from "react-feather";
import tw from "twin.macro";
import { Link } from "./Link";
import { Logo } from "./Logo";
import { OpenSearchModalButton } from "@/components/Search";
import { MobileSidebar } from "./Sidebar";
import { ThemeSwitcher } from "./ThemeSwitcher";

Expand Down Expand Up @@ -55,9 +56,9 @@ export const MobileNav: React.FC = () => {
<Logo tw="w-10 h-10" />
</Link>

{/* <div tw="w-full block">
<Search />
</div> */}
<div tw="w-full block">
<OpenSearchModalButton />
</div>

<div tw="flex items-center space-x-4 md:space-x-4">
<button
Expand Down
144 changes: 0 additions & 144 deletions src/components/Search.tsx

This file was deleted.

60 changes: 60 additions & 0 deletions src/components/Search/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useDebouncedSearch } from "@/hooks/useDebouncedSearch";
import { Search } from "@/types";
import React from "react";
import tw from "twin.macro";
import NoResults from "./NoResults";
import QueryInput from "./QueryInput";
import Results from "./Results";

interface Props {
closeModal: () => void;
}

const Modal: React.FC<Props> = ({ closeModal }) => {
const searchParams = {
limit: 10,
attributesToHighlight: ["*"],
highlightPreTag: "<span>",
highlightPostTag: "</span>",
};
const { clearResponse, query, setQuery, response } =
useDebouncedSearch<Search.Document>(
process.env.NEXT_PUBLIC_MEILISEARCH_HOST ?? "",
process.env.NEXT_PUBLIC_MEILISEARCH_READ_API_KEY ?? "",
process.env.NEXT_PUBLIC_MEILISEARCH_INDEX_NAME ?? "",
searchParams,
200,
);

return (
<div
css={[tw`px-2 mt-12 mb-12`, `sm:px-4`, `md:px-0 md:mt-28 md:mb-28`]}
onPointerDown={() => {
closeModal();
}}
>
<div
onPointerDown={e => e.stopPropagation()}
css={[tw`bg-background border rounded-lg w-full md:w-1/2 mx-auto`]}
>
<div className="search-input">
<QueryInput
clearResponse={clearResponse}
query={query}
setQuery={setQuery}
/>
</div>
<div className="search-results">
{response &&
(response.hits.length === 0 ? (
<NoResults />
) : (
<Results response={response} />
))}
</div>
</div>
</div>
);
};

export default Modal;
43 changes: 43 additions & 0 deletions src/components/Search/NoResults.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { DiscordIcon } from "@/components/Icons";
import { Link } from "@/components/Link";
import React from "react";
import { HelpCircle, Mail } from "react-feather";
import tw, { styled } from "twin.macro";

const ContactButton = styled(Link)`
${[
tw`flex flex-row items-center gap-2 p-2`,
tw`border border-solid rounded-lg`,
tw`hover:bg-pink-100`,
tw`[&>svg]:w-8`,
tw`[&>svg]:h-8`,
]}
`;

const NoResults: React.FC = () => {
return (
<div css={[tw`flex flex-col items-center justify-center mb-4`]}>
<HelpCircle size={64} css={tw`mt-8 mb-4`} />
<div css={tw`flex flex-col items-center justify-center p-4`}>
<p css={tw`font-bold mb-4 text-center`}>
We couldn't find what you're searching for.
</p>
<div>
<p css={tw`mb-4 text-center`}>Reach out to us if you need help:</p>
<div css={tw`flex flex-row gap-4 items-center justify-center`}>
<ContactButton href="https://discord.gg/railway">
<DiscordIcon />
Discord
</ContactButton>
<ContactButton href="mailto:[email protected]">
<Mail />
Email
</ContactButton>
</div>
</div>
</div>
</div>
);
};

export default NoResults;
28 changes: 28 additions & 0 deletions src/components/Search/OpenModalButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { searchStore } from "@/store";
import React from "react";
import { Search as SearchIcon } from "react-feather";
import tw from "twin.macro";

const OpenModalButton: React.FC = () => {
return (
<>
<button
onClick={() => searchStore.set(true)}
css={[
tw`flex items-center justify-between space-x-4 w-full`,
tw`rounded border border-gray-200 cursor-pointer`,
tw`px-2 py-2 md:py-1 text-gray-300 text-left`,
tw`focus:outline-none md:hover:border-pink-300`,
]}
>
<div tw="flex items-center space-x-2">
<SearchIcon tw="w-4 h-4" />
<span tw="text-sm">Search</span>
</div>
<div tw="text-gray-300 text-sm hidden md:block">⌘K</div>
</button>
</>
);
};

export default OpenModalButton;
47 changes: 47 additions & 0 deletions src/components/Search/QueryInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from "react";
import { Search as SearchIcon } from "react-feather";
import tw from "twin.macro";

interface Props {
clearResponse: () => void;
query: string;
setQuery: (q: string) => void;
}

const QueryInput: React.FC<Props> = ({ clearResponse, query, setQuery }) => {
return (
<div css={tw`flex flex-col`}>
<form css={tw`flex flex-row`}>
<span css={tw`flex items-center px-3`}>
<SearchIcon />
</span>
<input
autoFocus
css={tw`w-full focus:outline-none bg-transparent`}
type="text"
placeholder="Search"
value={query}
onChange={e => setQuery(e.target.value)}
/>
<span css={tw`flex items-center p-2`}>
<button
css={[
tw`flex items-center justify-center w-14 h-8`,
tw`rounded border border-solid rounded-lg`,
tw`text-black text-sm dark:text-white`,
tw`hover:bg-pink-100`,
]}
onClick={e => {
e.preventDefault();
clearResponse();
}}
>
Clear
</button>
</span>
</form>
</div>
);
};

export default QueryInput;
Loading