bug: cleanup nip5 orders

This commit is contained in:
Kieran 2023-02-12 23:17:34 +00:00
parent 2adf176dde
commit e50dbf6ff8
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 45 additions and 42 deletions

View File

@ -19,7 +19,7 @@ import SendSats from "Element/SendSats";
import Copy from "Element/Copy"; import Copy from "Element/Copy";
import { useUserProfile } from "Feed/ProfileFeed"; import { useUserProfile } from "Feed/ProfileFeed";
import useEventPublisher from "Feed/EventPublisher"; import useEventPublisher from "Feed/EventPublisher";
import { debounce, hexToBech32 } from "Util"; import { debounce } from "Util";
import { UserMetadata } from "Nostr"; import { UserMetadata } from "Nostr";
import messages from "./messages"; import messages from "./messages";
@ -32,14 +32,13 @@ type Nip05ServiceProps = {
link: string; link: string;
supportLink: string; supportLink: string;
helpText?: boolean; helpText?: boolean;
autoUpdate?: boolean;
onChange?(h: string): void; onChange?(h: string): void;
onSuccess?(h: string): void; onSuccess?(h: string): void;
}; };
export default function Nip5Service(props: Nip05ServiceProps) { export default function Nip5Service(props: Nip05ServiceProps) {
const navigate = useNavigate(); const navigate = useNavigate();
const { helpText = true, autoUpdate = true } = props; const { helpText = true } = props;
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const pubkey = useSelector((s: RootState) => s.login.publicKey); const pubkey = useSelector((s: RootState) => s.login.publicKey);
const user = useUserProfile(pubkey); const user = useUserProfile(pubkey);
@ -49,6 +48,7 @@ export default function Nip5Service(props: Nip05ServiceProps) {
const [error, setError] = useState<ServiceError>(); const [error, setError] = useState<ServiceError>();
const [handle, setHandle] = useState<string>(""); const [handle, setHandle] = useState<string>("");
const [domain, setDomain] = useState<string>(""); const [domain, setDomain] = useState<string>("");
const [checking, setChecking] = useState(false);
const [availabilityResponse, setAvailabilityResponse] = useState<HandleAvailability>(); const [availabilityResponse, setAvailabilityResponse] = useState<HandleAvailability>();
const [registerResponse, setRegisterResponse] = useState<HandleRegisterResponse>(); const [registerResponse, setRegisterResponse] = useState<HandleRegisterResponse>();
const [showInvoice, setShowInvoice] = useState<boolean>(false); const [showInvoice, setShowInvoice] = useState<boolean>(false);
@ -120,30 +120,45 @@ export default function Nip5Service(props: Nip05ServiceProps) {
} }
}, [handle, domain, domainConfig, svc]); }, [handle, domain, domainConfig, svc]);
useEffect(() => { async function checkRegistration(rsp: HandleRegisterResponse) {
if (registerResponse && showInvoice) { const status = await svc.CheckRegistration(rsp.token);
const t = setInterval(async () => { if ("error" in status) {
const status = await svc.CheckRegistration(registerResponse.token); setError(status);
if ("error" in status) { setRegisterResponse(undefined);
setError(status); setShowInvoice(false);
setRegisterResponse(undefined); } else {
setShowInvoice(false); const result: CheckRegisterResponse = status;
if (result.paid) {
if (!result.available) {
setError({
error: "REGISTERED",
} as ServiceError);
} else { } else {
const result: CheckRegisterResponse = status; setError(undefined);
if (result.available && result.paid) { }
setShowInvoice(false); setShowInvoice(false);
setRegisterStatus(status); setRegisterStatus(status);
setRegisterResponse(undefined); setRegisterResponse(undefined);
setError(undefined); }
if (autoUpdate) { }
updateProfile(handle, domain); }
}
} useEffect(() => {
if (registerResponse && showInvoice && !checking) {
const t = setInterval(() => {
if (!checking) {
setChecking(true);
checkRegistration(registerResponse)
.then(() => setChecking(false))
.catch(e => {
console.error(e);
setChecking(false);
});
} }
}, 2_000); }, 2_000);
return () => clearInterval(t); return () => clearInterval(t);
} }
}, [registerResponse, showInvoice, svc]); }, [registerResponse, showInvoice, svc, checking]);
function mapError(e: ServiceErrorCode | undefined, t: string | null): string | undefined { function mapError(e: ServiceErrorCode | undefined, t: string | null): string | undefined {
if (e === undefined) { if (e === undefined) {
@ -233,15 +248,6 @@ export default function Nip5Service(props: Nip05ServiceProps) {
<br /> <br />
<small>{availabilityResponse.quote?.data.type}</small> <small>{availabilityResponse.quote?.data.type}</small>
</div> </div>
{!autoUpdate && (
<input
type="text"
className="f-grow mr10"
placeholder="pubkey"
value={hexToBech32("npub", pubkey)}
disabled
/>
)}
<AsyncButton onClick={() => startBuy(handle, domain)}> <AsyncButton onClick={() => startBuy(handle, domain)}>
<FormattedMessage {...messages.BuyNow} /> <FormattedMessage {...messages.BuyNow} />
</AsyncButton> </AsyncButton>
@ -285,16 +291,12 @@ export default function Nip5Service(props: Nip05ServiceProps) {
<FormattedMessage {...messages.AccountPage} /> <FormattedMessage {...messages.AccountPage} />
</a> </a>
</p> </p>
{!autoUpdate && ( <h4>
<> <FormattedMessage {...messages.ActivateNow} />
<h4> </h4>
<FormattedMessage {...messages.ActivateNow} /> <AsyncButton onClick={() => updateProfile(handle, domain)}>
</h4> <FormattedMessage {...messages.AddToProfile} />
<AsyncButton onClick={() => updateProfile(handle, domain)}> </AsyncButton>
<FormattedMessage {...messages.AddToProfile} />
</AsyncButton>
</>
)}
</div> </div>
)} )}
</> </>

View File

@ -11,7 +11,8 @@ export type ServiceErrorCode =
| "RATE_LIMITED" | "RATE_LIMITED"
| "NO_TOKEN" | "NO_TOKEN"
| "INVALID_TOKEN" | "INVALID_TOKEN"
| "NO_SUCH_PAYMENT"; | "NO_SUCH_PAYMENT"
| "INTERNAL_PAYMENT_CHECK_ERROR";
export interface ServiceError { export interface ServiceError {
error: ServiceErrorCode; error: ServiceErrorCode;