feat: delete torrent

This commit is contained in:
Kieran 2023-11-27 14:39:58 +00:00
parent d36d890dad
commit 1d16d61ea1
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 29 additions and 12 deletions

View File

@ -1,5 +1,7 @@
import { ExternalStore } from "@snort/shared";
import { useSyncExternalStore } from "react";
import { EventPublisher, Nip7Signer } from "@snort/system";
import { SnortContext } from "@snort/system-react";
import { useContext, useSyncExternalStore } from "react";
export interface LoginSession {
publicKey: string;
@ -35,8 +37,14 @@ class LoginStore extends ExternalStore<LoginSession | undefined> {
export const LoginState = new LoginStore();
export function useLogin() {
return useSyncExternalStore(
const session = useSyncExternalStore(
(c) => LoginState.hook(c),
() => LoginState.snapshot(),
);
const system = useContext(SnortContext);
return session ? {
...session,
builder: new EventPublisher(new Nip7Signer(), session.publicKey),
system
} : undefined;
}

View File

@ -1,13 +1,11 @@
import { ReactNode, useContext, useState } from "react";
import { ReactNode, useState } from "react";
import { Categories, Category, TorrentKind } from "../const";
import { Button } from "../element/button";
import { EventPublisher, Nip7Signer } from "@snort/system";
import { useLogin } from "../login";
import { dedupe } from "@snort/shared";
import * as bencode from "../bencode";
import { sha1 } from "@noble/hashes/sha1";
import { bytesToHex } from "@noble/hashes/utils";
import { SnortContext } from "@snort/system-react";
import { useNavigate } from "react-router-dom";
async function openFile(): Promise<File | undefined> {
@ -45,7 +43,6 @@ async function openFile(): Promise<File | undefined> {
export function NewPage() {
const login = useLogin();
const system = useContext(SnortContext);
const navigate = useNavigate();
const [obj, setObj] = useState({
@ -88,10 +85,7 @@ export function NewPage() {
async function publish() {
if (!login) return;
const signer = new Nip7Signer();
const builder = new EventPublisher(signer, login.publicKey);
const ev = await builder.generic((eb) => {
const ev = await login.builder.generic((eb) => {
const v = eb
.kind(TorrentKind)
.content(obj.desc)
@ -107,7 +101,7 @@ export function NewPage() {
console.debug(ev);
if (ev) {
await system.BroadcastEvent(ev);
await login.system.BroadcastEvent(ev);
}
navigate("/")
}

View File

@ -1,10 +1,12 @@
import { unwrap } from "@snort/shared";
import { NoteCollection, RequestBuilder, TaggedNostrEvent, parseNostrLink } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useLocation, useParams } from "react-router-dom";
import { useLocation, useNavigate, useParams } from "react-router-dom";
import { FormatBytes, TorrentKind } from "../const";
import { ProfileImage } from "../element/profile-image";
import { MagnetLink } from "../element/magnet";
import { useLogin } from "../login";
import { Button } from "../element/button";
export function TorrentPage() {
const location = useLocation();
@ -24,11 +26,21 @@ export function TorrentPage() {
}
export function TorrentDetail({ item }: { item: TaggedNostrEvent }) {
const login = useLogin();
const navigate = useNavigate();
const name = item.tags.find((a) => a[0] === "title")?.at(1);
const size = Number(item.tags.find((a) => a[0] === "size")?.at(1));
const files = item.tags.filter(a => a[0] === "file");
const tags = item.tags.filter(a => a[0] === "t").map(a => a[1]);
async function deleteTorrent() {
const ev = await login?.builder?.delete(item.id);
if (ev) {
await login?.system.BroadcastEvent(ev);
navigate(-1);
}
}
return (
<div className="flex flex-col gap-2">
<div className="flex gap-2 items-center text-xl">
@ -57,6 +69,9 @@ export function TorrentDetail({ item }: { item: TaggedNostrEvent }) {
<small className="text-slate-500 font-semibold">{FormatBytes(Number(a[2]))}</small>
</div>)}
</div>
{item.pubkey == login?.publicKey && <Button className="bg-red-600 hover:bg-red-800" onClick={deleteTorrent}>
Delete
</Button>}
</div>
);
}