2024-09-03 12:09:31 -05:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { InputTextarea } from 'primereact/inputtextarea';
|
2024-09-10 15:44:08 -05:00
|
|
|
import GenericButton from '@/components/buttons/GenericButton';
|
2024-09-03 12:09:31 -05:00
|
|
|
import { Panel } from 'primereact/panel';
|
2024-09-03 17:40:22 -05:00
|
|
|
import { useNDKContext } from "@/context/NDKContext";
|
|
|
|
import { NDKEvent } from "@nostr-dev-kit/ndk";
|
|
|
|
import { useToast } from '@/hooks/useToast';
|
2024-09-03 12:09:31 -05:00
|
|
|
|
2024-09-04 17:09:46 -05:00
|
|
|
const MessageInput = ({ onMessageSent }) => {
|
2024-09-03 12:09:31 -05:00
|
|
|
const [message, setMessage] = useState('');
|
2024-09-16 16:10:28 -05:00
|
|
|
const [collapsed, setCollapsed] = useState(true);
|
2024-10-13 14:23:10 -05:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
2024-09-03 17:40:22 -05:00
|
|
|
const { ndk, addSigner } = useNDKContext();
|
|
|
|
const { showToast } = useToast();
|
2024-09-10 15:44:08 -05:00
|
|
|
|
2024-09-03 17:40:22 -05:00
|
|
|
const handleSubmit = async () => {
|
2024-10-13 14:23:10 -05:00
|
|
|
if (!message.trim() || !ndk || isSubmitting) return;
|
|
|
|
|
|
|
|
setIsSubmitting(true);
|
2024-09-03 17:40:22 -05:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (!ndk.signer) {
|
|
|
|
await addSigner();
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = new NDKEvent(ndk);
|
|
|
|
event.kind = 1;
|
|
|
|
event.content = message;
|
|
|
|
event.tags = [['t', 'plebdevs']];
|
|
|
|
|
|
|
|
await event.publish();
|
|
|
|
showToast('success', 'Message Sent', 'Your message has been sent to the PlebDevs community.');
|
|
|
|
setMessage(''); // Clear the input after successful publish
|
|
|
|
onMessageSent(); // Call this function to close the accordion
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error publishing message:", error);
|
|
|
|
showToast('error', 'Error', 'There was an error sending your message. Please try again.');
|
2024-10-13 14:23:10 -05:00
|
|
|
} finally {
|
|
|
|
setIsSubmitting(false);
|
2024-09-03 17:40:22 -05:00
|
|
|
}
|
|
|
|
};
|
2024-09-03 12:09:31 -05:00
|
|
|
|
2024-09-10 15:44:08 -05:00
|
|
|
const headerTemplate = (options) => {
|
|
|
|
return (
|
|
|
|
<div className="flex align-items-center justify-content-between my-1 py-2">
|
2024-09-10 17:56:48 -05:00
|
|
|
<GenericButton outlined severity="primary" size="small" className="py-0" onClick={options.onTogglerClick} icon={options.collapsed ? 'pi pi-chevron-down' : 'pi pi-chevron-up'} />
|
|
|
|
<h2 className="m-0 ml-2">New Message</h2>
|
2024-09-10 15:44:08 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-09-03 12:09:31 -05:00
|
|
|
return (
|
2024-09-10 15:44:08 -05:00
|
|
|
<Panel
|
|
|
|
headerTemplate={headerTemplate}
|
|
|
|
toggleable
|
|
|
|
collapsed={collapsed}
|
|
|
|
onToggle={(e) => setCollapsed(e.value)}
|
|
|
|
className="w-full"
|
|
|
|
>
|
2024-09-03 12:09:31 -05:00
|
|
|
<div className="w-full flex flex-col">
|
|
|
|
<InputTextarea
|
|
|
|
value={message}
|
|
|
|
onChange={(e) => setMessage(e.target.value)}
|
2024-09-04 17:09:46 -05:00
|
|
|
rows={2}
|
|
|
|
cols={10}
|
2024-09-03 12:09:31 -05:00
|
|
|
autoResize
|
|
|
|
placeholder="Type your message here..."
|
|
|
|
className="w-full"
|
|
|
|
/>
|
2024-09-04 17:09:46 -05:00
|
|
|
</div>
|
|
|
|
<div className="w-full flex flex-row justify-end mt-4">
|
2024-09-10 15:44:08 -05:00
|
|
|
<GenericButton
|
2024-09-04 17:09:46 -05:00
|
|
|
label="Send"
|
|
|
|
icon="pi pi-send"
|
|
|
|
outlined
|
|
|
|
onClick={handleSubmit}
|
2024-09-10 15:44:08 -05:00
|
|
|
className="w-fit py-2"
|
2024-10-13 14:23:10 -05:00
|
|
|
disabled={isSubmitting}
|
2024-09-04 17:09:46 -05:00
|
|
|
/>
|
2024-09-03 12:09:31 -05:00
|
|
|
</div>
|
|
|
|
</Panel>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-10-13 14:23:10 -05:00
|
|
|
export default MessageInput;
|