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.

Added fuse.js for fuzzy search (h/t to @dferber90 for the protip)

Steven Teye8157b0a3bb9
diff --git a/app/ui/cmdk.tsx b/app/ui/cmdk.tsx
index 1efa9403cf..3ae7e711c6 100644
--- a/app/ui/cmdk.tsx
+++ b/app/ui/cmdk.tsx
@@ -7,6 +7,7 @@ import {
useCallback,
useEffect,
useMemo,
+ useRef,
useState,
} from "react";
import Modal from "./modal";
@@ -16,6 +17,7 @@ import { allHelpPosts } from "contentlayer/generated";
import { useRouter } from "next/navigation";
import Highlighter from "react-highlight-words";
import { APP_HOSTNAMES, HOME_DOMAIN } from "#/lib/constants";
+import Fuse from "fuse.js";
function CMDKHelper({
showCMDK,
@@ -24,26 +26,29 @@ function CMDKHelper({
showCMDK: boolean;
setShowCMDK: Dispatch<SetStateAction<boolean>>;
}) {
+ const commandListRef = useRef<HTMLDivElement>(null);
return (
<Modal
showModal={showCMDK}
setShowModal={setShowCMDK}
className="sm:max-w-xl"
>
- <Command
- label="CMDK"
- loop
- filter={(value, search) => {
- if (value.includes(search.toLowerCase())) return 1;
- return 0;
- }}
- >
+ <Command label="CMDK" loop shouldFilter={false}>
<Command.Input
autoFocus
+ onInput={() => {
+ // hack to scroll to top of list when input changes (for some reason beyond my comprehension, setTimeout is needed)
+ setTimeout(() => {
+ commandListRef.current?.scrollTo(0, 0);
+ }, 0);
+ }}
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="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.List
+ ref={commandListRef}
+ 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 cursor-not-allowed 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" />
<div className="flex flex-col space-y-1">
@@ -73,55 +78,68 @@ const CommandResults = ({
);
const allItems = [
- ...allHelpPosts,
+ ...allHelpPosts.map((post) => ({
+ ...post,
+ description: post.summary,
+ })),
// get all table of contents headings too
...allHelpPosts.flatMap((post) =>
post.tableOfContents.map((toc: { title: string; slug: string }) => ({
slug: `${post.slug}#${toc.slug}`,
title: toc.title,
+ description: null, // omit description since we don't want to search it
summary: `In: "${post.title}"`,
})),
),
];
+ const fuse = new Fuse(allItems, {
+ keys: ["title", "description"],
+ });
+
const search = useCommandState((state) => state.search);
- return (search.length === 0 ? popularArticles : allItems).map(
- ({ slug, title, summary }) => (
- <Command.Item
- key={slug}
- value={title}
- onSelect={() => {
- if (APP_HOSTNAMES.has(window.location.hostname)) {
- // this is from the app, open in new tab
- window.open(`${HOME_DOMAIN}/help/article/${slug}`);
- } else {
- router.push(`/help/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={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={summary}
- className="line-clamp-1 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>
- ),
- );
+ const results = useMemo(() => {
+ if (search.length === 0) {
+ return popularArticles;
+ }
+ return fuse.search(search).map((r) => r.item);
+ }, [search, popularArticles]);
+
+ return results.map(({ slug, title, summary }) => (
+ <Command.Item
+ key={slug}
+ value={title}
+ onSelect={() => {
+ if (APP_HOSTNAMES.has(window.location.hostname)) {
+ // this is from the app, open in new tab
+ window.open(`${HOME_DOMAIN}/help/article/${slug}`);
+ } else {
+ router.push(`/help/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.split(" ")}
+ autoEscape={true}
+ textToHighlight={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.split(" ")}
+ autoEscape={true}
+ textToHighlight={summary}
+ className="line-clamp-1 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() {
diff --git a/content/help/dub-api.mdx b/content/help/dub-api.mdx
index 27ed082759..2e9847c0d9 100644
--- a/content/help/dub-api.mdx
+++ b/content/help/dub-api.mdx
@@ -7,6 +7,6 @@ categories:
- api
---
-No, Dub does not have an API yet, but it is [planned for the September 2023](https://github.com/steven-tey/dub/issues/73#issuecomment-1638409034).
+No, Dub does not have an API yet, but it is [planned for September 2023](https://github.com/steven-tey/dub/issues/73#issuecomment-1638409034).
If you would like to be notified when the API is available, please [sign up for the waitlist](https://tally.so/r/wdN50V).
diff --git a/content/help/how-to-create-link.mdx b/content/help/how-to-create-link.mdx
index b4b29e50ab..14379cd961 100644
--- a/content/help/how-to-create-link.mdx
+++ b/content/help/how-to-create-link.mdx
@@ -63,7 +63,7 @@ Inside the link builder are the following fields:
5. **UTM Builder**: Add UTM parameters to your links. [Learn more](#utm-builder).
6. **Password Protection**: Protect your links with a password. [Learn more](#password-protection).
7. **Expiration Date**: Set an expiration date for your links. [Learn more](#expiration-date).
-8. **Device Targeting**: Set a custom destination URL for iOS and Android devices. [Learn more](#device-targeting).
+8. **Device Targeting**: Set a custom destination URL for iOS and Android devices. [Learn more](#device-targeting-ios--android).
9. **Social Previews**: See how your links will look when shared on social media.
## Custom social media cards
@@ -140,7 +140,7 @@ You can set an expiration date for your links by toggling the "Expiration Date"
If you set an expiration date for your link, the user will be redirected to a 404 page when they try to access the link after the expiration date.
-## Device targeting
+## Device targeting (iOS / Android)
Device targeting is a powerful feature that lets you personalize the destination URL for your links based on the user's device type.
diff --git a/package.json b/package.json
index fe8fa7188f..0cf8dfc492 100644
--- a/package.json
+++ b/package.json
@@ -50,6 +50,7 @@
"crisp-sdk-web": "^1.0.19",
"focus-trap-react": "^10.0.1",
"framer-motion": "^10.12.12",
+ "fuse.js": "^6.6.2",
"github-slugger": "^2.0.0",
"html-escaper": "^3.0.3",
"i": "^0.3.7",
@@ -98,6 +99,7 @@
"@types/node": "18.11.9",
"@types/react": "^18.2.14",
"@types/react-dom": "^18.2.6",
+ "@types/react-highlight-words": "^0.16.4",
"@types/topojson-client": "^3.1.1",
"array-to-ndjson": "^1.0.1",
"autoprefixer": "^10.4.13",
diff --git a/yarn.lock b/yarn.lock
index cecd8fb186..9f1e106b44 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2580,6 +2580,13 @@
dependencies:
"@types/react" "*"
+"@types/react-highlight-words@^0.16.4":
+ version "0.16.4"
+ resolved "https://registry.yarnpkg.com/@types/react-highlight-words/-/react-highlight-words-0.16.4.tgz#441b6f05c27ce6ab76833803a84bc385f0a1e8f2"
+ integrity sha512-KITBX3xzheQLu2s3bUgLmRE7ekmhc52zRjRTwkKayQARh30L4fjEGzGm7ULK9TuX2LgxWWavZqyQGDGjAHbL3w==
+ dependencies:
+ "@types/react" "*"
+
"@types/react@*":
version "18.0.25"
resolved "https://registry.npmjs.org/@types/react/-/react-18.0.25.tgz"
@@ -4345,6 +4352,11 @@ function-bind@^1.1.1:
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==
+fuse.js@^6.6.2:
+ version "6.6.2"
+ resolved "https://registry.yarnpkg.com/fuse.js/-/fuse.js-6.6.2.tgz#fe463fed4b98c0226ac3da2856a415576dc9a111"
+ integrity sha512-cJaJkxCCxC8qIIcPBF9yGxY0W/tVZS3uEISDxhYIdtk8OL93pe+6Zj7LjCqVV4dzbqcriOZ+kQ/NE4RXZHsIGA==
+
gauge@^4.0.3:
version "4.0.4"
resolved "https://registry.npmjs.org/gauge/-/gauge-4.0.4.tgz"