2024-09-03 12:09:31 -05:00
|
|
|
import React, { useState } from 'react';
|
|
|
|
import { InputTextarea } from 'primereact/inputtextarea';
|
|
|
|
import { Button } from 'primereact/button';
|
|
|
|
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-03 17:40:22 -05:00
|
|
|
const MessageInput = ({ collapsed, onToggle, onMessageSent }) => {
|
2024-09-03 12:09:31 -05:00
|
|
|
const [message, setMessage] = useState('');
|
2024-09-03 17:40:22 -05:00
|
|
|
const { ndk, addSigner } = useNDKContext();
|
|
|
|
const { showToast } = useToast();
|
|
|
|
const handleSubmit = async () => {
|
|
|
|
if (!message.trim() || !ndk) return;
|
|
|
|
|
|
|
|
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-09-03 12:09:31 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Panel header={null} toggleable collapsed={collapsed} onToggle={onToggle} className="w-full" pt={{
|
|
|
|
header: {
|
|
|
|
className: 'bg-transparent',
|
|
|
|
border: 'none',
|
|
|
|
},
|
|
|
|
toggler: {
|
|
|
|
className: 'bg-transparent',
|
|
|
|
border: 'none',
|
|
|
|
},
|
|
|
|
togglerIcon: {
|
|
|
|
display: 'none',
|
|
|
|
},
|
|
|
|
}}>
|
|
|
|
<div className="w-full flex flex-col">
|
|
|
|
<InputTextarea
|
|
|
|
value={message}
|
|
|
|
onChange={(e) => setMessage(e.target.value)}
|
|
|
|
rows={5}
|
|
|
|
cols={30}
|
|
|
|
autoResize
|
|
|
|
placeholder="Type your message here..."
|
|
|
|
className="w-full"
|
|
|
|
/>
|
|
|
|
<div className="w-full flex flex-row justify-end">
|
|
|
|
<Button
|
|
|
|
label="Send"
|
|
|
|
icon="pi pi-send"
|
2024-09-03 17:02:24 -05:00
|
|
|
outlined
|
2024-09-03 12:09:31 -05:00
|
|
|
className='mt-2'
|
2024-09-03 17:40:22 -05:00
|
|
|
onClick={handleSubmit}
|
2024-09-03 12:09:31 -05:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Panel>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MessageInput;
|