Skip to content
This repository was archived by the owner on Apr 21, 2025. It is now read-only.

Commit 0748148

Browse files
committed
method chooser for send
1 parent 858c51b commit 0748148

File tree

5 files changed

+277
-254
lines changed

5 files changed

+277
-254
lines changed

src/components/AmountEditable.tsx

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
Show
88
} from "solid-js";
99

10-
import { AmountSmall, BigMoney } from "~/components";
11-
import { useI18n } from "~/i18n/context";
10+
import { AmountSats, BigMoney } from "~/components";
1211
import { useMegaStore } from "~/state/megaStore";
1312
import {
1413
btcFloatRounding,
@@ -19,17 +18,32 @@ import {
1918
toDisplayHandleNaN
2019
} from "~/utils";
2120

21+
export type MethodChoice = {
22+
method: "lightning" | "onchain";
23+
maxAmountSats?: bigint;
24+
};
25+
26+
// Make sure to update this when we get the fedi option in here!
27+
function methodToIcon(method: MethodChoice["method"]) {
28+
if (method === "lightning") {
29+
return "lightning";
30+
} else if (method === "onchain") {
31+
return "chain";
32+
}
33+
}
34+
2235
export const AmountEditable: ParentComponent<{
2336
initialAmountSats: string | bigint;
2437
setAmountSats: (s: bigint) => void;
25-
maxAmountSats?: bigint;
2638
fee?: string;
2739
frozenAmount?: boolean;
2840
onSubmit?: () => void;
41+
activeMethod?: MethodChoice;
42+
methods?: MethodChoice[];
43+
setChosenMethod?: (method: MethodChoice) => void;
2944
}> = (props) => {
3045
const [state, _actions] = useMegaStore();
3146
const [mode, setMode] = createSignal<"fiat" | "sats">("sats");
32-
const i18n = useI18n();
3347
const [localSats, setLocalSats] = createSignal(
3448
props.initialAmountSats.toString() || "0"
3549
);
@@ -229,12 +243,47 @@ export const AmountEditable: ParentComponent<{
229243
onFocus={() => focus()}
230244
/>
231245
</div>
232-
<Show when={props.maxAmountSats}>
233-
<p class="flex gap-2 px-4 py-2 text-sm font-light text-m-grey-400 md:text-base">
234-
{`${i18n.t("receive.amount_editable.balance")} `}
235-
<AmountSmall amountSats={props.maxAmountSats!} />
236-
</p>
246+
<Show when={props.methods?.length && props.activeMethod}>
247+
<MethodChooser
248+
methods={props.methods!}
249+
activeMethod={props.activeMethod!}
250+
setChosenMethod={props.setChosenMethod}
251+
/>
237252
</Show>
238253
</div>
239254
);
240255
};
256+
257+
function MethodChooser(props: {
258+
activeMethod: MethodChoice;
259+
methods: MethodChoice[];
260+
setChosenMethod?: (method: MethodChoice) => void;
261+
}) {
262+
function setNextMethod() {
263+
const activeIndex = props.methods.findIndex(
264+
(m) => m.method === props.activeMethod.method
265+
);
266+
const nextMethod =
267+
props.methods[
268+
activeIndex === props.methods.length - 1 ? 0 : activeIndex + 1
269+
];
270+
props.setChosenMethod && props.setChosenMethod(nextMethod);
271+
}
272+
return (
273+
<button
274+
onClick={setNextMethod}
275+
disabled={props.methods.length === 1}
276+
class="flex gap-2 rounded px-2 py-1 text-sm font-light text-m-grey-400 md:text-base"
277+
classList={{
278+
"border-b border-t border-b-white/10 border-t-white/50 bg-neutral-700":
279+
props.methods?.length > 1
280+
}}
281+
>
282+
<AmountSats
283+
amountSats={props.activeMethod.maxAmountSats!}
284+
denominationSize="sm"
285+
icon={methodToIcon(props.activeMethod.method)}
286+
/>
287+
</button>
288+
);
289+
}

src/components/MethodChooser.tsx

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/components/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ export * from "./BigMoney";
4949
export * from "./FeeDisplay";
5050
export * from "./ReceiveWarnings";
5151
export * from "./SimpleInput";
52-
export * from "./MethodChooser";
5352
export * from "./LabelCircle";

src/routes/Send.tsx

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
MegaCheck,
3434
MegaClock,
3535
MegaEx,
36+
MethodChoice,
3637
MutinyWalletGuard,
3738
NavBar,
3839
showToast,
@@ -637,6 +638,59 @@ export function Send() {
637638
);
638639
});
639640

641+
const lightningMethod = createMemo<MethodChoice>(() => {
642+
return {
643+
method: "lightning",
644+
maxAmountSats: maxLightning()
645+
};
646+
});
647+
648+
const onchainMethod = createMemo<MethodChoice>(() => {
649+
return {
650+
method: "onchain",
651+
maxAmountSats: maxOnchain()
652+
};
653+
});
654+
655+
const sendMethods = createMemo<MethodChoice[]>(() => {
656+
if (lnAddress() || lnurlp() || nodePubkey()) {
657+
return [lightningMethod()];
658+
}
659+
660+
if (invoice() && address()) {
661+
return [lightningMethod(), onchainMethod()];
662+
}
663+
664+
if (invoice()) {
665+
return [lightningMethod()];
666+
}
667+
668+
if (address()) {
669+
return [onchainMethod()];
670+
}
671+
672+
// We should never get here
673+
console.error("No send methods found");
674+
675+
return [];
676+
});
677+
678+
function setSourceFromMethod(method: MethodChoice) {
679+
if (method.method === "lightning") {
680+
setSource("lightning");
681+
} else if (method.method === "onchain") {
682+
setSource("onchain");
683+
}
684+
}
685+
686+
const activeMethod = createMemo(() => {
687+
if (source() === "lightning") {
688+
return lightningMethod();
689+
} else if (source() === "onchain") {
690+
return onchainMethod();
691+
}
692+
});
693+
640694
return (
641695
<MutinyWalletGuard>
642696
<DefaultMain>
@@ -726,23 +780,27 @@ export function Send() {
726780
<AmountEditable
727781
initialAmountSats={amountSats()}
728782
setAmountSats={setAmountInput}
729-
maxAmountSats={maxAmountSats()}
730783
fee={feeEstimate()?.toString()}
731784
onSubmit={() =>
732785
sendButtonDisabled() ? undefined : handleSend()
733786
}
787+
activeMethod={activeMethod()}
788+
methods={sendMethods()}
789+
setChosenMethod={setSourceFromMethod}
734790
/>
735791
</Show>
736792
<Show when={!isAmtEditable()}>
737793
<AmountEditable
738794
initialAmountSats={amountSats()}
739795
setAmountSats={setAmountInput}
740-
maxAmountSats={maxAmountSats()}
741796
fee={feeEstimate()?.toString()}
742797
frozenAmount={true}
743798
onSubmit={() =>
744799
sendButtonDisabled() ? undefined : handleSend()
745800
}
801+
activeMethod={activeMethod()}
802+
methods={sendMethods()}
803+
setChosenMethod={setSourceFromMethod}
746804
/>
747805
</Show>
748806
<Show when={feeEstimate()}>

0 commit comments

Comments
 (0)