From 4b34b66ee8374116ded4a2217877f7e312b31fe2 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Fri, 10 Nov 2023 14:29:33 -0600 Subject: [PATCH] better types thx wasm-bindgen --- src/components/PendingNwc.tsx | 6 ++--- src/components/TagEditor.tsx | 17 +++++++------ src/components/layout/Misc.tsx | 7 +++--- src/routes/Receive.tsx | 16 ++++-------- src/routes/Send.tsx | 8 +++--- src/routes/settings/Connections.tsx | 4 +-- src/routes/settings/Gift.tsx | 4 +-- src/state/megaStore.tsx | 13 ++++++---- src/utils/tags.ts | 39 ++--------------------------- 9 files changed, 39 insertions(+), 75 deletions(-) diff --git a/src/components/PendingNwc.tsx b/src/components/PendingNwc.tsx index ecd0b4f..a1adea1 100644 --- a/src/components/PendingNwc.tsx +++ b/src/components/PendingNwc.tsx @@ -1,4 +1,3 @@ -import { NwcProfile } from "@mutinywallet/mutiny-wasm"; import { createEffect, createResource, @@ -42,10 +41,11 @@ export function PendingNwc() { const [error, setError] = createSignal(); async function fetchPendingRequests() { - const profiles: NwcProfile[] = - await state.mutiny_wallet?.get_nwc_profiles(); + const profiles = await state.mutiny_wallet?.get_nwc_profiles(); + if (!profiles) return []; const pending = await state.mutiny_wallet?.get_pending_nwc_invoices(); + if (!pending) return []; const pendingItems: PendingItem[] = []; diff --git a/src/components/TagEditor.tsx b/src/components/TagEditor.tsx index bd6d525..5946b5d 100644 --- a/src/components/TagEditor.tsx +++ b/src/components/TagEditor.tsx @@ -2,30 +2,31 @@ import { createOptions, Select } from "@thisbeyond/solid-select"; import "~/styles/solid-select.css"; +import { TagItem, TagKind } from "@mutinywallet/mutiny-wasm"; import { createMemo, createSignal, onMount } from "solid-js"; import { useMegaStore } from "~/state/megaStore"; -import { MutinyTagItem, sortByLastUsed } from "~/utils"; +import { sortByLastUsed } from "~/utils"; -const createLabelValue = (label: string): Partial => { - return { name: label, kind: "Contact" }; +const createLabelValue = (label: string): Partial => { + return { name: label, kind: TagKind.Contact }; }; export function TagEditor(props: { - selectedValues: Partial[]; - setSelectedValues: (value: Partial[]) => void; + selectedValues: Partial[]; + setSelectedValues: (value: Partial[]) => void; placeholder: string; autoFillTag?: string | undefined; }) { const [_state, actions] = useMegaStore(); - const [availableTags, setAvailableTags] = createSignal([]); + const [availableTags, setAvailableTags] = createSignal([]); onMount(async () => { const tags = await actions.listTags(); if (tags) { setAvailableTags( tags - .filter((tag) => tag.kind === "Contact") + .filter((tag) => tag.kind === TagKind.Contact) .sort(sortByLastUsed) ); if (props.autoFillTag && availableTags()) { @@ -51,7 +52,7 @@ export function TagEditor(props: { }); }); - const onChange = (selected: MutinyTagItem[]) => { + const onChange = (selected: TagItem[]) => { props.setSelectedValues(selected); const lastValue = selected[selected.length - 1]; diff --git a/src/components/layout/Misc.tsx b/src/components/layout/Misc.tsx index b57b3e4..b3b7d87 100644 --- a/src/components/layout/Misc.tsx +++ b/src/components/layout/Misc.tsx @@ -4,6 +4,7 @@ import { Checkbox as KCheckbox, Separator } from "@kobalte/core"; +import { TagItem, TagKind } from "@mutinywallet/mutiny-wasm"; import { createResource, createSignal, @@ -27,7 +28,7 @@ import { } from "~/components"; import { useI18n } from "~/i18n/context"; import { useMegaStore } from "~/state/megaStore"; -import { generateGradient, MutinyTagItem } from "~/utils"; +import { generateGradient } from "~/utils"; export const SmallHeader: ParentComponent<{ class?: string }> = (props) => { return ( @@ -268,7 +269,7 @@ export const TinyText: ParentComponent = (props) => { export const TinyButton: ParentComponent<{ onClick: () => void; - tag?: MutinyTagItem; + tag?: TagItem; }> = (props) => { // TODO: don't need to run this if it's not a contact const [gradient] = createResource(async () => { @@ -276,7 +277,7 @@ export const TinyButton: ParentComponent<{ }); const bg = () => - props.tag?.name && props.tag?.kind === "Contact" + props.tag?.name && props.tag?.kind === TagKind.Contact ? gradient() : "rgb(255 255 255 / 0.1)"; diff --git a/src/routes/Receive.tsx b/src/routes/Receive.tsx index 79b0c4b..1acd676 100644 --- a/src/routes/Receive.tsx +++ b/src/routes/Receive.tsx @@ -3,7 +3,8 @@ import { Contact, MutinyBip21RawMaterials, - MutinyInvoice + MutinyInvoice, + TagItem } from "@mutinywallet/mutiny-wasm"; import { createEffect, @@ -49,12 +50,7 @@ import { } from "~/components"; import { useI18n } from "~/i18n/context"; import { useMegaStore } from "~/state/megaStore"; -import { - eify, - MutinyTagItem, - objectToSearchParams, - vibrateSuccess -} from "~/utils"; +import { eify, objectToSearchParams, vibrateSuccess } from "~/utils"; type OnChainTx = { transaction: { @@ -126,9 +122,7 @@ export default function Receive() { const [lspFee, setLspFee] = createSignal(0n); // Tagging stuff - const [selectedValues, setSelectedValues] = createSignal( - [] - ); + const [selectedValues, setSelectedValues] = createSignal([]); // The data we get after a payment const [paymentTx, setPaymentTx] = createSignal(); @@ -215,7 +209,7 @@ export default function Receive() { } async function processContacts( - contacts: Partial[] + contacts: Partial[] ): Promise { if (contacts.length) { const first = contacts![0]; diff --git a/src/routes/Send.tsx b/src/routes/Send.tsx index c43d742..1b43e96 100644 --- a/src/routes/Send.tsx +++ b/src/routes/Send.tsx @@ -1,6 +1,6 @@ import { Clipboard } from "@capacitor/clipboard"; import { Capacitor } from "@capacitor/core"; -import { Contact, MutinyInvoice } from "@mutinywallet/mutiny-wasm"; +import { Contact, MutinyInvoice, TagItem } from "@mutinywallet/mutiny-wasm"; import { createEffect, createMemo, @@ -48,7 +48,7 @@ import { import { useI18n } from "~/i18n/context"; import { ParsedParams } from "~/logic/waila"; import { useMegaStore } from "~/state/megaStore"; -import { eify, MutinyTagItem, vibrateSuccess } from "~/utils"; +import { eify, vibrateSuccess } from "~/utils"; export type SendSource = "lightning" | "onchain"; @@ -250,7 +250,7 @@ export default function Send() { // Tagging stuff const [selectedContacts, setSelectedContacts] = createSignal< - Partial[] + Partial[] >([]); // Details Modal @@ -476,7 +476,7 @@ export default function Send() { } async function processContacts( - contacts: Partial[] + contacts: Partial[] ): Promise { if (contacts.length) { const first = contacts![0]; diff --git a/src/routes/settings/Connections.tsx b/src/routes/settings/Connections.tsx index 394f429..ea506ca 100644 --- a/src/routes/settings/Connections.tsx +++ b/src/routes/settings/Connections.tsx @@ -219,8 +219,8 @@ function Nwc() { async function fetchNwcProfiles() { try { - const profiles: NwcProfile[] = - await state.mutiny_wallet?.get_nwc_profiles(); + const profiles = await state.mutiny_wallet?.get_nwc_profiles(); + if (!profiles) return []; return profiles; } catch (e) { diff --git a/src/routes/settings/Gift.tsx b/src/routes/settings/Gift.tsx index 510749c..bf11ce9 100644 --- a/src/routes/settings/Gift.tsx +++ b/src/routes/settings/Gift.tsx @@ -104,8 +104,8 @@ function ExistingGifts() { const [giftNWCProfiles, { refetch }] = createResource(async () => { try { - const profiles: NwcProfile[] = - await state.mutiny_wallet?.get_nwc_profiles(); + const profiles = await state.mutiny_wallet?.get_nwc_profiles(); + if (!profiles) return []; const filteredForGifts = profiles.filter((p) => p.tag === "Gift"); diff --git a/src/state/megaStore.tsx b/src/state/megaStore.tsx index fcc639f..5e13c94 100644 --- a/src/state/megaStore.tsx +++ b/src/state/megaStore.tsx @@ -1,7 +1,11 @@ /* @refresh reload */ // Inspired by https://github.com/solidjs/solid-realworld/blob/main/src/store/index.js -import { MutinyBalance, MutinyWallet } from "@mutinywallet/mutiny-wasm"; +import { + MutinyBalance, + MutinyWallet, + TagItem +} from "@mutinywallet/mutiny-wasm"; import { createContext, onCleanup, @@ -25,7 +29,6 @@ import { BTC_OPTION, Currency, eify, - MutinyTagItem, subscriptionValid, USD_OPTION } from "~/utils"; @@ -70,7 +73,7 @@ export type MegaStore = [ setScanResult(scan_result: ParsedParams | undefined): void; sync(): Promise; setHasBackedUp(): void; - listTags(): Promise; + listTags(): Promise; checkForSubscription(justPaid?: boolean): Promise; fetchPrice(fiat: Currency): Promise; saveFiat(fiat: Currency): void; @@ -317,9 +320,9 @@ export const Provider: ParentComponent = (props) => { localStorage.setItem("has_backed_up", "true"); setState({ has_backed_up: true }); }, - async listTags(): Promise { + async listTags(): Promise { try { - return state.mutiny_wallet?.get_tag_items() as MutinyTagItem[]; + return state.mutiny_wallet?.get_tag_items(); } catch (e) { console.error(e); return []; diff --git a/src/utils/tags.ts b/src/utils/tags.ts index 0f944d9..1ca7ada 100644 --- a/src/utils/tags.ts +++ b/src/utils/tags.ts @@ -1,47 +1,12 @@ import { TagItem } from "@mutinywallet/mutiny-wasm"; -export type MutinyTagItem = { - id: string; - kind: "Label" | "Contact"; - name: string; - last_used_time: bigint; - npub?: string; - ln_address?: string; - lnurl?: string; -}; - -export const UNKNOWN_TAG: MutinyTagItem = { - id: "Unknown", - kind: "Label", - name: "Unknown", - last_used_time: 0n -}; - -export function tagsToIds(tags?: MutinyTagItem[]): string[] { +export function tagsToIds(tags?: TagItem[]): string[] { if (!tags) { return []; } return tags.filter((tag) => tag.id !== "Unknown").map((tag) => tag.id); } -export function tagToMutinyTag(tag: TagItem): MutinyTagItem { - let kind: MutinyTagItem["kind"]; - - switch (tag.kind) { - case 0: { - kind = "Label"; - break; - } - case 1: - default: { - kind = "Contact"; - break; - } - } - - return { ...tag, kind }; -} - -export function sortByLastUsed(a: MutinyTagItem, b: MutinyTagItem) { +export function sortByLastUsed(a: TagItem, b: TagItem) { return Number(b.last_used_time - a.last_used_time); }