snort/packages/app/src/Element/NoteCreator.tsx

240 lines
7.4 KiB
TypeScript
Raw Normal View History

2023-01-16 17:48:25 +00:00
import "./NoteCreator.css";
2023-03-27 22:58:29 +00:00
import { FormattedMessage, useIntl } from "react-intl";
import { useDispatch, useSelector } from "react-redux";
import { TaggedRawEvent } from "@snort/nostr";
2023-02-08 21:10:26 +00:00
2023-03-02 17:47:02 +00:00
import Icon from "Icons/Icon";
2023-01-20 11:11:50 +00:00
import useEventPublisher from "Feed/EventPublisher";
import { openFile } from "Util";
import Textarea from "Element/Textarea";
2023-01-25 18:08:53 +00:00
import Modal from "Element/Modal";
2023-02-01 22:14:30 +00:00
import ProfileImage from "Element/ProfileImage";
2023-01-31 19:08:11 +00:00
import useFileUpload from "Upload";
2023-03-31 22:43:07 +00:00
import Note from "Element/Note";
import {
setShow,
setNote,
setError,
setActive,
setPreview,
setShowAdvanced,
setZapForward,
setSensitive,
reset,
} from "State/NoteCreator";
import type { RootState } from "State/Store";
2023-03-27 22:58:29 +00:00
import { LNURL } from "LNURL";
2023-01-16 17:48:25 +00:00
2023-02-08 21:10:26 +00:00
import messages from "./messages";
2023-02-01 22:14:30 +00:00
interface NotePreviewProps {
2023-03-28 14:34:01 +00:00
note: TaggedRawEvent;
2023-02-01 22:14:30 +00:00
}
function NotePreview({ note }: NotePreviewProps) {
return (
<div className="note-preview">
2023-03-28 14:34:01 +00:00
<ProfileImage pubkey={note.pubkey} />
2023-02-01 22:14:30 +00:00
<div className="note-preview-body">
2023-03-28 14:34:01 +00:00
{note.content.slice(0, 136)}
{note.content.length > 140 && "..."}
2023-02-01 22:14:30 +00:00
</div>
</div>
);
2023-02-01 22:14:30 +00:00
}
export function NoteCreator() {
2023-03-27 22:58:29 +00:00
const { formatMessage } = useIntl();
2023-02-05 12:32:34 +00:00
const publisher = useEventPublisher();
const uploader = useFileUpload();
const note = useSelector((s: RootState) => s.noteCreator.note);
const show = useSelector((s: RootState) => s.noteCreator.show);
const error = useSelector((s: RootState) => s.noteCreator.error);
const active = useSelector((s: RootState) => s.noteCreator.active);
const preview = useSelector((s: RootState) => s.noteCreator.preview);
const replyTo = useSelector((s: RootState) => s.noteCreator.replyTo);
const showAdvanced = useSelector((s: RootState) => s.noteCreator.showAdvanced);
const zapForward = useSelector((s: RootState) => s.noteCreator.zapForward);
const sensitive = useSelector((s: RootState) => s.noteCreator.sensitive);
const dispatch = useDispatch();
2023-01-16 17:48:25 +00:00
2023-02-05 12:32:34 +00:00
async function sendNote() {
if (note) {
2023-03-27 22:58:29 +00:00
let extraTags: Array<Array<string>> | undefined;
if (zapForward) {
try {
const svc = new LNURL(zapForward);
await svc.load();
2023-04-05 10:58:26 +00:00
extraTags = [svc.getZapTag()];
2023-03-27 22:58:29 +00:00
} catch {
dispatch(
setError(
formatMessage({
defaultMessage: "Invalid LNURL",
})
)
2023-03-27 22:58:29 +00:00
);
return;
}
}
2023-04-06 22:12:51 +00:00
if (sensitive) {
extraTags ??= [];
extraTags.push(["content-warning", sensitive]);
}
2023-03-27 22:58:29 +00:00
const ev = replyTo ? await publisher.reply(replyTo, note, extraTags) : await publisher.note(note, extraTags);
2023-02-05 12:32:34 +00:00
console.debug("Sending note: ", ev);
publisher.broadcast(ev);
dispatch(reset());
2023-01-16 17:48:25 +00:00
}
2023-02-05 12:32:34 +00:00
}
2023-01-16 17:48:25 +00:00
2023-02-05 12:32:34 +00:00
async function attachFile() {
try {
2023-02-07 19:47:57 +00:00
const file = await openFile();
2023-02-05 12:32:34 +00:00
if (file) {
2023-02-07 19:47:57 +00:00
const rx = await uploader.upload(file, file.name);
2023-02-05 12:32:34 +00:00
if (rx.url) {
dispatch(setNote(`${note ? `${note}\n` : ""}${rx.url}`));
2023-02-05 12:32:34 +00:00
} else if (rx?.error) {
dispatch(setError(rx.error));
2023-01-16 17:48:25 +00:00
}
2023-02-05 12:32:34 +00:00
}
2023-02-07 19:47:57 +00:00
} catch (error: unknown) {
if (error instanceof Error) {
dispatch(setError(error?.message));
2023-02-07 19:47:57 +00:00
}
2023-01-16 17:48:25 +00:00
}
2023-02-05 12:32:34 +00:00
}
2023-01-16 17:48:25 +00:00
2023-02-07 19:47:57 +00:00
function onChange(ev: React.ChangeEvent<HTMLTextAreaElement>) {
const { value } = ev.target;
dispatch(setNote(value));
2023-02-05 12:32:34 +00:00
if (value) {
dispatch(setActive(true));
2023-02-05 12:32:34 +00:00
} else {
dispatch(setActive(false));
2023-01-16 17:48:25 +00:00
}
2023-02-05 12:32:34 +00:00
}
2023-01-16 17:48:25 +00:00
2023-02-07 19:47:57 +00:00
function cancel() {
dispatch(reset());
2023-02-05 12:32:34 +00:00
}
2023-01-25 18:08:53 +00:00
2023-02-05 12:32:34 +00:00
function onSubmit(ev: React.MouseEvent<HTMLButtonElement>) {
ev.stopPropagation();
sendNote().catch(console.warn);
}
2023-01-16 17:48:25 +00:00
2023-03-31 22:43:07 +00:00
async function loadPreview() {
if (preview) {
dispatch(setPreview(null));
2023-03-31 22:43:07 +00:00
} else {
const tmpNote = await publisher.note(note);
if (tmpNote) {
dispatch(setPreview(tmpNote));
2023-03-31 22:43:07 +00:00
}
}
}
function getPreviewNote() {
if (preview) {
return (
<Note
data={preview as TaggedRawEvent}
related={[]}
options={{
showFooter: false,
canClick: false,
}}
/>
);
}
}
2023-02-05 12:32:34 +00:00
return (
<>
{show && (
<Modal className="note-creator-modal" onClose={() => dispatch(setShow(false))}>
{replyTo && <NotePreview note={replyTo} />}
2023-03-31 22:43:07 +00:00
{preview && getPreviewNote()}
{!preview && (
<div className={`flex note-creator ${replyTo ? "note-reply" : ""}`}>
<div className="flex f-col mr10 f-grow">
<Textarea
autoFocus
2023-03-31 22:43:07 +00:00
className={`textarea ${active ? "textarea--focused" : ""}`}
onChange={onChange}
value={note}
onFocus={() => dispatch(setActive(true))}
2023-03-31 22:43:07 +00:00
/>
<button type="button" className="attachment" onClick={attachFile}>
<Icon name="attachment" />
</button>
</div>
{error && <span className="error">{error}</span>}
2023-01-16 17:48:25 +00:00
</div>
2023-03-31 22:43:07 +00:00
)}
2023-02-05 12:32:34 +00:00
<div className="note-creator-actions">
<button className="secondary" type="button" onClick={() => dispatch(setShowAdvanced(!showAdvanced))}>
2023-03-27 22:58:29 +00:00
<FormattedMessage defaultMessage="Advanced" />
</button>
2023-02-05 12:32:34 +00:00
<button className="secondary" type="button" onClick={cancel}>
2023-02-08 21:10:26 +00:00
<FormattedMessage {...messages.Cancel} />
2023-02-05 12:32:34 +00:00
</button>
<button type="button" onClick={onSubmit}>
2023-02-09 12:26:54 +00:00
{replyTo ? <FormattedMessage {...messages.Reply} /> : <FormattedMessage {...messages.Send} />}
2023-02-05 12:32:34 +00:00
</button>
</div>
2023-03-27 22:58:29 +00:00
{showAdvanced && (
2023-04-05 11:06:07 +00:00
<div>
<button className="secondary" type="button" onClick={loadPreview}>
<FormattedMessage defaultMessage="Toggle Preview" />
</button>
2023-03-27 22:58:29 +00:00
<h4>
<FormattedMessage defaultMessage="Forward Zaps" />
</h4>
<p>
<FormattedMessage defaultMessage="All zaps sent to this note will be received by the following LNURL" />
</p>
2023-04-06 22:12:51 +00:00
<b className="warning">
<FormattedMessage defaultMessage="Not all clients support this yet" />
</b>
2023-03-27 22:58:29 +00:00
<input
type="text"
className="w-max"
placeholder={formatMessage({
defaultMessage: "LNURL to forward zaps to",
})}
value={zapForward}
onChange={e => dispatch(setZapForward(e.target.value))}
2023-03-27 22:58:29 +00:00
/>
2023-04-06 22:12:51 +00:00
<h4>
<FormattedMessage defaultMessage="Sensitive Content" />
</h4>
<p>
<FormattedMessage defaultMessage="Users must accept the content warning to show the content of your note." />
</p>
<b className="warning">
<FormattedMessage defaultMessage="Not all clients support this yet" />
</b>
<div className="flex">
<input
className="w-max"
type="text"
value={sensitive}
onChange={e => dispatch(setSensitive(e.target.value))}
2023-04-06 22:12:51 +00:00
maxLength={50}
minLength={1}
placeholder={formatMessage({
defaultMessage: "Reason",
})}
/>
</div>
2023-04-05 11:06:07 +00:00
</div>
2023-03-27 22:58:29 +00:00
)}
2023-02-05 12:32:34 +00:00
</Modal>
)}
</>
);
2023-01-18 23:31:34 +00:00
}