mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {
3 AppBskyLabelerDefs,
4 BskyAgent,
5 ComAtprotoLabelDefs,
6 InterpretedLabelValueDefinition,
7 LABELS,
8 ModerationCause,
9 ModerationOpts,
10 ModerationUI,
11} from '@atproto/api'
12
13import {sanitizeDisplayName} from '#/lib/strings/display-names'
14import {sanitizeHandle} from '#/lib/strings/handles'
15import {AppModerationCause} from '#/components/Pills'
16
17export function getModerationCauseKey(
18 cause: ModerationCause | AppModerationCause,
19): string {
20 const source =
21 cause.source.type === 'labeler'
22 ? cause.source.did
23 : cause.source.type === 'list'
24 ? cause.source.list.uri
25 : 'user'
26 if (cause.type === 'label') {
27 return `label:${cause.label.val}:${source}`
28 }
29 return `${cause.type}:${source}`
30}
31
32export function isJustAMute(modui: ModerationUI): boolean {
33 return modui.filters.length === 1 && modui.filters[0].type === 'muted'
34}
35
36export function getLabelingServiceTitle({
37 displayName,
38 handle,
39}: {
40 displayName?: string
41 handle: string
42}) {
43 return displayName
44 ? sanitizeDisplayName(displayName)
45 : sanitizeHandle(handle, '@')
46}
47
48export function lookupLabelValueDefinition(
49 labelValue: string,
50 customDefs: InterpretedLabelValueDefinition[] | undefined,
51): InterpretedLabelValueDefinition | undefined {
52 let def
53 if (!labelValue.startsWith('!') && customDefs) {
54 def = customDefs.find(d => d.identifier === labelValue)
55 }
56 if (!def) {
57 def = LABELS[labelValue as keyof typeof LABELS]
58 }
59 return def
60}
61
62export function isAppLabeler(
63 labeler:
64 | string
65 | AppBskyLabelerDefs.LabelerView
66 | AppBskyLabelerDefs.LabelerViewDetailed,
67): boolean {
68 if (typeof labeler === 'string') {
69 return BskyAgent.appLabelers.includes(labeler)
70 }
71 return BskyAgent.appLabelers.includes(labeler.creator.did)
72}
73
74export function isLabelerSubscribed(
75 labeler:
76 | string
77 | AppBskyLabelerDefs.LabelerView
78 | AppBskyLabelerDefs.LabelerViewDetailed,
79 modOpts: ModerationOpts,
80) {
81 labeler = typeof labeler === 'string' ? labeler : labeler.creator.did
82 if (isAppLabeler(labeler)) {
83 return true
84 }
85 return modOpts.prefs.labelers.find(l => l.did === labeler)
86}
87
88export type Subject =
89 | {
90 uri: string
91 cid: string
92 }
93 | {
94 did: string
95 }
96
97export function useLabelSubject({label}: {label: ComAtprotoLabelDefs.Label}): {
98 subject: Subject
99} {
100 return React.useMemo(() => {
101 const {cid, uri} = label
102 if (cid) {
103 return {
104 subject: {
105 uri,
106 cid,
107 },
108 }
109 } else {
110 return {
111 subject: {
112 did: uri,
113 },
114 }
115 }
116 }, [label])
117}