@suiware/kit
Version:
Opinionated React components and hooks for building Sui dApps.
87 lines (85 loc) • 2.66 kB
JavaScript
import { __async } from './chunk-IQXHJV5O.mjs';
import { isValidSuiAddress } from '@mysten/sui/utils';
import debounce from 'lodash.debounce';
import { useState, useCallback, useEffect } from 'react';
import { jsxs, jsx } from 'react/jsx-runtime';
// src/helpers/suins.ts
var resolveSuinsName = (suinsClient, name) => __async(null, null, function* () {
const nameRecord = yield suinsClient.getNameRecord(name);
return (nameRecord == null ? void 0 : nameRecord.targetAddress) || null;
});
var DEBOUNCE_DELAY = 500;
var AddressInput = ({
value,
onChange,
placeholder,
className = "",
disabled = false,
suinsClient
}) => {
const [inputValue, setInputValue] = useState(value);
const [error, setError] = useState(null);
const debouncedResolve = useCallback(
debounce((name) => __async(null, null, function* () {
if (!suinsClient) {
setError(
"SuinsClient is not available. Pass it through component props to make SuiNS name resolving work."
);
return;
}
try {
const resolvedAddress = yield resolveSuinsName(suinsClient, name);
if (resolvedAddress) {
setError(null);
onChange(resolvedAddress);
} else {
setError("Invalid SuiNS name");
}
} catch (err) {
setError("Failed to resolve SuiNS name");
}
}), DEBOUNCE_DELAY),
[suinsClient, onChange]
);
const handleChange = (e) => __async(null, null, function* () {
const newValue = e.target.value.trim();
setInputValue(newValue);
if (newValue.endsWith(".sui") || newValue.startsWith("@")) {
debouncedResolve(newValue);
return;
}
if (newValue && !isValidSuiAddress(newValue)) {
setError("Invalid Sui address");
onChange(newValue);
return;
}
setError(null);
onChange(newValue);
});
useEffect(() => {
setInputValue(value);
}, [value]);
useEffect(() => {
return () => {
debouncedResolve.cancel();
};
}, [debouncedResolve]);
return /* @__PURE__ */ jsxs("div", { className: `sk-address-input ${className}`, children: [
/* @__PURE__ */ jsx(
"input",
{
type: "text",
value: inputValue,
onChange: handleChange,
placeholder: placeholder || "Enter Sui address" + (suinsClient != null ? " or SuiNS name" : ""),
disabled,
className: error ? "error" : ""
}
),
error && /* @__PURE__ */ jsx("span", { children: error })
] });
};
var AddressInput_default = AddressInput;
export { AddressInput_default };
//# sourceMappingURL=chunk-Q7UZRD3B.mjs.map
//# sourceMappingURL=chunk-Q7UZRD3B.mjs.map