plebdevs/src/components/feeds/MessageInput.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

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';
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('');
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 (
2024-09-04 17:09:46 -05:00
<Panel header={null} toggleable collapsed={false} className="w-full" pt={{
2024-09-03 12:09:31 -05:00
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)}
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">
<Button
label="Send"
icon="pi pi-send"
outlined
onClick={handleSubmit}
/>
2024-09-03 12:09:31 -05:00
</div>
</Panel>
);
};
export default MessageInput;