From 9be3e2c31372dd7943c7c7c8c663d038db8fc9c7 Mon Sep 17 00:00:00 2001 From: Shusui MOYATANI Date: Mon, 1 May 2023 13:58:07 +0900 Subject: [PATCH] feat: support lud06 / lud16 in profile input --- src/components/ProfileEdit.tsx | 99 +++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/src/components/ProfileEdit.tsx b/src/components/ProfileEdit.tsx index adccabd..d4b7370 100644 --- a/src/components/ProfileEdit.tsx +++ b/src/components/ProfileEdit.tsx @@ -3,6 +3,7 @@ import { createSignal, type Component, batch, onMount, For, JSX, Show } from 'so import { createMutation } from '@tanstack/solid-query'; import ArrowLeft from 'heroicons/24/outline/arrow-left.svg'; import omit from 'lodash/omit'; +import omitBy from 'lodash/omitBy'; import Modal from '@/components/Modal'; import useConfig from '@/core/useConfig'; @@ -16,6 +17,16 @@ export type ProfileEditProps = { onClose: () => void; }; +const LNURLRegexString = 'LNURL1[AC-HJ-NP-Zac-hj-np-z02-9]+'; +const LightningAddressRegexString = '[-a-zA-Z0-9.]+@[-a-zA-Z0-9.]+'; +const LUDAddressRegexString = `^(${LNURLRegexString}|${LightningAddressRegexString})$`; + +const LNURLRegex = new RegExp(`^${LNURLRegexString}$`); +const LightningAddressRegex = new RegExp(`${LightningAddressRegexString}`); + +const isLNURL = (s: string) => LNURLRegex.test(s); +const isLightningAddress = (s: string) => LightningAddressRegex.test(s); + const ProfileEdit: Component = (props) => { const pubkey = usePubkey(); const { config } = useConfig(); @@ -33,6 +44,7 @@ const ProfileEdit: Component = (props) => { const [about, setAbout] = createSignal(''); const [website, setWebsite] = createSignal(''); const [nip05, setNIP05] = createSignal(''); + const [lightningAddress, setLightningAddress] = createSignal(''); const mutation = createMutation({ mutationKey: ['updateProfile'], @@ -61,7 +73,17 @@ const ProfileEdit: Component = (props) => { const disabled = () => query.isLoading || query.isError || mutation.isLoading; const otherProperties = () => - omit(profile(), ['picture', 'banner', 'name', 'display_name', 'about', 'website', 'nip05']); + omit(profile(), [ + 'picture', + 'banner', + 'name', + 'display_name', + 'about', + 'website', + 'nip05', + 'lud06', + 'lud16', + ]); const handleSubmit: JSX.EventHandler = (ev) => { ev.preventDefault(); @@ -69,15 +91,20 @@ const ProfileEdit: Component = (props) => { const p = pubkey(); if (p == null) return; - const newProfile: Profile = { - picture: picture(), - banner: banner(), - name: name(), - display_name: displayName(), - about: about(), - website: website(), - nip05: nip05(), - }; + const newProfile: Profile = omitBy( + { + picture: picture(), + banner: banner(), + name: name(), + display_name: displayName(), + about: about(), + website: website(), + nip05: nip05(), + lud06: isLNURL(lightningAddress()) ? lightningAddress() : null, + lud16: isLightningAddress(lightningAddress()) ? lightningAddress() : null, + }, + (v) => v == null || v.length === 0, + ); mutation.mutate({ relayUrls: config().relayUrls, @@ -103,6 +130,11 @@ const ProfileEdit: Component = (props) => { setAbout((current) => currentProfile.about ?? current); setWebsite((current) => currentProfile.website ?? current); setNIP05((current) => currentProfile.nip05 ?? current); + if (currentProfile.lud16 != null) { + setLightningAddress(currentProfile.lud16); + } else if (currentProfile.lud06 != null) { + setLightningAddress(currentProfile.lud06); + } }); }); @@ -118,7 +150,7 @@ const ProfileEdit: Component = (props) => { -
+
0} fallback={
} keyed>
@@ -210,7 +242,7 @@ const ProfileEdit: Component = (props) => { 自己紹介