src/components/ShareModal.jsx (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
import { useState } from 'react';
import { createDocument, friendlyErrorMessage, toAbsoluteUrl } from '../lib/api.js';
import { EXPIRY_OPTIONS, formatExpiryDate, STR } from '../lib/strings.js';
export default function ShareModal({ open, onClose, title, mode, content }) {
const [docTitle, setDocTitle] = useState(title);
const [expiresIn, setExpiresIn] = useState('7d');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [result, setResult] = useState(null);
const [copied, setCopied] = useState('');
if (!open) return null;
const handleSubmit = async (event) => {
event.preventDefault();
setLoading(true);
setError('');
setResult(null);
try {
const data = await createDocument({
title: docTitle,
mode,
content,
expiresIn,
});
setResult({
...data,
viewUrlAbs: toAbsoluteUrl(data.viewUrl),
editUrlAbs: toAbsoluteUrl(data.editUrl),
});
} catch (err) {
setError(friendlyErrorMessage(err));
} finally {
setLoading(false);
}
};
const copyLink = async (url, key) => {
try {
await navigator.clipboard.writeText(url);
setCopied(key);
window.setTimeout(() => setCopied(''), 2000);
} catch {
setError(STR.COPY_FAILED);
}
};
return (
<div className="modal-backdrop" role="presentation" onClick={onClose}>
<div
className="modal share-panel"
role="dialog"
aria-labelledby="share-title"
aria-modal="true"
onClick={(e) => e.stopPropagation()}
>
<h2 id="share-title" className="modal__title">
{STR.SHARE_DOCUMENT}
</h2>
{!result ? (
<form onSubmit={handleSubmit}>
<label className="share-field">
<span>{STR.TITLE}</span>
<input
type="text"
value={docTitle}
onChange={(e) => setDocTitle(e.target.value)}
maxLength={200}
/>
</label>
<fieldset className="share-field">
<legend>{STR.LINK_VALIDITY}</legend>
{EXPIRY_OPTIONS.map((opt) => (
<label key={opt.value} className="share-radio">
<input
type="radio"
name="expiresIn"
value={opt.value}
checked={expiresIn === opt.value}
onChange={() => setExpiresIn(opt.value)}
/>
{opt.label}
</label>
))}
</fieldset>
{error && (
<p className="share-error" role="alert">
{error}
</p>
)}
<div className="modal__actions">
<button type="button" className="btn btn-ghost" onClick={onClose}>
{STR.CANCEL}
</button>
<button type="submit" className="btn" disabled={loading}>
{loading ? STR.CREATING : STR.CREATE_LINKS}
</button>
</div>
</form>
) : (
<div className="share-result">
<p className="share-result__expiry">{formatExpiryDate(result.expiresAt)}</p>
<label className="share-field">
<span>{STR.VIEW_LINK}</span>
<div className="share-copy-row">
<input type="text" readOnly value={result.viewUrlAbs} />
<button
type="button"
className="btn"
onClick={() => copyLink(result.viewUrlAbs, 'view')}
>
{copied === 'view' ? STR.COPIED : STR.COPY}
</button>
</div>
</label>
<label className="share-field">
<span>{STR.EDIT_LINK}</span>
<div className="share-copy-row">
<input type="text" readOnly value={result.editUrlAbs} />
<button
type="button"
className="btn"
onClick={() => copyLink(result.editUrlAbs, 'edit')}
>
{copied === 'edit' ? STR.COPIED : STR.COPY}
</button>
</div>
</label>
<div className="modal__actions">
<button type="button" className="btn" onClick={onClose}>
{STR.CLOSE}
</button>
</div>
</div>
)}
</div>
</div>
);
}
|