snort/src/Element/NoteCreator.tsx

142 lines
3.8 KiB
TypeScript
Raw Normal View History

2023-01-16 17:48:25 +00:00
import "./NoteCreator.css";
2023-02-01 22:14:30 +00:00
import { useState } from "react";
2023-02-08 21:10:26 +00:00
import { FormattedMessage } from "react-intl";
2023-02-01 22:14:30 +00:00
import Attachment from "Icons/Attachment";
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-25 18:08:53 +00:00
import { default as NEvent } from "Nostr/Event";
2023-01-31 19:08:11 +00:00
import useFileUpload from "Upload";
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 {
note: NEvent;
2023-02-01 22:14:30 +00:00
}
function NotePreview({ note }: NotePreviewProps) {
return (
<div className="note-preview">
<ProfileImage pubkey={note.PubKey} />
<div className="note-preview-body">
{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
}
2023-01-16 17:48:25 +00:00
export interface NoteCreatorProps {
show: boolean;
setShow: (s: boolean) => void;
replyTo?: NEvent;
onSend?: Function;
autoFocus: boolean;
2023-01-16 17:48:25 +00:00
}
export function NoteCreator(props: NoteCreatorProps) {
const { show, setShow, replyTo, onSend, autoFocus } = props;
2023-02-05 12:32:34 +00:00
const publisher = useEventPublisher();
const [note, setNote] = useState<string>();
const [error, setError] = useState<string>();
const [active, setActive] = useState<boolean>(false);
const uploader = useFileUpload();
const hasErrors = (error?.length ?? 0) > 0;
2023-01-16 17:48:25 +00:00
2023-02-05 12:32:34 +00:00
async function sendNote() {
if (note) {
let ev = replyTo
? await publisher.reply(replyTo, note)
: await publisher.note(note);
2023-02-05 12:32:34 +00:00
console.debug("Sending note: ", ev);
publisher.broadcast(ev);
setNote("");
setShow(false);
if (typeof onSend === "function") {
onSend();
}
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-05 12:32:34 +00:00
async function attachFile() {
try {
let file = await openFile();
if (file) {
let rx = await uploader.upload(file, file.name);
if (rx.url) {
setNote((n) => `${n ? `${n}\n` : ""}${rx.url}`);
2023-02-05 12:32:34 +00:00
} else if (rx?.error) {
setError(rx.error);
2023-01-16 17:48:25 +00:00
}
2023-02-05 12:32:34 +00:00
}
} catch (error: any) {
setError(error?.message);
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
function onChange(ev: any) {
const { value } = ev.target;
setNote(value);
2023-02-05 12:32:34 +00:00
if (value) {
setActive(true);
2023-02-05 12:32:34 +00:00
} else {
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-05 12:32:34 +00:00
function cancel(ev: any) {
setShow(false);
setNote("");
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-02-05 12:32:34 +00:00
return (
<>
{show && (
<Modal className="note-creator-modal" onClose={() => setShow(false)}>
{replyTo && <NotePreview note={replyTo} />}
<div className={`flex note-creator ${replyTo ? "note-reply" : ""}`}>
2023-02-05 12:32:34 +00:00
<div className="flex f-col mr10 f-grow">
<Textarea
autoFocus={autoFocus}
className={`textarea ${active ? "textarea--focused" : ""}`}
onChange={onChange}
value={note}
onFocus={() => setActive(true)}
/>
<button
type="button"
className="attachment"
onClick={(e) => attachFile()}
>
2023-02-05 12:32:34 +00:00
<Attachment />
</button>
2023-01-16 17:48:25 +00:00
</div>
2023-02-05 12:32:34 +00:00
{hasErrors && <span className="error">{error}</span>}
</div>
<div className="note-creator-actions">
<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-08 21:10:26 +00:00
{replyTo ? (
<FormattedMessage {...messages.Reply} />
) : (
<FormattedMessage {...messages.Send} />
)}
2023-02-05 12:32:34 +00:00
</button>
</div>
</Modal>
)}
</>
);
2023-01-18 23:31:34 +00:00
}