Skip to content

Commit

Permalink
better types thx wasm-bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Nov 14, 2023
1 parent 21ccd95 commit 4b34b66
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/components/PendingNwc.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { NwcProfile } from "@mutinywallet/mutiny-wasm";
import {
createEffect,
createResource,
Expand Down Expand Up @@ -42,10 +41,11 @@ export function PendingNwc() {
const [error, setError] = createSignal<Error>();

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[] = [];

Expand Down
17 changes: 9 additions & 8 deletions src/components/TagEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MutinyTagItem> => {
return { name: label, kind: "Contact" };
const createLabelValue = (label: string): Partial<TagItem> => {
return { name: label, kind: TagKind.Contact };
};

export function TagEditor(props: {
selectedValues: Partial<MutinyTagItem>[];
setSelectedValues: (value: Partial<MutinyTagItem>[]) => void;
selectedValues: Partial<TagItem>[];
setSelectedValues: (value: Partial<TagItem>[]) => void;
placeholder: string;
autoFillTag?: string | undefined;
}) {
const [_state, actions] = useMegaStore();
const [availableTags, setAvailableTags] = createSignal<MutinyTagItem[]>([]);
const [availableTags, setAvailableTags] = createSignal<TagItem[]>([]);

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()) {
Expand All @@ -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];
Expand Down
7 changes: 4 additions & 3 deletions src/components/layout/Misc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Checkbox as KCheckbox,
Separator
} from "@kobalte/core";
import { TagItem, TagKind } from "@mutinywallet/mutiny-wasm";
import {
createResource,
createSignal,
Expand All @@ -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 (
Expand Down Expand Up @@ -268,15 +269,15 @@ 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 () => {
return generateGradient(props.tag?.name || "?");
});

const bg = () =>
props.tag?.name && props.tag?.kind === "Contact"
props.tag?.name && props.tag?.kind === TagKind.Contact
? gradient()
: "rgb(255 255 255 / 0.1)";

Expand Down
16 changes: 5 additions & 11 deletions src/routes/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import {
Contact,
MutinyBip21RawMaterials,
MutinyInvoice
MutinyInvoice,
TagItem
} from "@mutinywallet/mutiny-wasm";
import {
createEffect,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -126,9 +122,7 @@ export default function Receive() {
const [lspFee, setLspFee] = createSignal(0n);

// Tagging stuff
const [selectedValues, setSelectedValues] = createSignal<MutinyTagItem[]>(
[]
);
const [selectedValues, setSelectedValues] = createSignal<TagItem[]>([]);

// The data we get after a payment
const [paymentTx, setPaymentTx] = createSignal<OnChainTx>();
Expand Down Expand Up @@ -215,7 +209,7 @@ export default function Receive() {
}

async function processContacts(
contacts: Partial<MutinyTagItem>[]
contacts: Partial<TagItem>[]
): Promise<string[]> {
if (contacts.length) {
const first = contacts![0];
Expand Down
8 changes: 4 additions & 4 deletions src/routes/Send.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -250,7 +250,7 @@ export default function Send() {

// Tagging stuff
const [selectedContacts, setSelectedContacts] = createSignal<
Partial<MutinyTagItem>[]
Partial<TagItem>[]
>([]);

// Details Modal
Expand Down Expand Up @@ -476,7 +476,7 @@ export default function Send() {
}

async function processContacts(
contacts: Partial<MutinyTagItem>[]
contacts: Partial<TagItem>[]
): Promise<string[]> {
if (contacts.length) {
const first = contacts![0];
Expand Down
4 changes: 2 additions & 2 deletions src/routes/settings/Connections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/settings/Gift.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
13 changes: 8 additions & 5 deletions src/state/megaStore.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -25,7 +29,6 @@ import {
BTC_OPTION,
Currency,
eify,
MutinyTagItem,
subscriptionValid,
USD_OPTION
} from "~/utils";
Expand Down Expand Up @@ -70,7 +73,7 @@ export type MegaStore = [
setScanResult(scan_result: ParsedParams | undefined): void;
sync(): Promise<void>;
setHasBackedUp(): void;
listTags(): Promise<MutinyTagItem[]>;
listTags(): Promise<TagItem[]>;
checkForSubscription(justPaid?: boolean): Promise<void>;
fetchPrice(fiat: Currency): Promise<number>;
saveFiat(fiat: Currency): void;
Expand Down Expand Up @@ -317,9 +320,9 @@ export const Provider: ParentComponent = (props) => {
localStorage.setItem("has_backed_up", "true");
setState({ has_backed_up: true });
},
async listTags(): Promise<MutinyTagItem[]> {
async listTags(): Promise<TagItem[] | undefined> {
try {
return state.mutiny_wallet?.get_tag_items() as MutinyTagItem[];
return state.mutiny_wallet?.get_tag_items();
} catch (e) {
console.error(e);
return [];
Expand Down
39 changes: 2 additions & 37 deletions src/utils/tags.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 4b34b66

Please sign in to comment.