feat: delete torrent
This commit is contained in:
parent
d36d890dad
commit
1d16d61ea1
@ -1,5 +1,7 @@
|
|||||||
import { ExternalStore } from "@snort/shared";
|
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 {
|
export interface LoginSession {
|
||||||
publicKey: string;
|
publicKey: string;
|
||||||
@ -35,8 +37,14 @@ class LoginStore extends ExternalStore<LoginSession | undefined> {
|
|||||||
export const LoginState = new LoginStore();
|
export const LoginState = new LoginStore();
|
||||||
|
|
||||||
export function useLogin() {
|
export function useLogin() {
|
||||||
return useSyncExternalStore(
|
const session = useSyncExternalStore(
|
||||||
(c) => LoginState.hook(c),
|
(c) => LoginState.hook(c),
|
||||||
() => LoginState.snapshot(),
|
() => LoginState.snapshot(),
|
||||||
);
|
);
|
||||||
|
const system = useContext(SnortContext);
|
||||||
|
return session ? {
|
||||||
|
...session,
|
||||||
|
builder: new EventPublisher(new Nip7Signer(), session.publicKey),
|
||||||
|
system
|
||||||
|
} : undefined;
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
import { ReactNode, useContext, useState } from "react";
|
import { ReactNode, useState } from "react";
|
||||||
import { Categories, Category, TorrentKind } from "../const";
|
import { Categories, Category, TorrentKind } from "../const";
|
||||||
import { Button } from "../element/button";
|
import { Button } from "../element/button";
|
||||||
import { EventPublisher, Nip7Signer } from "@snort/system";
|
|
||||||
import { useLogin } from "../login";
|
import { useLogin } from "../login";
|
||||||
import { dedupe } from "@snort/shared";
|
import { dedupe } from "@snort/shared";
|
||||||
import * as bencode from "../bencode";
|
import * as bencode from "../bencode";
|
||||||
import { sha1 } from "@noble/hashes/sha1";
|
import { sha1 } from "@noble/hashes/sha1";
|
||||||
import { bytesToHex } from "@noble/hashes/utils";
|
import { bytesToHex } from "@noble/hashes/utils";
|
||||||
import { SnortContext } from "@snort/system-react";
|
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
async function openFile(): Promise<File | undefined> {
|
async function openFile(): Promise<File | undefined> {
|
||||||
@ -45,7 +43,6 @@ async function openFile(): Promise<File | undefined> {
|
|||||||
|
|
||||||
export function NewPage() {
|
export function NewPage() {
|
||||||
const login = useLogin();
|
const login = useLogin();
|
||||||
const system = useContext(SnortContext);
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const [obj, setObj] = useState({
|
const [obj, setObj] = useState({
|
||||||
@ -88,10 +85,7 @@ export function NewPage() {
|
|||||||
|
|
||||||
async function publish() {
|
async function publish() {
|
||||||
if (!login) return;
|
if (!login) return;
|
||||||
const signer = new Nip7Signer();
|
const ev = await login.builder.generic((eb) => {
|
||||||
const builder = new EventPublisher(signer, login.publicKey);
|
|
||||||
|
|
||||||
const ev = await builder.generic((eb) => {
|
|
||||||
const v = eb
|
const v = eb
|
||||||
.kind(TorrentKind)
|
.kind(TorrentKind)
|
||||||
.content(obj.desc)
|
.content(obj.desc)
|
||||||
@ -107,7 +101,7 @@ export function NewPage() {
|
|||||||
console.debug(ev);
|
console.debug(ev);
|
||||||
|
|
||||||
if (ev) {
|
if (ev) {
|
||||||
await system.BroadcastEvent(ev);
|
await login.system.BroadcastEvent(ev);
|
||||||
}
|
}
|
||||||
navigate("/")
|
navigate("/")
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import { unwrap } from "@snort/shared";
|
import { unwrap } from "@snort/shared";
|
||||||
import { NoteCollection, RequestBuilder, TaggedNostrEvent, parseNostrLink } from "@snort/system";
|
import { NoteCollection, RequestBuilder, TaggedNostrEvent, parseNostrLink } from "@snort/system";
|
||||||
import { useRequestBuilder } from "@snort/system-react";
|
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 { FormatBytes, TorrentKind } from "../const";
|
||||||
import { ProfileImage } from "../element/profile-image";
|
import { ProfileImage } from "../element/profile-image";
|
||||||
import { MagnetLink } from "../element/magnet";
|
import { MagnetLink } from "../element/magnet";
|
||||||
|
import { useLogin } from "../login";
|
||||||
|
import { Button } from "../element/button";
|
||||||
|
|
||||||
export function TorrentPage() {
|
export function TorrentPage() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
@ -24,11 +26,21 @@ export function TorrentPage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function TorrentDetail({ item }: { item: TaggedNostrEvent }) {
|
export function TorrentDetail({ item }: { item: TaggedNostrEvent }) {
|
||||||
|
const login = useLogin();
|
||||||
|
const navigate = useNavigate();
|
||||||
const name = item.tags.find((a) => a[0] === "title")?.at(1);
|
const name = item.tags.find((a) => a[0] === "title")?.at(1);
|
||||||
const size = Number(item.tags.find((a) => a[0] === "size")?.at(1));
|
const size = Number(item.tags.find((a) => a[0] === "size")?.at(1));
|
||||||
const files = item.tags.filter(a => a[0] === "file");
|
const files = item.tags.filter(a => a[0] === "file");
|
||||||
const tags = item.tags.filter(a => a[0] === "t").map(a => a[1]);
|
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 (
|
return (
|
||||||
<div className="flex flex-col gap-2">
|
<div className="flex flex-col gap-2">
|
||||||
<div className="flex gap-2 items-center text-xl">
|
<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>
|
<small className="text-slate-500 font-semibold">{FormatBytes(Number(a[2]))}</small>
|
||||||
</div>)}
|
</div>)}
|
||||||
</div>
|
</div>
|
||||||
|
{item.pubkey == login?.publicKey && <Button className="bg-red-600 hover:bg-red-800" onClick={deleteTorrent}>
|
||||||
|
Delete
|
||||||
|
</Button>}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user