2023-01-03 16:55:51 +00:00
|
|
|
import "./NoteCreator.css";
|
2022-12-29 22:23:41 +00:00
|
|
|
import { useState } from "react";
|
|
|
|
import useEventPublisher from "../feed/EventPublisher";
|
2023-01-03 16:55:51 +00:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faPaperclip } from "@fortawesome/free-solid-svg-icons";
|
|
|
|
import { openFile } from "../Util";
|
|
|
|
import VoidUpload from "../feed/VoidUpload";
|
|
|
|
import { FileExtensionRegex } from "../Const";
|
2022-12-29 22:23:41 +00:00
|
|
|
|
|
|
|
export function NoteCreator(props) {
|
|
|
|
const replyTo = props.replyTo;
|
|
|
|
const onSend = props.onSend;
|
2023-01-07 20:54:12 +00:00
|
|
|
const show = props.show || false;
|
2022-12-29 22:23:41 +00:00
|
|
|
const publisher = useEventPublisher();
|
|
|
|
const [note, setNote] = useState("");
|
2023-01-03 16:55:51 +00:00
|
|
|
const [error, setError] = useState("");
|
2022-12-29 22:23:41 +00:00
|
|
|
|
|
|
|
async function sendNote() {
|
|
|
|
let ev = replyTo ?
|
|
|
|
await publisher.reply(replyTo, note)
|
|
|
|
: await publisher.note(note);
|
|
|
|
|
|
|
|
console.debug("Sending note: ", ev);
|
|
|
|
publisher.broadcast(ev);
|
|
|
|
setNote("");
|
2023-01-03 16:55:51 +00:00
|
|
|
if (typeof onSend === "function") {
|
2022-12-29 22:23:41 +00:00
|
|
|
onSend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 16:55:51 +00:00
|
|
|
async function attachFile() {
|
2023-01-06 12:45:30 +00:00
|
|
|
try {
|
2023-01-07 20:54:12 +00:00
|
|
|
let file = await openFile();
|
|
|
|
let rsp = await VoidUpload(file);
|
|
|
|
let ext = file.name.match(FileExtensionRegex)[1];
|
2023-01-03 16:55:51 +00:00
|
|
|
|
2023-01-07 20:54:12 +00:00
|
|
|
// extension tricks note parser to embed the content
|
|
|
|
let url = rsp.metadata.url ?? `https://void.cat/d/${rsp.id}.${ext}`;
|
2023-01-03 16:55:51 +00:00
|
|
|
|
2023-01-07 20:54:12 +00:00
|
|
|
setNote(n => `${n}\n${url}`);
|
2023-01-06 12:45:30 +00:00
|
|
|
} catch (error) {
|
2023-01-07 20:54:12 +00:00
|
|
|
setError(error?.message)
|
2023-01-06 12:45:30 +00:00
|
|
|
}
|
2023-01-03 16:55:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-07 20:54:12 +00:00
|
|
|
if (!show) return false;
|
2022-12-29 22:23:41 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{replyTo ? <small>{`Reply to: ${replyTo.Id.substring(0, 8)}`}</small> : null}
|
2023-01-03 16:55:51 +00:00
|
|
|
<div className="flex note-creator">
|
2023-01-06 12:45:30 +00:00
|
|
|
<div className="textarea flex f-col mr10 f-grow">
|
|
|
|
<textarea className="textarea" placeholder="Say something!" value={note} onChange={(e) => setNote(e.target.value)} />
|
|
|
|
<div className="actions flex f-row">
|
|
|
|
<div className="attachment flex f-row">
|
|
|
|
{error.length > 0 ? <b className="error">{error}</b> : null}
|
2023-01-07 20:54:12 +00:00
|
|
|
<FontAwesomeIcon icon={faPaperclip} size="xl" onClick={(e) => attachFile()} />
|
2023-01-06 12:45:30 +00:00
|
|
|
</div>
|
|
|
|
<div className="btn" onClick={() => sendNote()}>Send</div>
|
2023-01-03 16:55:51 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-29 22:23:41 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|