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.

Add shift-key multiselect

Tim Wilsonbe72146651e4
diff --git a/apps/web/ui/links/link-selection-provider.tsx b/apps/web/ui/links/link-selection-provider.tsx
new file mode 100644
index 0000000000..705ce9f607
--- /dev/null
+++ b/apps/web/ui/links/link-selection-provider.tsx
@@ -0,0 +1,86 @@
+import {
+ Dispatch,
+ SetStateAction,
+ createContext,
+ useContext,
+ useState,
+} from "react";
+import { ResponseLink } from "./links-container";
+
+interface LinkSelectionContext {
+ selectedLinkIds: string[];
+ setSelectedLinkIds: Dispatch<SetStateAction<string[]>>;
+ lastSelectedLinkId: string | null;
+ handleLinkSelection: (linkId: string, e: React.MouseEvent) => void;
+}
+
+const LinkSelectionContext = createContext<LinkSelectionContext | null>(null);
+
+export function LinkSelectionProvider({
+ children,
+ links,
+}: {
+ children: React.ReactNode;
+ links?: ResponseLink[];
+}) {
+ const [selectedLinkIds, setSelectedLinkIds] = useState<string[]>([]);
+ const [lastSelectedLinkId, setLastSelectedLinkId] = useState<string | null>(
+ null,
+ );
+
+ const handleLinkSelection = (linkId: string, e: React.MouseEvent) => {
+ if (e.shiftKey && lastSelectedLinkId && links) {
+ const lastSelectedIndex = links.findIndex(
+ (l) => l.id === lastSelectedLinkId,
+ );
+ const currentIndex = links.findIndex((l) => l.id === linkId);
+
+ if (lastSelectedIndex !== -1 && currentIndex !== -1) {
+ const start = Math.min(lastSelectedIndex, currentIndex);
+ const end = Math.max(lastSelectedIndex, currentIndex);
+ const rangeIds = links.slice(start, end + 1).map((l) => l.id);
+
+ if (selectedLinkIds.includes(linkId)) {
+ setSelectedLinkIds((prev) =>
+ prev.filter((id) => !rangeIds.includes(id)),
+ );
+ } else {
+ setSelectedLinkIds((prev) =>
+ Array.from(new Set([...prev, ...rangeIds])),
+ );
+ }
+ setLastSelectedLinkId(linkId);
+ }
+ } else {
+ setLastSelectedLinkId(linkId);
+ setSelectedLinkIds((prev) =>
+ prev.includes(linkId)
+ ? prev.filter((id) => id !== linkId)
+ : [...prev, linkId],
+ );
+ }
+ };
+
+ return (
+ <LinkSelectionContext.Provider
+ value={{
+ selectedLinkIds,
+ setSelectedLinkIds,
+ lastSelectedLinkId,
+ handleLinkSelection,
+ }}
+ >
+ {children}
+ </LinkSelectionContext.Provider>
+ );
+}
+
+export function useLinkSelection() {
+ const context = useContext(LinkSelectionContext);
+ if (context === null) {
+ throw new Error(
+ "useLinkSelection must be used within LinkSelectionProvider",
+ );
+ }
+ return context;
+}
diff --git a/apps/web/ui/links/link-title-column.tsx b/apps/web/ui/links/link-title-column.tsx
index e317957d8a..a198cb4053 100644
--- a/apps/web/ui/links/link-title-column.tsx
+++ b/apps/web/ui/links/link-title-column.tsx
@@ -45,7 +45,8 @@ import { memo, PropsWithChildren, useContext, useRef, useState } from "react";
import { FolderIcon } from "../folders/folder-icon";
import { useLinkBuilder } from "../modals/link-builder";
import { CommentsBadge } from "./comments-badge";
-import { LinksListContext, ResponseLink } from "./links-container";
+import { useLinkSelection } from "./link-selection-provider";
+import { ResponseLink } from "./links-container";
import { LinksDisplayContext } from "./links-display-provider";
const quickViewSettings = [
@@ -67,7 +68,7 @@ export function LinkTitleColumn({ link }: { link: ResponseLink }) {
const { variant } = useContext(CardList.Context);
const { displayProperties } = useContext(LinksDisplayContext);
- const { selectedLinkIds, setSelectedLinkIds } = useContext(LinksListContext);
+ const { selectedLinkIds, handleLinkSelection } = useLinkSelection();
const ref = useRef<HTMLDivElement>(null);
@@ -105,13 +106,7 @@ export function LinkTitleColumn({ link }: { link: ResponseLink }) {
role="checkbox"
aria-checked={isSelected}
data-checked={isSelected}
- onClick={() => {
- setSelectedLinkIds((prev) =>
- prev.includes(link.id)
- ? prev.filter((id) => id !== link.id)
- : [...prev, link.id],
- );
- }}
+ onClick={(e) => handleLinkSelection(link.id, e)}
className="group relative hidden shrink-0 items-center justify-center outline-none sm:flex"
>
{/* Link logo background circle */}
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 7dfd3d487d..499508122b 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -13,9 +13,9 @@ import { CursorRays, Hyperlink, LoadingSpinner } from "@dub/ui/icons";
import { cn } from "@dub/utils";
import { useSearchParams } from "next/navigation";
import {
+ createContext,
Dispatch,
SetStateAction,
- createContext,
useContext,
useState,
} from "react";
@@ -24,6 +24,10 @@ import { X } from "../shared/icons";
import ArchivedLinksHint from "./archived-links-hint";
import { LinkCard } from "./link-card";
import LinkCardPlaceholder from "./link-card-placeholder";
+import {
+ LinkSelectionProvider,
+ useLinkSelection,
+} from "./link-selection-provider";
import { LinksDisplayContext } from "./links-display-provider";
export type ResponseLink = ExpandedLinkProps & {
@@ -54,13 +58,9 @@ export default function LinksContainer({
}
export const LinksListContext = createContext<{
- selectedLinkIds: string[];
- setSelectedLinkIds: Dispatch<SetStateAction<string[]>>;
openMenuLinkId: string | null;
setOpenMenuLinkId: Dispatch<SetStateAction<string | null>>;
}>({
- selectedLinkIds: [],
- setSelectedLinkIds: () => {},
openMenuLinkId: null,
setOpenMenuLinkId: () => {},
});
@@ -80,7 +80,6 @@ function LinksList({
}) {
const searchParams = useSearchParams();
- const [selectedLinkIds, setSelectedLinkIds] = useState<string[]>([]);
const [openMenuLinkId, setOpenMenuLinkId] = useState<string | null>(null);
const isFiltered = [
@@ -93,31 +92,28 @@ function LinksList({
].some((param) => searchParams.has(param));
return (
- <LinksListContext.Provider
- value={{
- selectedLinkIds,
- setSelectedLinkIds,
- openMenuLinkId,
- setOpenMenuLinkId,
- }}
- >
+ <LinkSelectionProvider links={links}>
{!links || links.length ? (
- // Cards
- <CardList variant={compact ? "compact" : "loose"} loading={loading}>
- {links?.length
- ? // Link cards
- links.map((link) => <LinkCard key={link.id} link={link} />)
- : // Loading placeholder cards
- Array.from({ length: 12 }).map((_, idx) => (
- <CardList.Card
- key={idx}
- outerClassName="pointer-events-none"
- innerClassName="flex items-center gap-4"
- >
- <LinkCardPlaceholder />
- </CardList.Card>
- ))}
- </CardList>
+ <LinksListContext.Provider
+ value={{ openMenuLinkId, setOpenMenuLinkId }}
+ >
+ {/* Cards */}
+ <CardList variant={compact ? "compact" : "loose"} loading={loading}>
+ {links?.length
+ ? // Link cards
+ links.map((link) => <LinkCard key={link.id} link={link} />)
+ : // Loading placeholder cards
+ Array.from({ length: 12 }).map((_, idx) => (
+ <CardList.Card
+ key={idx}
+ outerClassName="pointer-events-none"
+ innerClassName="flex items-center gap-4"
+ >
+ <LinkCardPlaceholder />
+ </CardList.Card>
+ ))}
+ </CardList>
+ </LinksListContext.Provider>
) : (
<AnimatedEmptyState
title={isFiltered ? "No links found" : "No links yet"}
@@ -154,7 +150,7 @@ function LinksList({
linksCount={count ?? links?.length ?? 0}
/>
)}
- </LinksListContext.Provider>
+ </LinkSelectionProvider>
);
}
@@ -165,7 +161,7 @@ const FloatingToolbar = ({
loading: boolean;
linksCount: number;
}) => {
- const { selectedLinkIds, setSelectedLinkIds } = useContext(LinksListContext);
+ const { selectedLinkIds, setSelectedLinkIds } = useLinkSelection();
const { pagination, setPagination } = usePagination();
return (