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 folder permissions checks + keyboard shortcuts

Tim Wilsona2279ade0445
diff --git a/apps/web/ui/links/links-toolbar.tsx b/apps/web/ui/links/links-toolbar.tsx
index 65eafb56a9..865efcbda4 100644
--- a/apps/web/ui/links/links-toolbar.tsx
+++ b/apps/web/ui/links/links-toolbar.tsx
@@ -1,3 +1,4 @@
+import { useFolderPermissions } from "@/lib/swr/use-folder-permissions";
import useWorkspace from "@/lib/swr/use-workspace";
import {
BoxArchive,
@@ -11,11 +12,12 @@ import {
Tag,
TooltipContent,
Trash,
+ useKeyboardShortcut,
usePagination,
} from "@dub/ui";
import { cn } from "@dub/utils";
import { Command } from "cmdk";
-import { ReactNode, useState } from "react";
+import { memo, ReactNode, useContext, useMemo, useState } from "react";
import { useArchiveLinkModal } from "../modals/archive-link-modal";
import { useDeleteLinkModal } from "../modals/delete-link-modal";
import { useLinkConversionTrackingModal } from "../modals/link-conversion-tracking-modal";
@@ -24,200 +26,267 @@ import { useTagLinkModal } from "../modals/tag-link-modal";
import { ThreeDots, X } from "../shared/icons";
import ArchivedLinksHint from "./archived-links-hint";
import { useLinkSelection } from "./link-selection-provider";
-import { ResponseLink } from "./links-container";
+import { LinksListContext, ResponseLink } from "./links-container";
type BulkAction = {
label: string;
icon: Icon;
action: () => void;
disabledTooltip?: string | ReactNode;
+ keyboardShortcut?: string;
};
-export const LinksToolbar = ({
- loading,
- links,
- linksCount,
-}: {
- loading: boolean;
- links: ResponseLink[];
- linksCount: number;
-}) => {
- const { flags, slug, plan } = useWorkspace();
+export const LinksToolbar = memo(
+ ({
+ loading,
+ links,
+ linksCount,
+ }: {
+ loading: boolean;
+ links: ResponseLink[];
+ linksCount: number;
+ }) => {
+ const { flags, slug, plan } = useWorkspace();
+ const { folders } = useFolderPermissions();
+ const conversionsEnabled = !!plan && plan !== "free" && plan !== "pro";
- const conversionsEnabled = !!plan && plan !== "free" && plan !== "pro";
+ const { openMenuLinkId } = useContext(LinksListContext);
+ const { selectedLinkIds, setSelectedLinkIds } = useLinkSelection();
+ const { pagination, setPagination } = usePagination();
- const { selectedLinkIds, setSelectedLinkIds } = useLinkSelection();
- const { pagination, setPagination } = usePagination();
+ const selectedLinks = useMemo(
+ () => links.filter(({ id }) => selectedLinkIds.includes(id)),
+ [links, selectedLinkIds],
+ );
- const selectedLinks = links.filter(({ id }) => selectedLinkIds.includes(id));
+ const hasAllFolderPermissions = useMemo(() => {
+ if (!folders || !Array.isArray(folders)) return false;
- const { setShowTagLinkModal, TagLinkModal } = useTagLinkModal({
- props: selectedLinks,
- });
- const { setShowMoveLinkToFolderModal, MoveLinkToFolderModal } =
- useMoveLinkToFolderModal({
- links: selectedLinks,
+ return selectedLinks.every(
+ (link) =>
+ !link.folderId ||
+ folders
+ .find((folder) => folder.id === link.folderId)
+ ?.permissions.includes("folders.links.write"),
+ );
+ }, [selectedLinks, folders]);
+
+ const { setShowTagLinkModal, TagLinkModal } = useTagLinkModal({
+ props: selectedLinks,
+ });
+ const { setShowMoveLinkToFolderModal, MoveLinkToFolderModal } =
+ useMoveLinkToFolderModal({
+ links: selectedLinks,
+ });
+ const { setShowLinkConversionTrackingModal, LinkConversionTrackingModal } =
+ useLinkConversionTrackingModal({
+ props: selectedLinks,
+ });
+ const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
+ props: selectedLinks,
});
- const { setShowLinkConversionTrackingModal, LinkConversionTrackingModal } =
- useLinkConversionTrackingModal({
+ const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
props: selectedLinks,
});
- const { setShowArchiveLinkModal, ArchiveLinkModal } = useArchiveLinkModal({
- props: selectedLinks,
- });
- const { setShowDeleteLinkModal, DeleteLinkModal } = useDeleteLinkModal({
- props: selectedLinks,
- });
- const bulkActions: BulkAction[] = [
- {
- label: "Tags",
- icon: Tag,
- action: () => setShowTagLinkModal(true),
- },
- ...(flags?.linkFolders
- ? [
- {
- label: "Folder",
- icon: Folder,
- action: () => setShowMoveLinkToFolderModal(true),
- disabledTooltip:
- plan === "free" ? (
- <TooltipContent
- title="You can only use Link Folders on a Pro plan and above. Upgrade to Pro to continue."
- cta="Upgrade to Pro"
- href={`/${slug}/upgrade`}
- />
- ) : undefined,
- },
- ]
- : []),
- {
- label: "Conversion",
- icon: CircleDollar,
- action: () => setShowLinkConversionTrackingModal(true),
- disabledTooltip: conversionsEnabled ? undefined : (
- <TooltipContent
- title="Conversion tracking is only available on Business plans and above."
- cta="Upgrade to Business"
- href={slug ? `/${slug}/upgrade?exit=close` : "https://dub.co/pricing"}
- target="_blank"
- />
- ),
- },
- {
- label:
- selectedLinks.length && selectedLinks.every(({ archived }) => archived)
- ? "Unarchive"
- : "Archive",
- icon: BoxArchive,
- action: () => setShowArchiveLinkModal(true),
- },
- {
- label: "Delete",
- icon: Trash,
- action: () => setShowDeleteLinkModal(true),
- disabledTooltip: selectedLinks.some(({ programId }) => programId)
- ? "You can't delete a link that's part of a program."
- : undefined,
- },
- ];
+ const bulkActions: BulkAction[] = useMemo(
+ () => [
+ {
+ label: "Tags",
+ icon: Tag,
+ action: () => setShowTagLinkModal(true),
+ keyboardShortcut: "t",
+ },
+ ...(flags?.linkFolders
+ ? [
+ {
+ label: "Folder",
+ icon: Folder,
+ action: () => setShowMoveLinkToFolderModal(true),
+ disabledTooltip:
+ plan === "free" ? (
+ <TooltipContent
+ title="You can only use Link Folders on a Pro plan and above. Upgrade to Pro to continue."
+ cta="Upgrade to Pro"
+ href={`/${slug}/upgrade`}
+ />
+ ) : undefined,
+ keyboardShortcut: "m",
+ },
+ ]
+ : []),
+ {
+ label: "Conversion",
+ icon: CircleDollar,
+ action: () => setShowLinkConversionTrackingModal(true),
+ disabledTooltip: conversionsEnabled ? undefined : (
+ <TooltipContent
+ title="Conversion tracking is only available on Business plans and above."
+ cta="Upgrade to Business"
+ href={
+ slug ? `/${slug}/upgrade?exit=close` : "https://dub.co/pricing"
+ }
+ target="_blank"
+ />
+ ),
+ keyboardShortcut: "c",
+ },
+ {
+ label:
+ selectedLinks.length &&
+ selectedLinks.every(({ archived }) => archived)
+ ? "Unarchive"
+ : "Archive",
+ icon: BoxArchive,
+ action: () => setShowArchiveLinkModal(true),
+ keyboardShortcut: "a",
+ },
+ {
+ label: "Delete",
+ icon: Trash,
+ action: () => setShowDeleteLinkModal(true),
+ disabledTooltip: selectedLinks.some(({ programId }) => programId)
+ ? "You can't delete a link that's part of a program."
+ : undefined,
+ keyboardShortcut: "x",
+ },
+ ],
+ [plan, conversionsEnabled, selectedLinks],
+ );
- return (
- <>
- <TagLinkModal />
- <MoveLinkToFolderModal />
- <LinkConversionTrackingModal />
- <ArchiveLinkModal />
- <DeleteLinkModal />
+ useKeyboardShortcut(
+ bulkActions
+ .map(({ keyboardShortcut }) => keyboardShortcut)
+ .filter(Boolean) as string[],
+ (e) => {
+ const action = bulkActions.find(
+ ({ keyboardShortcut }) => keyboardShortcut === e.key,
+ );
+ if (action && !action.disabledTooltip && hasAllFolderPermissions)
+ action.action();
+ },
+ {
+ enabled: selectedLinkIds.length > 0 && openMenuLinkId === null,
+ priority: 1, // Take priority over individual link controls
+ modal: false,
+ },
+ );
- {/* Leave room at bottom of list */}
- <div className="h-[90px]" />
+ useKeyboardShortcut("Escape", () => setSelectedLinkIds([]), {
+ enabled: selectedLinkIds.length > 0,
+ priority: 2, // Take priority over clearing filters
+ modal: false,
+ });
- <div className="fixed bottom-4 left-0 w-full sm:max-[1330px]:w-[calc(100%-150px)] md:left-[240px] md:w-[calc(100%-240px)] md:max-[1330px]:w-[calc(100%-240px-150px)]">
- <div
- className={cn(
- "relative left-1/2 w-full max-w-[768px] -translate-x-1/2 px-5",
- "max-[1330px]:left-0 max-[1330px]:translate-x-0",
- )}
- >
- <div className="overflow-hidden rounded-xl border border-neutral-200 bg-white [filter:drop-shadow(0_5px_8px_#222A351d)]">
- <div
- className={cn(
- "relative px-4 py-3.5 transition-[opacity,transform] duration-100",
- selectedLinkIds.length > 0 &&
- "pointer-events-none absolute inset-0 translate-y-1/2 opacity-0",
- )}
- >
- <PaginationControls
- pagination={pagination}
- setPagination={setPagination}
- totalCount={linksCount}
- unit={(plural) => `${plural ? "links" : "link"}`}
- >
- {loading ? (
- <LoadingSpinner className="size-3.5" />
- ) : (
- <div className="hidden sm:block">
- <ArchivedLinksHint />
- </div>
- )}
- </PaginationControls>
- </div>
+ return (
+ <>
+ <TagLinkModal />
+ <MoveLinkToFolderModal />
+ <LinkConversionTrackingModal />
+ <ArchiveLinkModal />
+ <DeleteLinkModal />
- <div
- className={cn(
- "relative px-4 py-3.5 transition-[opacity,transform] duration-100",
- !selectedLinkIds.length &&
- "pointer-events-none absolute inset-0 translate-y-1/2 opacity-0",
- )}
- >
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2">
- <button
- type="button"
- onClick={() => setSelectedLinkIds([])}
- className="rounded-md p-1.5 transition-colors duration-75 hover:bg-neutral-50 active:bg-neutral-100"
- >
- <X className="size-4 text-neutral-900" />
- </button>
- <span className="text-sm font-medium text-neutral-600">
- <strong className="font-semibold">
- {selectedLinkIds.length}
- </strong>{" "}
- selected
- </span>
- </div>
+ {/* Leave room at bottom of list */}
+ <div className="h-[90px]" />
- {/* Large screen controls */}
- <div className="hidden items-center gap-2 min-[1040px]:flex">
- {bulkActions.map(
- ({ label, icon: Icon, action, disabledTooltip }) => (
- <Button
- type="button"
- variant="secondary"
- className="h-7 gap-1.5 px-2.5 text-xs"
- icon={<Icon className="size-3.5" />}
- text={label}
- onClick={action}
- disabledTooltip={disabledTooltip}
- />
- ),
+ <div className="fixed bottom-4 left-0 w-full sm:max-[1330px]:w-[calc(100%-150px)] md:left-[240px] md:w-[calc(100%-240px)] md:max-[1330px]:w-[calc(100%-240px-150px)]">
+ <div
+ className={cn(
+ "relative left-1/2 w-full max-w-[768px] -translate-x-1/2 px-5",
+ "max-[1330px]:left-0 max-[1330px]:translate-x-0",
+ )}
+ >
+ <div className="overflow-hidden rounded-xl border border-neutral-200 bg-white [filter:drop-shadow(0_5px_8px_#222A351d)]">
+ <div
+ className={cn(
+ "relative px-4 py-3.5 transition-[opacity,transform] duration-100",
+ selectedLinkIds.length > 0 &&
+ "pointer-events-none absolute inset-0 translate-y-1/2 opacity-0",
+ )}
+ >
+ <PaginationControls
+ pagination={pagination}
+ setPagination={setPagination}
+ totalCount={linksCount}
+ unit={(plural) => `${plural ? "links" : "link"}`}
+ >
+ {loading ? (
+ <LoadingSpinner className="size-3.5" />
+ ) : (
+ <div className="hidden sm:block">
+ <ArchivedLinksHint />
+ </div>
)}
- </div>
+ </PaginationControls>
+ </div>
+
+ <div
+ className={cn(
+ "relative px-4 py-3.5 transition-[opacity,transform] duration-100",
+ !selectedLinkIds.length &&
+ "pointer-events-none absolute inset-0 translate-y-1/2 opacity-0",
+ )}
+ >
+ <div className="flex items-center justify-between">
+ <div className="flex items-center gap-2">
+ <button
+ type="button"
+ onClick={() => setSelectedLinkIds([])}
+ className="rounded-md p-1.5 transition-colors duration-75 hover:bg-neutral-50 active:bg-neutral-100"
+ >
+ <X className="size-4 text-neutral-900" />
+ </button>
+ <span className="whitespace-nowrap text-sm font-medium text-neutral-600">
+ <strong className="font-semibold">
+ {selectedLinkIds.length}
+ </strong>{" "}
+ selected
+ </span>
+ </div>
+
+ {/* Large screen controls */}
+ <div className="hidden items-center gap-2 min-[1120px]:flex">
+ {bulkActions.map(
+ ({
+ label,
+ icon: Icon,
+ action,
+ disabledTooltip,
+ keyboardShortcut,
+ }) => (
+ <Button
+ type="button"
+ variant="secondary"
+ className="h-7 gap-1.5 pl-2.5 pr-1.5 text-xs"
+ icon={<Icon className="size-3.5" />}
+ text={label}
+ onClick={action}
+ disabledTooltip={
+ disabledTooltip ||
+ (!hasAllFolderPermissions
+ ? "You don't have permission to perform this action."
+ : undefined)
+ }
+ shortcut={keyboardShortcut?.toUpperCase()}
+ shortcutClassName="py-0 px-1 text-[0.625rem] leading-snug"
+ />
+ ),
+ )}
+ </div>
- {/* Small screen controls */}
- <div className="block min-[1040px]:hidden">
- <BulkActionMenu bulkActions={bulkActions} />
+ {/* Small screen controls */}
+ <div className="block min-[1120px]:hidden">
+ <BulkActionMenu bulkActions={bulkActions} />
+ </div>
</div>
</div>
</div>
</div>
</div>
- </div>
- </>
- );
-};
+ </>
+ );
+ },
+);
function BulkActionMenu({ bulkActions }: { bulkActions: BulkAction[] }) {
const [isOpen, setIsOpen] = useState(false);
diff --git a/apps/web/ui/modals/link-builder/tag-select.tsx b/apps/web/ui/modals/link-builder/tag-select.tsx
index 67daa70f03..221e93e276 100644
--- a/apps/web/ui/modals/link-builder/tag-select.tsx
+++ b/apps/web/ui/modals/link-builder/tag-select.tsx
@@ -102,7 +102,7 @@ export function TagSelect() {
[tags],
);
- useKeyboardShortcut("t", () => setIsOpen(true), { modal: true });
+ useKeyboardShortcut("t", () => setIsOpen(true), { modal: true, priority: 2 });
const [suggestedTags, setSuggestedTags] = useState<TagProps[]>([]);
diff --git a/packages/ui/src/button.tsx b/packages/ui/src/button.tsx
index dd22b7312b..a9d2bf39f3 100644
--- a/packages/ui/src/button.tsx
+++ b/packages/ui/src/button.tsx
@@ -32,6 +32,7 @@ export interface ButtonProps
VariantProps<typeof buttonVariants> {
text?: ReactNode | string;
textWrapperClassName?: string;
+ shortcutClassName?: string;
loading?: boolean;
icon?: ReactNode;
shortcut?: string;
@@ -46,6 +47,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
variant = "primary",
className,
textWrapperClassName,
+ shortcutClassName,
loading,
icon,
shortcut,
@@ -87,6 +89,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
{
"bg-neutral-100": variant?.endsWith("outline"),
},
+ shortcutClassName,
)}
>
{shortcut}
@@ -137,6 +140,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
"bg-red-100 text-red-600 group-hover:bg-red-500 group-hover:text-white":
variant === "danger-outline",
},
+ shortcutClassName,
)}
>
{shortcut}
diff --git a/packages/ui/src/hooks/use-keyboard-shortcut.tsx b/packages/ui/src/hooks/use-keyboard-shortcut.tsx
index f799f493a6..c1b28261f4 100644
--- a/packages/ui/src/hooks/use-keyboard-shortcut.tsx
+++ b/packages/ui/src/hooks/use-keyboard-shortcut.tsx
@@ -82,7 +82,7 @@ export function useKeyboardShortcut(
const topListener = stableSort(
matchingListeners,
(a, b) => (b.priority ?? 0) - (a.priority ?? 0),
- ).slice(-1)[0];
+ )[0];
// Check if this is the top listener
if (topListener.id !== id) return;
@@ -90,7 +90,7 @@ export function useKeyboardShortcut(
e.preventDefault();
callback(e);
},
- [key, listeners, id, callback],
+ [key, listeners, id, callback, options.modal],
);
useEffect(() => {