MicProtocol / apps /web /src /components /ui /documents /DocumentShareDialog.tsx
Benaah's picture
Upload folder using huggingface_hub
fb12607 verified
Raw History Blame Contribute Delete
7.42 kB
"use client";
import { useCallback, useMemo, useState } from "react";
import {
Share2,
Link,
Clock,
ShieldCheck,
Copy,
CheckCircle,
AlertCircle,
} from "lucide-react";
import { trpc } from "@/lib/trpc";
import { useToast } from "@/lib/toast";
import { Button } from "@micprotocol/ui";
import { Dialog, DialogTrigger, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog";
import { Input } from "@micprotocol/ui";
import { SectionHeader } from "@micprotocol/ui";
import { ShareType } from "@micprotocol/shared-types";
interface DocumentShareDialogProps {
documentId: string;
documentTitle: string;
workspaceId: string;
open: boolean;
onOpenChange: (open: boolean) => void;
onShareCreated?: () => void;
}
export default function DocumentShareDialog({
documentId,
documentTitle,
workspaceId,
open,
onOpenChange,
onShareCreated,
}: DocumentShareDialogProps) {
const [shareType, setShareType] = useState<ShareType>(ShareType.PUBLIC);
const [accessCode, setAccessCode] = useState("");
const [expiresAt, setExpiresAt] = useState("");
const [maxDownloads, setMaxDownloads] = useState("");
const [isCreating, setIsCreating] = useState(false);
const [shareUrl, setShareUrl] = useState<string | null>(null);
const { success, error } = useToast();
const createShare = trpc.documents.createShare.useMutation({
onSuccess: (data) => {
setIsCreating(false);
setShareUrl(`/documents/shared/${data.id}`);
success("Share link created");
onShareCreated?.();
},
onError: (err) => {
setIsCreating(false);
error(err.message || "Failed to create share link");
},
});
const handleCreate = async () => {
setIsCreating(true);
try {
await createShare.mutateAsync({
documentId,
workspaceId,
shareType,
accessCode: accessCode || undefined,
expiresAt: expiresAt || undefined,
maxDownloads: maxDownloads ? parseInt(maxDownloads, 10) : undefined,
});
} catch (err) {
setIsCreating(false);
error(err instanceof Error ? err.message : "Failed to create share");
}
};
const handleCopy = () => {
if (!shareUrl) return;
navigator.clipboard.writeText(shareUrl);
success("Share link copied to clipboard");
};
const typeLabels: Record<ShareType, { label: string; description: string }> = {
[ShareType.PUBLIC]: { label: "Public", description: "Anyone with the link can access" },
[ShareType.PROTECTED]: { label: "Protected", description: "Requires an access code" },
[ShareType.PRIVATE]: { label: "Private", description: "Only workspace members" },
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogTrigger asChild>
<Button variant="outline" size="sm">
<Share2 size={14} /> Create Share Link
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Create Share Link</DialogTitle>
<DialogDescription>Share &quot;{documentTitle}&quot; with controlled access</DialogDescription>
</DialogHeader>
<div style={{ display: "flex", flexDirection: "column", gap: "var(--space-4)" }}>
<div>
<label style={{ display: "block", fontSize: "var(--text-xs)", color: "var(--text-tertiary)", marginBottom: "var(--space-2)", fontWeight: 600 }}>
Share Type
</label>
<div style={{ display: "flex", gap: "var(--space-2)" }}>
{Object.values(ShareType).map((type) => {
const info = typeLabels[type];
const selected = shareType === type;
return (
<button
key={type}
onClick={() => setShareType(type)}
style={{
flex: 1,
padding: "var(--space-3)",
border: selected ? "1px solid var(--color-primary)" : "1px solid var(--border-color)",
background: selected ? "color-mix(in srgb, var(--color-primary) 10%, transparent)" : "var(--bg-secondary)",
borderRadius: "var(--radius-md)",
fontSize: "var(--text-xs)",
fontWeight: 600,
color: selected ? "var(--color-primary)" : "var(--text-primary)",
textAlign: "left" as const,
cursor: "pointer",
}}
>
<div style={{ fontSize: "var(--text-xs)", color: "var(--text-secondary)", marginTop: 2 }}>{info.label}</div>
<div style={{ fontSize: "var(--text-xs)", color: selected ? "var(--color-primary)" : "var(--text-tertiary)" }}>{info.description}</div>
</button>
);
})}
</div>
</div>
{shareType === ShareType.PROTECTED && (
<div>
<label style={{ display: "block", fontSize: "var(--text-xs)", color: "var(--text-tertiary)", marginBottom: "var(--space-2)", fontWeight: 600 }}>
Access Code
</label>
<Input
value={accessCode}
onChange={(e) => setAccessCode(e.target.value)}
placeholder="Enter access code"
className="input"
/>
</div>
)}
<div>
<label style={{ display: "block", fontSize: "var(--text-xs)", color: "var(--text-tertiary)", marginBottom: "var(--space-2)", fontWeight: 600 }}>
Expires At
</label>
<Input
type="datetime-local"
value={expiresAt}
onChange={(e) => setExpiresAt(e.target.value)}
className="input"
/>
</div>
<div>
<label style={{ display: "block", fontSize: "var(--text-xs)", color: "var(--text-tertiary)", marginBottom: "var(--space-2)", fontWeight: 600 }}>
Max Downloads
</label>
<Input
type="number"
value={maxDownloads}
onChange={(e) => setMaxDownloads(e.target.value)}
placeholder="Unlimited"
className="input"
/>
</div>
</div>
{shareUrl && (
<div style={{ padding: "var(--space-3)", background: "color-mix(in srgb, var(--color-success) 8%, transparent)", border: "1px solid color-mix(in srgb, var(--color-success) 30%, transparent)", borderRadius: "var(--radius-md)", fontSize: "var(--text-sm)", color: "var(--color-success)" }}>
<CheckCircle size={14} style={{ marginRight: "var(--space-2)", verticalAlign: "middle" }} />
Share link ready: {shareUrl}
</div>
)}
<div style={{ display: "flex", gap: "var(--space-3)", justifyContent: "flex-end" }}>
<Button variant="outline" size="sm" onClick={() => onOpenChange(false)} disabled={isCreating}>
Cancel
</Button>
<Button
variant="primary"
size="sm"
onClick={handleCreate}
disabled={isCreating}
loading={isCreating}
>
{isCreating ? "Creating..." : "Create Share"}
</Button>
</div>
</DialogContent>
</Dialog>
);
}