diff --git a/lib/utils/entity-text.js b/lib/utils/entity-text.js --- a/lib/utils/entity-text.js +++ b/lib/utils/entity-text.js @@ -13,6 +13,7 @@ type ThreadInfo, } from '../types/thread-types'; import { basePluralize } from '../utils/text-utils'; +import type { GetENSNames } from './ens-helpers'; type UserEntity = { +type: 'user', @@ -335,24 +336,36 @@ ); } +type TextEntity = { +type: 'text', +text: string }; +type EntityTextComponentAsObject = + | UserEntity + | ThreadEntity + | ColorEntity + | TextEntity; +function entityTextToObjects( + entityText: EntityText, +): EntityTextComponentAsObject[] { + return entityText.map(entity => + typeof entity === 'string' ? { type: 'text', text: entity } : entity, + ); +} +function entityTextFromObjects( + objects: $ReadOnlyArray, +): EntityText { + return objects.map(entity => (entity.type === 'text' ? entity.text : entity)); +} + function useENSNamesForEntityText(entityText: ?EntityText): ?EntityText { - const allObjects = React.useMemo(() => { - if (!entityText) { - return []; - } - return entityText.map(entity => - typeof entity === 'string' ? { type: 'text', text: entity } : entity, - ); - }, [entityText]); + const allObjects = React.useMemo( + () => (entityText ? entityTextToObjects(entityText) : []), + [entityText], + ); const objectsWithENSNames = useENSNames(allObjects); - return React.useMemo(() => { - if (!entityText) { - return entityText; - } - return objectsWithENSNames.map(entity => - entity.type === 'text' ? entity.text : entity, - ); - }, [entityText, objectsWithENSNames]); + return React.useMemo( + () => + entityText ? entityTextFromObjects(objectsWithENSNames) : entityText, + [entityText, objectsWithENSNames], + ); } function useEntityTextAsString( @@ -368,6 +381,25 @@ }, [withENSNames, params]); } +async function getEntityTextAsString( + entityText: ?EntityText, + getENSNames: ?GetENSNames, + params?: EntityTextToRawStringParams, +): Promise { + if (!entityText) { + return entityText; + } + + let resolvedEntityText = entityText; + if (getENSNames) { + const allObjects = entityTextToObjects(entityText); + const objectsWithENSNames = await getENSNames(allObjects); + resolvedEntityText = entityTextFromObjects(objectsWithENSNames); + } + + return entityTextToRawString(resolvedEntityText, params); +} + export { ET, entityTextToRawString, @@ -375,4 +407,5 @@ pluralizeEntityText, useENSNamesForEntityText, useEntityTextAsString, + getEntityTextAsString, };