@atlaskit/editor-plugin-avatar-group
Version:
Avatar Group plugin for @atlaskit/editor-core.
47 lines • 1.38 kB
JavaScript
import { logException } from '@atlaskit/editor-common/monitoring';
const AGG_PATH = '/gateway/api/graphql';
const USERS_OPERATION_NAME = 'AvatarGroupUsersQuery';
const USERS_QUERY = `
query ${USERS_OPERATION_NAME}($accountIds: [ID!]!) {
users(accountIds: $accountIds) {
accountId
name
}
}
`;
export const fetchUserNames = async accountIds => {
if (!accountIds.length) {
return {};
}
try {
var _json$data$users, _json$data;
const response = await fetch(AGG_PATH, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
operationName: USERS_OPERATION_NAME,
query: USERS_QUERY,
variables: {
accountIds
}
})
});
if (!response.ok) {
throw new Error(`AGG user request failed: ${response.status}`);
}
const json = await response.json();
return ((_json$data$users = (_json$data = json.data) === null || _json$data === void 0 ? void 0 : _json$data.users) !== null && _json$data$users !== void 0 ? _json$data$users : []).reduce((namesById, user) => {
if (user.accountId && user.name) {
namesById[user.accountId] = user.name;
}
return namesById;
}, {});
} catch (error) {
logException(error, {
location: 'editor-plugin-avatar-group/fetchUserNames'
});
return {};
}
};