mirror of
https://github.com/aljazceru/mutiny-web.git
synced 2026-01-16 20:54:20 +01:00
contact search and new amount editor
This commit is contained in:
committed by
Tony Giorgio
parent
077ccb2a63
commit
bc2fbf9b2f
@@ -221,12 +221,11 @@ export function bech32WordsToUrl(words: number[]) {
|
||||
}
|
||||
|
||||
export const bech32 = getLibraryFromEncoding("bech32");
|
||||
export const bech32m = getLibraryFromEncoding("bech32m");
|
||||
export interface Decoded {
|
||||
interface Decoded {
|
||||
prefix: string;
|
||||
words: number[];
|
||||
}
|
||||
export interface BechLib {
|
||||
interface BechLib {
|
||||
decodeUnsafe: (
|
||||
str: string,
|
||||
LIMIT?: number | undefined
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ResourceFetcher } from "solid-js";
|
||||
import { useMegaStore } from "~/state/megaStore";
|
||||
import { hexpubFromNpub, NostrKind, NostrTag } from "~/utils/nostr";
|
||||
|
||||
export type NostrEvent = {
|
||||
type NostrEvent = {
|
||||
created_at: number;
|
||||
content: string;
|
||||
tags: NostrTag[];
|
||||
@@ -16,7 +16,7 @@ export type NostrEvent = {
|
||||
sig?: string;
|
||||
};
|
||||
|
||||
export type SimpleZapItem = {
|
||||
type SimpleZapItem = {
|
||||
kind: "public" | "private" | "anonymous";
|
||||
from_hexpub: string;
|
||||
to_hexpub: string;
|
||||
@@ -28,7 +28,7 @@ export type SimpleZapItem = {
|
||||
content?: string;
|
||||
};
|
||||
|
||||
export type NostrProfile = {
|
||||
type NostrProfile = {
|
||||
id: string;
|
||||
pubkey: string;
|
||||
created_at: number;
|
||||
@@ -116,7 +116,7 @@ async function simpleZapFromEvent(
|
||||
}
|
||||
}
|
||||
|
||||
export const PRIMAL_API = import.meta.env.VITE_PRIMAL;
|
||||
const PRIMAL_API = import.meta.env.VITE_PRIMAL;
|
||||
|
||||
async function fetchFollows(npub: string): Promise<string[]> {
|
||||
let pubkey = undefined;
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
export function getHostname(url: string): string {
|
||||
// Check if the URL begins with "ws://" or "wss://"
|
||||
if (url.startsWith("ws://")) {
|
||||
// If it does, remove "ws://" from the URL
|
||||
url = url.slice(5);
|
||||
} else if (url.startsWith("wss://")) {
|
||||
// If it begins with "wss://", remove "wss://" from the URL
|
||||
url = url.slice(6);
|
||||
}
|
||||
|
||||
// Return the resulting URL
|
||||
return url;
|
||||
}
|
||||
@@ -2,13 +2,11 @@ export * from "./conversions";
|
||||
export * from "./deepSignal";
|
||||
export * from "./download";
|
||||
export * from "./eify";
|
||||
export * from "./getHostname";
|
||||
export * from "./gradientHash";
|
||||
export * from "./mempoolTxUrl";
|
||||
export * from "./objectToSearchParams";
|
||||
export * from "./prettyPrintTime";
|
||||
export * from "./subscriptions";
|
||||
export * from "./tags";
|
||||
export * from "./timeout";
|
||||
export * from "./typescript";
|
||||
export * from "./useCopy";
|
||||
@@ -19,3 +17,4 @@ export * from "./openLinkProgrammatically";
|
||||
export * from "./nostr";
|
||||
export * from "./currencies";
|
||||
export * from "./bech32";
|
||||
export * from "./keypad";
|
||||
|
||||
121
src/utils/keypad.ts
Normal file
121
src/utils/keypad.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { Currency } from "./currencies";
|
||||
|
||||
// Checks the users locale to determine if decimals should be a "." or a ","
|
||||
const decimalDigitDivider = Number(1.0)
|
||||
.toLocaleString(navigator.languages[0], { minimumFractionDigits: 1 })
|
||||
.substring(1, 2);
|
||||
|
||||
export function toDisplayHandleNaN(
|
||||
input?: string | bigint,
|
||||
fiat?: Currency
|
||||
): string {
|
||||
if (!input) {
|
||||
return "0";
|
||||
}
|
||||
|
||||
if (typeof input === "bigint") {
|
||||
console.error("toDisplayHandleNaN: input is a bigint", input);
|
||||
}
|
||||
|
||||
const inputStr = input.toString();
|
||||
|
||||
const parsed = Number(input);
|
||||
|
||||
//handle decimals so the user can always see the accurate amount
|
||||
if (isNaN(parsed)) {
|
||||
return "0";
|
||||
} else if (parsed === Math.trunc(parsed) && inputStr.endsWith(".")) {
|
||||
return (
|
||||
parsed.toLocaleString(navigator.languages[0]) + decimalDigitDivider
|
||||
);
|
||||
/* To avoid having logic to handle every number up to 8 decimals
|
||||
any custom currency pair that has more than 3 decimals will always show all decimals*/
|
||||
} else if (fiat?.maxFractionalDigits && fiat.maxFractionalDigits > 3) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: parsed === 0 ? 0 : fiat.maxFractionalDigits,
|
||||
maximumFractionDigits: fiat.maxFractionalDigits
|
||||
});
|
||||
} else if (parsed === Math.trunc(parsed) && inputStr.endsWith(".0")) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: 1
|
||||
});
|
||||
} else if (parsed === Math.trunc(parsed) && inputStr.endsWith(".00")) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: 2
|
||||
});
|
||||
} else if (parsed === Math.trunc(parsed) && inputStr.endsWith(".000")) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: 3
|
||||
});
|
||||
} else if (
|
||||
parsed !== Math.trunc(parsed) &&
|
||||
// matches strings that have 3 total digits after the decimal and ends with 0
|
||||
inputStr.match(/\.\d{2}0$/) &&
|
||||
inputStr.includes(".", inputStr.length - 4)
|
||||
) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: 3
|
||||
});
|
||||
} else if (
|
||||
parsed !== Math.trunc(parsed) &&
|
||||
// matches strings that have 2 total digits after the decimal and ends with 0
|
||||
inputStr.match(/\.\d{1}0$/) &&
|
||||
inputStr.includes(".", inputStr.length - 3)
|
||||
) {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
minimumFractionDigits: 2
|
||||
});
|
||||
} else {
|
||||
return parsed.toLocaleString(navigator.languages[0], {
|
||||
maximumFractionDigits: 3
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function fiatInputSanitizer(input: string, maxDecimals: number): string {
|
||||
// Make sure only numbers and a single decimal point are allowed if decimals are allowed
|
||||
let allowDecimalRegex;
|
||||
if (maxDecimals !== 0) {
|
||||
allowDecimalRegex = new RegExp("[^0-9.]", "g");
|
||||
} else {
|
||||
allowDecimalRegex = new RegExp("[^0-9]", "g");
|
||||
}
|
||||
const numeric = input
|
||||
.replace(allowDecimalRegex, "")
|
||||
.replace(/(\..*)\./g, "$1");
|
||||
|
||||
// Remove leading zeros if not a decimal, add 0 if starts with a decimal
|
||||
const cleaned = numeric.replace(/^0([^.]|$)/g, "$1").replace(/^\./g, "0.");
|
||||
|
||||
// If there are more characters after the decimal than allowed, shift the decimal
|
||||
const shiftRegex = new RegExp(
|
||||
"(\\.[0-9]{" + (maxDecimals + 1) + "}).*",
|
||||
"g"
|
||||
);
|
||||
const shifted = cleaned.match(shiftRegex)
|
||||
? (parseFloat(cleaned) * 10).toFixed(maxDecimals)
|
||||
: cleaned;
|
||||
|
||||
// Truncate any numbers past the maxDecimal for the currency
|
||||
const decimalRegex = new RegExp("(\\.[0-9]{" + maxDecimals + "}).*", "g");
|
||||
const decimals = shifted.replace(decimalRegex, "$1");
|
||||
|
||||
return decimals;
|
||||
}
|
||||
|
||||
export function satsInputSanitizer(input: string): string {
|
||||
// Make sure only numbers are allowed
|
||||
const numeric = input.replace(/[^0-9]/g, "");
|
||||
// If it starts with a 0, remove the 0
|
||||
const noLeadingZero = numeric.replace(/^0([^.]|$)/g, "$1");
|
||||
|
||||
return noLeadingZero;
|
||||
}
|
||||
|
||||
export function btcFloatRounding(localValue: string): string {
|
||||
return (
|
||||
(parseFloat(localValue) -
|
||||
parseFloat(localValue.charAt(localValue.length - 1)) / 100000000) /
|
||||
10
|
||||
).toFixed(8);
|
||||
}
|
||||
@@ -45,8 +45,11 @@ export declare enum NostrKind {
|
||||
}
|
||||
|
||||
export async function hexpubFromNpub(
|
||||
npub: string
|
||||
npub?: string
|
||||
): Promise<string | undefined> {
|
||||
if (!npub) {
|
||||
return undefined;
|
||||
}
|
||||
if (!npub.toLowerCase().startsWith("npub")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { TagItem } from "@mutinywallet/mutiny-wasm";
|
||||
|
||||
export function tagsToIds(tags?: TagItem[]): string[] {
|
||||
if (!tags) {
|
||||
return [];
|
||||
}
|
||||
return tags.filter((tag) => tag.id !== "Unknown").map((tag) => tag.id);
|
||||
}
|
||||
|
||||
export function sortByLastUsed(a: TagItem, b: TagItem) {
|
||||
return Number(b.last_used_time - a.last_used_time);
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import { Capacitor } from "@capacitor/core";
|
||||
import type { Accessor } from "solid-js";
|
||||
import { createSignal } from "solid-js";
|
||||
|
||||
export type UseCopyProps = {
|
||||
type UseCopyProps = {
|
||||
copiedTimeout?: number;
|
||||
};
|
||||
type CopyFn = (text: string) => Promise<void>;
|
||||
|
||||
@@ -1,14 +1,6 @@
|
||||
import { Haptics } from "@capacitor/haptics";
|
||||
import { NotificationType } from "@capacitor/haptics/dist/esm/definitions";
|
||||
|
||||
export const vibrate = async (millis = 250) => {
|
||||
try {
|
||||
await Haptics.vibrate({ duration: millis });
|
||||
} catch (error) {
|
||||
console.warn(error);
|
||||
}
|
||||
};
|
||||
|
||||
export const vibrateSuccess = async () => {
|
||||
try {
|
||||
await Haptics.notification({ type: NotificationType.Success });
|
||||
|
||||
Reference in New Issue
Block a user