Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/entity-helpers.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { | import { | ||||
ET, | ET, | ||||
useENSNamesForEntityText, | useENSNamesForEntityText, | ||||
entityTextToRawString, | entityTextToRawString, | ||||
} from './entity-text.js'; | } from './entity-text.js'; | ||||
import { getAvatarForThreadEntity } from '../shared/avatar-utils.js'; | |||||
import type { ThreadInfo, ResolvedThreadInfo } from '../types/thread-types.js'; | import type { ThreadInfo, ResolvedThreadInfo } from '../types/thread-types.js'; | ||||
import { values } from '../utils/objects.js'; | import { values } from '../utils/objects.js'; | ||||
function useResolvedThreadInfos( | function useResolvedThreadInfos( | ||||
threadInfos: $ReadOnlyArray<ThreadInfo>, | threadInfos: $ReadOnlyArray<ThreadInfo>, | ||||
): $ReadOnlyArray<ResolvedThreadInfo> { | ): $ReadOnlyArray<ResolvedThreadInfo> { | ||||
const entityText = React.useMemo( | const entityText = React.useMemo( | ||||
() => threadInfos.map(threadInfo => threadInfo.uiName), | () => threadInfos.map(threadInfo => threadInfo.uiName), | ||||
Show All 11 Lines | () => | ||||
// Flow wants return { ...threadInfo, uiName: threadInfo.uiName } | // Flow wants return { ...threadInfo, uiName: threadInfo.uiName } | ||||
// but that's wasteful and unneeded, so we any-cast here | // but that's wasteful and unneeded, so we any-cast here | ||||
return (threadInfo: any); | return (threadInfo: any); | ||||
} | } | ||||
const resolvedThreadEntity = withENSNames[i]; | const resolvedThreadEntity = withENSNames[i]; | ||||
return { | return { | ||||
...threadInfo, | ...threadInfo, | ||||
uiName: entityTextToRawString([resolvedThreadEntity]), | uiName: entityTextToRawString([resolvedThreadEntity]), | ||||
avatar: getAvatarForThreadEntity(resolvedThreadEntity), | |||||
}; | }; | ||||
}), | }), | ||||
[threadInfos, withENSNames], | [threadInfos, withENSNames], | ||||
); | ); | ||||
} | } | ||||
function useResolvedOptionalThreadInfos( | function useResolvedOptionalThreadInfos( | ||||
threadInfos: ?$ReadOnlyArray<ThreadInfo>, | threadInfos: ?$ReadOnlyArray<ThreadInfo>, | ||||
Show All 20 Lines | return threadInfos.map((threadInfo, i) => { | ||||
// Flow wants return { ...threadInfo, uiName: threadInfo.uiName } | // Flow wants return { ...threadInfo, uiName: threadInfo.uiName } | ||||
// but that's wasteful and unneeded, so we any-cast here | // but that's wasteful and unneeded, so we any-cast here | ||||
return (threadInfo: any); | return (threadInfo: any); | ||||
} | } | ||||
const resolvedThreadEntity = withENSNames[i]; | const resolvedThreadEntity = withENSNames[i]; | ||||
return { | return { | ||||
...threadInfo, | ...threadInfo, | ||||
uiName: entityTextToRawString([resolvedThreadEntity]), | uiName: entityTextToRawString([resolvedThreadEntity]), | ||||
avatar: getAvatarForThreadEntity(resolvedThreadEntity), | |||||
}; | }; | ||||
}); | }); | ||||
}, [threadInfos, withENSNames]); | }, [threadInfos, withENSNames]); | ||||
} | } | ||||
function useResolvedThreadInfosObj(threadInfosObj: { | function useResolvedThreadInfosObj(threadInfosObj: { | ||||
+[id: string]: ThreadInfo, | +[id: string]: ThreadInfo, | ||||
}): { +[id: string]: ResolvedThreadInfo } { | }): { +[id: string]: ResolvedThreadInfo } { | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |