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 prompt modal for image URLs

Tim Wilsond390b1ef4eed
diff --git a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
index 57bd7c3714..75db54e2e9 100644
--- a/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/og-section.tsx
@@ -20,6 +20,7 @@ import { Link2 } from "lucide-react";
import { Dispatch, SetStateAction, useEffect, useState } from "react";
import TextareaAutosize from "react-textarea-autosize";
import { toast } from "sonner";
+import { usePromptModal } from "../prompt-modal";
import UnsplashSearch from "./unsplash-search";
export default function OGSection({
@@ -35,6 +36,19 @@ export default function OGSection({
}) {
const { id: workspaceId, exceededAI, mutate } = useWorkspace();
+ const { setShowPromptModal, PromptModal } = usePromptModal({
+ title: "Choose image from URL",
+ description:
+ "Paste an image URL to use for your link's social media cards.",
+ label: "Image URL",
+ inputProps: {
+ placeholder: "https://example.com/og.png",
+ },
+ onSubmit: (image) => {
+ if (image) setData((prev) => ({ ...prev, image }));
+ },
+ });
+
const { title, description, image, proxy } = data;
const {
@@ -202,15 +216,7 @@ export default function OGSection({
<div className="flex items-center justify-between">
<ButtonTooltip
tooltipContent="Paste a URL to an image."
- onClick={() => {
- const image = window.prompt(
- "Paste a URL to an image.",
- "https://",
- );
- if (image) {
- setData((prev) => ({ ...prev, image }));
- }
- }}
+ onClick={() => setShowPromptModal(true)}
className="flex h-6 w-6 items-center justify-center rounded-md text-gray-500 transition-colors duration-75 hover:bg-gray-100 active:bg-gray-200 disabled:cursor-not-allowed"
>
<Link2 className="h-4 w-4 text-gray-500" />
@@ -334,6 +340,7 @@ export default function OGSection({
</div>
</motion.div>
)}
+ <PromptModal />
</div>
);
}
diff --git a/apps/web/ui/modals/prompt-modal.tsx b/apps/web/ui/modals/prompt-modal.tsx
new file mode 100644
index 0000000000..2f1cf86419
--- /dev/null
+++ b/apps/web/ui/modals/prompt-modal.tsx
@@ -0,0 +1,107 @@
+import { Button, ButtonProps, Modal, useMediaQuery } from "@dub/ui";
+import {
+ Dispatch,
+ HTMLProps,
+ SetStateAction,
+ useCallback,
+ useMemo,
+ useState,
+} from "react";
+
+type PromptModelProps = {
+ title: string;
+ label: string;
+ description?: string;
+ onSubmit?: (value: string) => Promise<void> | void;
+ inputProps?: HTMLProps<HTMLInputElement>;
+ buttonProps?: ButtonProps;
+};
+
+/**
+ * A generic prompt modal for text input
+ */
+function PromptModal({
+ showPromptModal,
+ setShowPromptModal,
+ title,
+ label,
+ description,
+ onSubmit,
+ inputProps,
+ buttonProps,
+}: {
+ showPromptModal: boolean;
+ setShowPromptModal: Dispatch<SetStateAction<boolean>>;
+} & PromptModelProps) {
+ const { isMobile } = useMediaQuery();
+
+ const [value, setValue] = useState("");
+ const [loading, setLoading] = useState(false);
+
+ return (
+ <Modal showModal={showPromptModal} setShowModal={setShowPromptModal}>
+ <div className="flex flex-col items-center justify-center border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16">
+ <h3 className="mb-3 text-lg font-medium">{title}</h3>
+ {description && <p className="text-sm text-gray-500">{description}</p>}
+ </div>
+
+ <form
+ onSubmit={async (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+
+ setLoading(true);
+ await onSubmit?.(value);
+ setLoading(false);
+ setShowPromptModal(false);
+ }}
+ className="flex flex-col space-y-3 bg-gray-50 px-4 py-8 text-left sm:px-16"
+ >
+ <label className="block">
+ <p className="text-sm text-gray-700">{label}</p>
+ <div className="relative mt-1 rounded-md shadow-sm">
+ <input
+ type="text"
+ value={value}
+ onChange={(e) => setValue(e.target.value)}
+ required
+ autoFocus={!isMobile}
+ autoComplete="off"
+ className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
+ {...inputProps}
+ />
+ </div>
+ </label>
+
+ <Button
+ variant="primary"
+ text="Submit"
+ loading={loading}
+ {...buttonProps}
+ />
+ </form>
+ </Modal>
+ );
+}
+
+export function usePromptModal(props: PromptModelProps) {
+ const [showPromptModal, setShowPromptModal] = useState(false);
+
+ const PromptModalCallback = useCallback(() => {
+ return props ? (
+ <PromptModal
+ showPromptModal={showPromptModal}
+ setShowPromptModal={setShowPromptModal}
+ {...props}
+ />
+ ) : null;
+ }, [showPromptModal, setShowPromptModal]);
+
+ return useMemo(
+ () => ({
+ setShowPromptModal,
+ PromptModal: PromptModalCallback,
+ }),
+ [setShowPromptModal, PromptModalCallback],
+ );
+}
diff --git a/packages/ui/src/image-drop.tsx b/packages/ui/src/image-drop.tsx
index c9e7875491..267e0c855b 100644
--- a/packages/ui/src/image-drop.tsx
+++ b/packages/ui/src/image-drop.tsx
@@ -66,7 +66,7 @@ export function ImageDrop({
)}
>
{loading && (
- <div className="absolute inset-0 z-[5] flex items-center justify-center bg-white">
+ <div className="absolute inset-0 z-[5] flex items-center justify-center rounded-[inherit] bg-white">
<LoadingCircle />
</div>
)}