|
| 1 | +/* eslint-disable sort-keys */ |
| 2 | +import { createAction } from 'typesafe-actions' |
| 3 | +import type { FileBoxInterface } from 'file-box' |
| 4 | + |
| 5 | +import { MessageType } from './message.js' |
| 6 | +import type { LocationPayload } from './location.js' |
| 7 | +import type { UrlLinkPayload } from './url-link' |
| 8 | +import type { MiniProgramPayload } from './mini-program.js' |
| 9 | +import type { PostPayload } from './post.js' |
| 10 | + |
| 11 | +const payloadContact = (contactId: string) => ({ contactId }) |
| 12 | +const payloadFilebox = (filebox: string | FileBoxInterface) => ({ filebox }) |
| 13 | +const payloadText = (text: string) => ({ text }) |
| 14 | +// expand/merge the payload altogether |
| 15 | +const payloadLocation = (locationPayload: LocationPayload) => ({ ...locationPayload }) |
| 16 | +const payloadMiniProgram = (miniProgramPayload: MiniProgramPayload) => ({ ...miniProgramPayload }) |
| 17 | +const payloadUrlLink = (urlLinkPayload: UrlLinkPayload) => ({ ...urlLinkPayload }) |
| 18 | +const payloadPost = (postPayload: PostPayload) => ({ ...postPayload }) |
| 19 | + |
| 20 | +/** |
| 21 | + * using `types` as a static typed string name list for `createAction` |
| 22 | + * |
| 23 | + * Huan(202201): if we remove the `(() => ({}))()`, then the typing will fail. |
| 24 | + * FIXME: remove the `(() => ({}))()` after we fix the issue. |
| 25 | + */ |
| 26 | +const sayableTypes = (() => ({ |
| 27 | + ...Object.keys(MessageType) |
| 28 | + .filter(k => isNaN(Number(k))) |
| 29 | + .reduce((acc, cur) => ({ |
| 30 | + ...acc, |
| 31 | + [cur]: cur, |
| 32 | + }), {}), |
| 33 | +} as { |
| 34 | + [k in keyof typeof MessageType]: k |
| 35 | +}))() |
| 36 | + |
| 37 | +/** |
| 38 | + * Simple data |
| 39 | + */ |
| 40 | +const contact = createAction(sayableTypes.Contact, payloadContact)() |
| 41 | +const text = createAction(sayableTypes.Text, payloadText)() |
| 42 | +// (conversationId: string, text: string, mentionIdList?: string[]) => ({ conversationId, mentionIdList, text } |
| 43 | + |
| 44 | +/** |
| 45 | +* FileBoxs |
| 46 | +*/ |
| 47 | +const attatchment = createAction(sayableTypes.Attachment, payloadFilebox)() |
| 48 | +const audio = createAction(sayableTypes.Audio, payloadFilebox)() |
| 49 | +const emoticon = createAction(sayableTypes.Emoticon, payloadFilebox)() |
| 50 | +const image = createAction(sayableTypes.Image, payloadFilebox)() |
| 51 | +const video = createAction(sayableTypes.Video, payloadFilebox)() |
| 52 | + |
| 53 | +/** |
| 54 | +* Payload data |
| 55 | +*/ |
| 56 | +const location = createAction(sayableTypes.Location, payloadLocation)() |
| 57 | +const miniProgram = createAction(sayableTypes.MiniProgram, payloadMiniProgram)() |
| 58 | +const url = createAction(sayableTypes.Url, payloadUrlLink)() |
| 59 | +const post = createAction(sayableTypes.Post, payloadPost)() |
| 60 | + |
| 61 | +type SayablePayloadPost = ReturnType<typeof post> |
| 62 | + |
| 63 | +/** |
| 64 | + * Huan(202201): Recursive type references |
| 65 | + * @link https://github.com/microsoft/TypeScript/pull/33050#issuecomment-1002455128 |
| 66 | + */ |
| 67 | +const sayablePayloadsNoPost = { |
| 68 | + attatchment, |
| 69 | + audio, |
| 70 | + contact, |
| 71 | + emoticon, |
| 72 | + image, |
| 73 | + location, |
| 74 | + miniProgram, |
| 75 | + text, |
| 76 | + url, |
| 77 | + video, |
| 78 | +} as const |
| 79 | + |
| 80 | +const sayablePayloads = { |
| 81 | + ...sayablePayloadsNoPost, |
| 82 | + post, |
| 83 | +} as const |
| 84 | + |
| 85 | +/** |
| 86 | + * Huan(202201): Recursive type references |
| 87 | + * @link https://github.com/microsoft/TypeScript/pull/33050#issuecomment-1002455128 |
| 88 | + */ |
| 89 | +type SayablePayloadNoPost = ReturnType<typeof sayablePayloadsNoPost[keyof typeof sayablePayloadsNoPost]> |
| 90 | + |
| 91 | +// TODO: add an unit test to confirm that all unsupported type are listed here |
| 92 | +type SayablePayloadUnsupportedType = |
| 93 | + | 'ChatHistory' |
| 94 | + | 'GroupNote' |
| 95 | + | 'Recalled' |
| 96 | + | 'RedEnvelope' |
| 97 | + | 'Transfer' |
| 98 | + | 'Unknown' |
| 99 | + |
| 100 | +export { |
| 101 | + sayablePayloads, |
| 102 | + sayableTypes, |
| 103 | + type SayablePayloadNoPost, |
| 104 | + type SayablePayloadPost, |
| 105 | + |
| 106 | + type SayablePayloadUnsupportedType, |
| 107 | +} |
0 commit comments