mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: UserSelectAutoComplete: support pasting username/email
This commit is contained in:
4 files changed
+27
-10
No files matched your search
@@ -541,11 +541,24 @@ export async function apply(ctx: Context) {
|
||||
return c.user;
|
||||
}, `Get a user by id, uname, or mail.
|
||||
Returns current user if no argument is provided.`);
|
||||
api.resolver('Query', 'users(ids: [Int], search: String, limit: Int, exact: Boolean)', '[User]', async (arg, c) => {
|
||||
if (arg.ids?.length) {
|
||||
const res = await user.getList(c.args.domainId, arg.ids);
|
||||
for (const i in res) res[i].avatarUrl = avatar(res[i].avatar);
|
||||
return Object.keys(res).map((id) => res[+id]);
|
||||
api.resolver('Query', 'users(ids: [Int], auto: [String], search: String, limit: Int, exact: Boolean)', '[User]', async (arg, c) => {
|
||||
const auto = arg.ids || arg.auto || [];
|
||||
if (auto.length) {
|
||||
const maybeId = auto.filter((i) => !Number.isNaN(+i));
|
||||
const result = [];
|
||||
if (maybeId.length) {
|
||||
const udocs = await user.getList(c.args.domainId, maybeId.map((i) => +i));
|
||||
for (const i in udocs) udocs[i].avatarUrl = avatar(udocs[i].avatar);
|
||||
result.push(...Object.values(udocs));
|
||||
}
|
||||
const notFound = auto.filter((i) => !result.find((j) => j._id === +i));
|
||||
if (notFound.length > 50) return result; // reject if too many
|
||||
for (const i of notFound) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const udoc = await user.getByUname(c.args.domainId, i) || await user.getByEmail(c.args.domainId, i);
|
||||
if (udoc) result.push(udoc);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (!arg.search) return [];
|
||||
const udoc = await user.getById(c.args.domainId, +arg.search)
|
||||
|
||||
@@ -307,10 +307,10 @@ const AutoComplete = forwardRef(function Impl<T>(props: AutoCompleteProps<T>, re
|
||||
e.preventDefault();
|
||||
const ids = text.replace(/,/g, ',').split(',').filter((v) => v?.trim().length && !selectedKeys.includes(v));
|
||||
if (!ids.length) return;
|
||||
const fetched = await props.fetchItems(ids.filter((v) => !valueCache[v]));
|
||||
const fetched = await props.fetchItems(ids);
|
||||
for (const item of fetched) valueCache[itemKey(item)] = item;
|
||||
setSelected([...selected, ...ids.map((id) => valueCache[id])]);
|
||||
setSelectedKeys([...selectedKeys, ...ids.map((id) => itemKey(valueCache[id]))]);
|
||||
setSelected([...selected, ...fetched]);
|
||||
setSelectedKeys([...selectedKeys, ...fetched.map((val) => itemKey(val))]);
|
||||
}}
|
||||
placeholder={props.placeholder}
|
||||
onBlur={() => setFocused(false)}
|
||||
|
||||
@@ -19,7 +19,7 @@ const UserSelectAutoComplete = forwardRef<AutoCompleteHandle<Udoc>, AutoComplete
|
||||
}
|
||||
`, ['data', 'users'])}
|
||||
fetchItems={(ids) => api(gql`
|
||||
users(ids: ${ids.map((i) => +i)}) {
|
||||
users(auto: ${ids}) {
|
||||
_id
|
||||
uname
|
||||
displayName
|
||||
|
||||
Reference in new issue
Block a user