|
| 1 | +import { Collapsible } from "@kobalte/core"; |
| 2 | +import { Contract } from "@mutinywallet/mutiny-wasm"; |
| 3 | +import { createResource, For, Suspense } from "solid-js"; |
| 4 | + |
| 5 | +import { Button, InnerCard, VStack } from "~/components"; |
| 6 | +import { useMegaStore } from "~/state/megaStore"; |
| 7 | + |
| 8 | +type RefetchDLCsType = ( |
| 9 | + info?: unknown |
| 10 | +) => Contract[] | Promise<Contract[] | undefined> | null | undefined; |
| 11 | + |
| 12 | +function DLCItem(props: { dlc: Contract; refetch: RefetchDLCsType }) { |
| 13 | + const [state, _] = useMegaStore(); |
| 14 | + |
| 15 | + const handleRejectDLC = async () => { |
| 16 | + await state.mutiny_wallet?.reject_dlc_offer(props.dlc.id); |
| 17 | + await props.refetch(); |
| 18 | + }; |
| 19 | + |
| 20 | + const handleAcceptDLC = async () => { |
| 21 | + await state.mutiny_wallet?.accept_dlc_offer(props.dlc.id); |
| 22 | + }; |
| 23 | + |
| 24 | + const handleCloseDLC = async () => { |
| 25 | + const userInput = prompt("Enter oracle sigs:"); |
| 26 | + if (userInput != null) { |
| 27 | + await state.mutiny_wallet?.close_dlc( |
| 28 | + props.dlc.id, |
| 29 | + userInput.trim() |
| 30 | + ); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <Collapsible.Root> |
| 36 | + <Collapsible.Trigger class="w-full"> |
| 37 | + <h2 class="truncate rounded bg-neutral-200 px-4 py-2 text-start font-mono text-lg text-black"> |
| 38 | + {">"} {props.dlc.id} |
| 39 | + </h2> |
| 40 | + </Collapsible.Trigger> |
| 41 | + <Collapsible.Content> |
| 42 | + <VStack> |
| 43 | + <pre class="overflow-x-auto whitespace-pre-wrap break-all"> |
| 44 | + {JSON.stringify(props.dlc, null, 2)} |
| 45 | + </pre> |
| 46 | + <Button intent="green" layout="xs" onClick={handleCloseDLC}> |
| 47 | + Close |
| 48 | + </Button> |
| 49 | + <Button |
| 50 | + intent="green" |
| 51 | + layout="xs" |
| 52 | + onClick={handleAcceptDLC} |
| 53 | + > |
| 54 | + Accept |
| 55 | + </Button> |
| 56 | + <Button intent="red" layout="xs" onClick={handleRejectDLC}> |
| 57 | + Reject |
| 58 | + </Button> |
| 59 | + </VStack> |
| 60 | + </Collapsible.Content> |
| 61 | + </Collapsible.Root> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +export function DLCsList() { |
| 66 | + const [state, _] = useMegaStore(); |
| 67 | + |
| 68 | + const getDLCs = async () => { |
| 69 | + return (await state.mutiny_wallet?.list_dlcs()) as Promise<Contract[]>; |
| 70 | + }; |
| 71 | + |
| 72 | + const [peers, { refetch }] = createResource(getDLCs); |
| 73 | + |
| 74 | + return ( |
| 75 | + <> |
| 76 | + <InnerCard title="DLCs"> |
| 77 | + {/* By wrapping this in a suspense I don't cause the page to jump to the top */} |
| 78 | + <Suspense> |
| 79 | + <VStack> |
| 80 | + <For |
| 81 | + each={peers.latest} |
| 82 | + fallback={<code>No DLCs found.</code>} |
| 83 | + > |
| 84 | + {(dlc) => <DLCItem dlc={dlc} refetch={refetch} />} |
| 85 | + </For> |
| 86 | + </VStack> |
| 87 | + </Suspense> |
| 88 | + <Button layout="small" onClick={refetch}> |
| 89 | + Refresh |
| 90 | + </Button> |
| 91 | + </InnerCard> |
| 92 | + </> |
| 93 | + ); |
| 94 | +} |
0 commit comments