mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {
3 BSKY_LABELER_DID,
4 ModerationCause,
5 ModerationCauseSource,
6} from '@atproto/api'
7import {msg} from '@lingui/macro'
8import {useLingui} from '@lingui/react'
9
10import {useLabelDefinitions} from '#/state/preferences'
11import {useSession} from '#/state/session'
12import {CircleBanSign_Stroke2_Corner0_Rounded as CircleBanSign} from '#/components/icons/CircleBanSign'
13import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
14import {Props as SVGIconProps} from '#/components/icons/common'
15import {EyeSlash_Stroke2_Corner0_Rounded as EyeSlash} from '#/components/icons/EyeSlash'
16import {Warning_Stroke2_Corner0_Rounded as Warning} from '#/components/icons/Warning'
17import {AppModerationCause} from '#/components/Pills'
18import {useGlobalLabelStrings} from './useGlobalLabelStrings'
19import {getDefinition, getLabelStrings} from './useLabelInfo'
20
21export interface ModerationCauseDescription {
22 icon: React.ComponentType<SVGIconProps>
23 name: string
24 description: string
25 source?: string
26 sourceType?: ModerationCauseSource['type']
27 sourceAvi?: string
28 sourceDid?: string
29}
30
31export function useModerationCauseDescription(
32 cause: ModerationCause | AppModerationCause | undefined,
33): ModerationCauseDescription {
34 const {currentAccount} = useSession()
35 const {_, i18n} = useLingui()
36 const {labelDefs, labelers} = useLabelDefinitions()
37 const globalLabelStrings = useGlobalLabelStrings()
38
39 return React.useMemo(() => {
40 if (!cause) {
41 return {
42 icon: Warning,
43 name: _(msg`Content Warning`),
44 description: _(
45 msg`Moderator has chosen to set a general warning on the content.`,
46 ),
47 }
48 }
49 if (cause.type === 'blocking') {
50 if (cause.source.type === 'list') {
51 return {
52 icon: CircleBanSign,
53 name: _(msg`User Blocked by "${cause.source.list.name}"`),
54 description: _(
55 msg`You have blocked this user. You cannot view their content.`,
56 ),
57 }
58 } else {
59 return {
60 icon: CircleBanSign,
61 name: _(msg`User Blocked`),
62 description: _(
63 msg`You have blocked this user. You cannot view their content.`,
64 ),
65 }
66 }
67 }
68 if (cause.type === 'blocked-by') {
69 return {
70 icon: CircleBanSign,
71 name: _(msg`User Blocking You`),
72 description: _(
73 msg`This user has blocked you. You cannot view their content.`,
74 ),
75 }
76 }
77 if (cause.type === 'block-other') {
78 return {
79 icon: CircleBanSign,
80 name: _(msg`Content Not Available`),
81 description: _(
82 msg`This content is not available because one of the users involved has blocked the other.`,
83 ),
84 }
85 }
86 if (cause.type === 'muted') {
87 if (cause.source.type === 'list') {
88 return {
89 icon: EyeSlash,
90 name: _(msg`Muted by "${cause.source.list.name}"`),
91 description: _(msg`You have muted this user`),
92 }
93 } else {
94 return {
95 icon: EyeSlash,
96 name: _(msg`Account Muted`),
97 description: _(msg`You have muted this account.`),
98 }
99 }
100 }
101 if (cause.type === 'mute-word') {
102 return {
103 icon: EyeSlash,
104 name: _(msg`Post Hidden by Muted Word`),
105 description: _(
106 msg`You've chosen to hide a word or tag within this post.`,
107 ),
108 }
109 }
110 if (cause.type === 'hidden') {
111 return {
112 icon: EyeSlash,
113 name: _(msg`Post Hidden by You`),
114 description: _(msg`You have hidden this post`),
115 }
116 }
117 if (cause.type === 'reply-hidden') {
118 const isMe = currentAccount?.did === cause.source.did
119 return {
120 icon: EyeSlash,
121 name: isMe
122 ? _(msg`Reply Hidden by You`)
123 : _(msg`Reply Hidden by Thread Author`),
124 description: isMe
125 ? _(msg`You hid this reply.`)
126 : _(msg`The author of this thread has hidden this reply.`),
127 }
128 }
129 if (cause.type === 'label') {
130 const def = cause.labelDef || getDefinition(labelDefs, cause.label)
131 const strings = getLabelStrings(i18n.locale, globalLabelStrings, def)
132 const labeler = labelers.find(l => l.creator.did === cause.label.src)
133 let source =
134 labeler?.creator.displayName ||
135 (labeler?.creator.handle ? '@' + labeler?.creator.handle : undefined)
136 if (!source) {
137 if (cause.label.src === BSKY_LABELER_DID) {
138 source = 'Bluesky Moderation Service'
139 } else {
140 source = cause.label.src
141 }
142 }
143 if (def.identifier === 'porn' || def.identifier === 'sexual') {
144 strings.name = _(msg`Adult Content`)
145 }
146
147 return {
148 icon:
149 def.identifier === '!no-unauthenticated'
150 ? EyeSlash
151 : def.severity === 'alert'
152 ? Warning
153 : CircleInfo,
154 name: strings.name,
155 description: strings.description,
156 source,
157 sourceType: cause.source.type,
158 sourceAvi: labeler?.creator.avatar,
159 sourceDid: cause.label.src,
160 }
161 }
162 // should never happen
163 return {
164 icon: CircleInfo,
165 name: '',
166 description: ``,
167 }
168 }, [
169 labelDefs,
170 labelers,
171 globalLabelStrings,
172 cause,
173 _,
174 i18n.locale,
175 currentAccount?.did,
176 ])
177}