Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

improved CMDK + help center

Steven Teycd38f8be17b3
diff --git a/app/ui/cmdk.tsx b/app/ui/cmdk.tsx
index 014890bdd3..fbf4fe2c0a 100644
--- a/app/ui/cmdk.tsx
+++ b/app/ui/cmdk.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Command } from "cmdk";
+import { Command, useCommandState } from "cmdk";
import {
Dispatch,
SetStateAction,
@@ -10,8 +10,12 @@ import {
useState,
} from "react";
import Modal from "./modal";
-import { Magic } from "./icons";
-import { Book } from "lucide-react";
+import { ExpandingArrow, Magic } from "./icons";
+import { POPULAR_ARTICLES } from "app/dub.sh/help/constants";
+import { allHelpPosts } from "contentlayer/generated";
+import { useRouter } from "next/navigation";
+import { useRouter as usePagesRouter } from "next/router";
+import Highlighter from "react-highlight-words";
function CMDKHelper({
showCMDK,
@@ -20,7 +24,6 @@ function CMDKHelper({
showCMDK: boolean;
setShowCMDK: Dispatch<SetStateAction<boolean>>;
}) {
- const items = ["Getting Started", "Guides", "API Reference", "Examples"];
return (
<Modal showModal={showCMDK} setShowModal={setShowCMDK}>
<Command
@@ -33,31 +36,75 @@ function CMDKHelper({
placeholder="Search articles, guides, and more..."
className="w-full border-none p-4 font-normal placeholder-gray-400 focus:outline-none focus:ring-0"
/>
- <Command.List className="border-t border-gray-200 p-2 transition-all sm:h-[calc(var(--cmdk-list-height)+10rem)] sm:max-h-[300px]">
- <Command.Empty className="flex items-center space-x-2 rounded-md bg-gray-100 px-3 py-2 text-sm text-gray-600">
+ <Command.List className="h-[50vh] max-h-[360px] min-h-[250px] overflow-scroll border-t border-gray-200 p-2 transition-all scrollbar-hide sm:h-[calc(var(--cmdk-list-height)+10rem)]">
+ <Command.Empty className="flex items-center space-x-2 rounded-md bg-gray-100 px-4 py-2 text-sm text-gray-600">
<Magic className="h-4 w-4 text-gray-400" />
- <p>Ask AI</p>
+ <div className="flex flex-col space-y-1">
+ <p className="text-sm font-medium text-purple-600">Ask AI</p>
+ <p className="text-xs text-gray-400">
+ Use our AI to find answers to your questions
+ </p>
+ </div>
</Command.Empty>
- {items.map((item) => (
- <Command.Item
- key={item}
- value={item}
- onSelect={() => {
- console.log("selected", item);
- }}
- className="flex cursor-pointer items-center space-x-2 rounded-md px-3 py-2 text-sm text-gray-500 hover:bg-gray-100 hover:text-gray-700 active:bg-gray-200 aria-selected:bg-gray-100 aria-selected:text-gray-700"
- >
- <Book className="h-4 w-4 text-gray-400" />
- <p>{item}</p>
- </Command.Item>
- ))}
+ <CommandResults setShowCMDK={setShowCMDK} />
</Command.List>
</Command>
</Modal>
);
}
+const CommandResults = ({
+ setShowCMDK,
+}: {
+ setShowCMDK: Dispatch<SetStateAction<boolean>>;
+}) => {
+ let router;
+ try {
+ router = useRouter();
+ } catch (e) {
+ router = usePagesRouter();
+ }
+
+ const popularArticles = POPULAR_ARTICLES.map(
+ (slug) => allHelpPosts.find((post) => post.slug === slug)!,
+ );
+
+ const search = useCommandState((state) => state.search);
+
+ return (search.length === 0 ? popularArticles : allHelpPosts).map(
+ (article) => (
+ <Command.Item
+ key={article.slug}
+ value={article.title}
+ onSelect={() => {
+ router.push(`/help/article/${article.slug}`);
+ setShowCMDK(false);
+ }}
+ className="group flex cursor-pointer items-center justify-between space-x-2 rounded-md px-4 py-2 hover:bg-gray-100 active:bg-gray-200 aria-selected:bg-gray-100"
+ >
+ <div className="flex flex-col space-y-1">
+ <Highlighter
+ highlightClassName="underline bg-transparent text-purple-500"
+ searchWords={[search]}
+ autoEscape={true}
+ textToHighlight={article.title}
+ className="text-sm font-medium text-gray-600 group-aria-selected:text-purple-600 sm:group-hover:text-purple-600"
+ />
+ <Highlighter
+ highlightClassName="underline bg-transparent text-purple-500"
+ searchWords={[search]}
+ autoEscape={true}
+ textToHighlight={article.summary}
+ className="text-xs text-gray-400"
+ />
+ </div>
+ <ExpandingArrow className="invisible -ml-4 h-4 w-4 text-purple-600 group-aria-selected:visible sm:group-hover:visible" />
+ </Command.Item>
+ ),
+ );
+};
+
export default function useCMDK() {
const [showCMDK, setShowCMDK] = useState(false);
diff --git a/app/ui/help/search-button.tsx b/app/ui/help/search-button.tsx
index d46244898a..c67937c312 100644
--- a/app/ui/help/search-button.tsx
+++ b/app/ui/help/search-button.tsx
@@ -6,11 +6,17 @@ import { AppContext } from "app/providers";
export default function SearchButton() {
const { setShowCMDK } = useContext(AppContext);
return (
- <button onClick={() => setShowCMDK(true)} className="group relative flex">
+ <button
+ onClick={() => setShowCMDK(true)}
+ className="group relative flex focus:outline-none"
+ >
<Search className="absolute inset-y-0 left-4 z-10 my-auto h-4 w-4 text-gray-500" />
<div className="w-full rounded-xl border border-gray-200 bg-white p-3 pl-12 text-left text-gray-500 transition-colors group-active:bg-gray-50">
Search for articles...
</div>
+ <span className="absolute inset-y-0 right-4 my-auto h-5 text-sm text-gray-400">
+ ⌘K
+ </span>
</button>
);
}
diff --git a/app/ui/modal.tsx b/app/ui/modal.tsx
index 6d9497ac1c..1d72a87436 100644
--- a/app/ui/modal.tsx
+++ b/app/ui/modal.tsx
@@ -136,7 +136,7 @@ export default function Modal({
<motion.div
id="modal-backdrop"
key="backdrop"
- className="fixed inset-0 z-40 bg-gray-100 bg-opacity-10 backdrop-blur"
+ className="fixed inset-0 z-40 bg-gray-100 bg-opacity-50 backdrop-blur-md"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
diff --git a/package.json b/package.json
index 9043566c4a..fcb4c52f61 100644
--- a/package.json
+++ b/package.json
@@ -68,6 +68,7 @@
"react-dom": "^18.2.0",
"react-dom-confetti": "^0.2.0",
"react-email": "^1.9.4",
+ "react-highlight-words": "^0.20.0",
"react-parallax-tilt": "^1.7.70",
"react-spring": "^9.5.5",
"react-textarea-autosize": "^8.4.0",
diff --git a/yarn.lock b/yarn.lock
index db7a1962df..722514a98e 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4652,6 +4652,11 @@ [email protected]:
resolved "https://registry.npmjs.org/he/-/he-1.2.0.tgz"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==
+highlight-words-core@^1.2.0:
+ version "1.2.2"
+ resolved "https://registry.yarnpkg.com/highlight-words-core/-/highlight-words-core-1.2.2.tgz#1eff6d7d9f0a22f155042a00791237791b1eeaaa"
+ integrity sha512-BXUKIkUuh6cmmxzi5OIbUJxrG8OAk2MqoL1DtO3Wo9D2faJg2ph5ntyuQeLqaHJmzER6H5tllCDA9ZnNe9BVGg==
+
hosted-git-info@^2.1.4:
version "2.8.9"
resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9"
@@ -5696,6 +5701,11 @@ memfs@^3.5.1:
dependencies:
fs-monkey "^1.0.3"
+memoize-one@^4.0.0:
+ version "4.1.0"
+ resolved "https://registry.yarnpkg.com/memoize-one/-/memoize-one-4.1.0.tgz#a2387c58c03fff27ca390c31b764a79addf3f906"
+ integrity sha512-2GApq0yI/b22J2j9rhbrAlsHb0Qcz+7yWxeLG8h+95sl1XPUgeLimQSOdur4Vw7cUhrBHwaUZxWFZueojqNRzA==
+
merge-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
@@ -7112,7 +7122,7 @@ promzard@^0.3.0:
dependencies:
read "1"
-prop-types@^15.5.10, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
+prop-types@^15.5.10, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.1, prop-types@^15.6.2, prop-types@^15.7.2:
version "15.8.1"
resolved "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
@@ -7233,6 +7243,15 @@ react-email@^1.9.4:
shelljs "0.8.5"
tree-node-cli "1.6.0"
+react-highlight-words@^0.20.0:
+ version "0.20.0"
+ resolved "https://registry.yarnpkg.com/react-highlight-words/-/react-highlight-words-0.20.0.tgz#c60bfff5d14678c8f0e8fbe4bdcf083e6c70d507"
+ integrity sha512-asCxy+jCehDVhusNmCBoxDf2mm1AJ//D+EzDx1m5K7EqsMBIHdZ5G4LdwbSEXqZq1Ros0G0UySWmAtntSph7XA==
+ dependencies:
+ highlight-words-core "^1.2.0"
+ memoize-one "^4.0.0"
+ prop-types "^15.5.8"
+
react-is@^16.13.1:
version "16.13.1"
resolved "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz"