2023-09-03 13:04:25 +00:00
|
|
|
import CardContainer from './CardContainer';
|
2023-09-14 13:44:13 +00:00
|
|
|
import { ArrowUpTrayIcon } from '@heroicons/react/24/outline';
|
2023-09-14 14:48:09 +00:00
|
|
|
import { useState } from 'react';
|
|
|
|
import { generatePrivateKey, getPublicKey, finishEvent, relayInit} from 'nostr-tools';
|
2023-09-15 07:06:37 +00:00
|
|
|
import { minePow } from '../../utils/mine';
|
2023-09-14 14:48:09 +00:00
|
|
|
|
|
|
|
const difficulty = 10
|
2023-09-03 13:04:25 +00:00
|
|
|
|
2023-09-15 07:06:37 +00:00
|
|
|
export const relay = relayInit('wss://nostr.lu.ke')
|
|
|
|
|
2023-09-03 13:04:25 +00:00
|
|
|
const NewThreadCard = () => {
|
2023-09-14 14:48:09 +00:00
|
|
|
const [comment, setComment] = useState("");
|
|
|
|
|
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
let sk = generatePrivateKey()
|
|
|
|
let pk = getPublicKey(sk)
|
|
|
|
|
|
|
|
relay.on('connect', () => {
|
|
|
|
console.log(`connected to ${relay.url}`)
|
|
|
|
})
|
|
|
|
relay.on('error', () => {
|
|
|
|
console.log(`failed to connect to ${relay.url}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
await relay.connect()
|
|
|
|
|
|
|
|
try {
|
|
|
|
const event = minePow({
|
|
|
|
kind: 1,
|
|
|
|
tags: [],
|
|
|
|
content: 'Hello, world!',
|
|
|
|
created_at: Math.floor(Date.now() / 1000), //needs to be date To Unix
|
|
|
|
pubkey: pk,
|
|
|
|
}, difficulty)
|
|
|
|
|
|
|
|
const signedEvent = finishEvent(event, sk)
|
|
|
|
await relay.publish(signedEvent)
|
|
|
|
console.log(signedEvent.id)
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
setComment(comment + " " + error);
|
|
|
|
}
|
|
|
|
relay.close()
|
|
|
|
};
|
|
|
|
|
|
|
|
// async function attachFile(file_input: File | null) {
|
|
|
|
// try {
|
|
|
|
// if (file_input) {
|
|
|
|
// const rx = await NostrImg(file_input);
|
|
|
|
// if (rx.url) {
|
|
|
|
// setComment(comment + " " + rx.url);
|
|
|
|
// } else if (rx?.error) {
|
|
|
|
// setComment(comment + " " + rx.error);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// } catch (error: unknown) {
|
|
|
|
// if (error instanceof Error) {
|
|
|
|
// setComment(comment + " " + error?.message);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-09-03 13:04:25 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<CardContainer>
|
2023-09-14 13:44:13 +00:00
|
|
|
<form
|
|
|
|
name="post"
|
|
|
|
method="post"
|
|
|
|
encType="multipart/form-data"
|
|
|
|
className=""
|
2023-09-14 14:48:09 +00:00
|
|
|
onSubmit={handleSubmit}
|
2023-09-14 13:44:13 +00:00
|
|
|
>
|
|
|
|
<input type="hidden" name="MAX_FILE_SIZE" defaultValue={4194304} />
|
|
|
|
<div id="togglePostFormLink" className="text-lg font-semibold">
|
|
|
|
Start a New Thread
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<textarea
|
|
|
|
name="com"
|
|
|
|
wrap="soft"
|
|
|
|
className="w-full p-2 rounded bg-gradient-to-r from-blue-900 to-cyan-500 text-white border-none"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="flex justify-between items-center">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<ArrowUpTrayIcon className="h-6 w-6 text-white" />
|
|
|
|
<input type="file" className="hidden" />
|
|
|
|
</div>
|
|
|
|
<button type="submit" className="px-4 py-2 bg-gradient-to-r from-cyan-900 to-blue-500 rounded text-white font-semibold">
|
|
|
|
Submit
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div id="postFormError" className="text-red-500" />
|
|
|
|
</form>
|
2023-09-03 13:04:25 +00:00
|
|
|
</CardContainer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-14 13:44:13 +00:00
|
|
|
|
2023-09-03 13:04:25 +00:00
|
|
|
export default NewThreadCard;
|