Changeset View
Changeset View
Standalone View
Standalone View
lib/utils/entity-text.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import t, { type TInterface, type TUnion } from 'tcomb'; | |||||
import type { GetENSNames } from './ens-helpers.js'; | import type { GetENSNames } from './ens-helpers.js'; | ||||
import { tID, tShape, tString } from './validation-utils.js'; | |||||
import { useENSNames } from '../hooks/ens-cache.js'; | import { useENSNames } from '../hooks/ens-cache.js'; | ||||
import { threadNoun } from '../shared/thread-utils.js'; | import { threadNoun } from '../shared/thread-utils.js'; | ||||
import { stringForUser } from '../shared/user-utils.js'; | import { stringForUser } from '../shared/user-utils.js'; | ||||
import { type ThreadType, threadTypes } from '../types/thread-types-enum.js'; | import { | ||||
type ThreadType, | |||||
threadTypes, | |||||
threadTypeValidator, | |||||
} from '../types/thread-types-enum.js'; | |||||
import { type RawThreadInfo, type ThreadInfo } from '../types/thread-types.js'; | import { type RawThreadInfo, type ThreadInfo } from '../types/thread-types.js'; | ||||
import { basePluralize } from '../utils/text-utils.js'; | import { basePluralize } from '../utils/text-utils.js'; | ||||
type UserEntity = { | type UserEntity = { | ||||
+type: 'user', | +type: 'user', | ||||
+id: string, | +id: string, | ||||
+username?: ?string, | +username?: ?string, | ||||
+isViewer?: ?boolean, | +isViewer?: ?boolean, | ||||
+possessive?: ?boolean, // eg. `user's` instead of `user` | +possessive?: ?boolean, // eg. `user's` instead of `user` | ||||
}; | }; | ||||
export const userEntityValidator: TInterface<UserEntity> = tShape<UserEntity>({ | |||||
type: tString('user'), | |||||
id: t.String, | |||||
username: t.maybe(t.String), | |||||
isViewer: t.maybe(t.Boolean), | |||||
possessive: t.maybe(t.Boolean), | |||||
}); | |||||
// Comments explain how thread name will appear from user4's perspective | // Comments explain how thread name will appear from user4's perspective | ||||
export type ThreadEntity = | export type ThreadEntity = | ||||
| { | | { | ||||
+type: 'thread', | +type: 'thread', | ||||
+id: string, | +id: string, | ||||
+name?: ?string, | +name?: ?string, | ||||
// displays threadInfo.name if set, or 'user1, user2, and user3' | // displays threadInfo.name if set, or 'user1, user2, and user3' | ||||
Show All 14 Lines | | { | ||||
+display: 'shortName', | +display: 'shortName', | ||||
+threadType?: ?ThreadType, | +threadType?: ?ThreadType, | ||||
+parentThreadID?: ?string, | +parentThreadID?: ?string, | ||||
+alwaysDisplayShortName?: ?boolean, // don't default to name | +alwaysDisplayShortName?: ?boolean, // don't default to name | ||||
+subchannel?: ?boolean, // short name should be "subchannel" | +subchannel?: ?boolean, // short name should be "subchannel" | ||||
+possessive?: ?boolean, // eg. `this thread's` instead of `this thread` | +possessive?: ?boolean, // eg. `this thread's` instead of `this thread` | ||||
}; | }; | ||||
export const threadEntityValidator: TUnion<ThreadEntity> = t.union([ | |||||
tShape({ | |||||
type: tString('thread'), | |||||
id: tID, | |||||
name: t.maybe(t.String), | |||||
display: tString('uiName'), | |||||
uiName: t.union([t.list(userEntityValidator), t.String]), | |||||
ifJustViewer: t.maybe(t.enums.of(['just_you_string', 'viewer_username'])), | |||||
}), | |||||
tShape({ | |||||
type: tString('thread'), | |||||
id: tID, | |||||
name: t.maybe(t.String), | |||||
display: tString('shortName'), | |||||
threadType: t.maybe(threadTypeValidator), | |||||
parentThreadID: t.maybe(tID), | |||||
alwaysDisplayShortName: t.maybe(t.Boolean), | |||||
subchannel: t.maybe(t.Boolean), | |||||
possessive: t.maybe(t.Boolean), | |||||
}), | |||||
]); | |||||
type ColorEntity = { | type ColorEntity = { | ||||
+type: 'color', | +type: 'color', | ||||
+hex: string, | +hex: string, | ||||
}; | }; | ||||
type EntityTextComponent = UserEntity | ThreadEntity | ColorEntity | string; | type EntityTextComponent = UserEntity | ThreadEntity | ColorEntity | string; | ||||
export type EntityText = $ReadOnlyArray<EntityTextComponent>; | export type EntityText = $ReadOnlyArray<EntityTextComponent>; | ||||
▲ Show 20 Lines • Show All 488 Lines • Show Last 20 Lines |