diff --git a/components/common/labels/Grid.tsx b/components/common/labels/Grid.tsx index 55b75e9c..ebeb4f47 100644 --- a/components/common/labels/Grid.tsx +++ b/components/common/labels/Grid.tsx @@ -1,5 +1,6 @@ import { useState } from 'react' import Select from 'react-tailwindcss-select' +import client from '@/lib/client' import { labelOptions, groupLabelList, diff --git a/components/common/labels/util.ts b/components/common/labels/util.ts index 034eabe7..2fdf5369 100644 --- a/components/common/labels/util.ts +++ b/components/common/labels/util.ts @@ -1,3 +1,4 @@ +import client from '@/lib/client' import { unique } from '@/lib/util' import { AppBskyActorDefs, @@ -85,6 +86,17 @@ export const LabelGroupInfo: Record< [LABEL_GROUPS.misinfo.id]: { color: '#530303', }, + custom: { + color: '#218734', + strings: { + settings: { + en: { + name: 'Custom Labels', + description: 'Labels from labeler service', + }, + }, + }, + }, uncategorized: { strings: { settings: { @@ -106,12 +118,19 @@ const labelGroupsRequiringBlur = [ export const groupLabelList = (labels: string[]): GroupedLabelList => { const groupedList: GroupedLabelList = {} + const customLabels = getCustomLabels() labels.forEach((label) => { // SELF_FLAG is embedded in the label value so when grouping, we have to take it out of the value const cleanedLabel = unFlagSelfLabel(label) const group = LABELS[cleanedLabel] - const groupId = group?.groupId || 'uncategorized' + let groupId = 'uncategorized' + + if (group?.groupId) { + groupId = group.groupId + } else if (customLabels?.includes(cleanedLabel)) { + groupId = 'custom' + } if (groupedList[groupId]) { groupedList[groupId].labels.push(label) @@ -127,9 +146,15 @@ export const groupLabelList = (labels: string[]): GroupedLabelList => { return groupedList } +const getCustomLabels = () => + client.session?.config.labeler?.policies.labelValues + export const getLabelGroupInfo = (label: string): LabelGroupInfoRecord => { + const customLabels = getCustomLabels() const group = LABELS[label] - const groupId = group?.groupId || 'uncategorized' + const groupId = + group?.groupId || + (customLabels?.includes(label) ? 'custom' : 'uncategorized') return { // TODO: We shouldn't have to do this, there's a weird type def somewhere that's causing this @@ -178,5 +203,6 @@ export const buildAllLabelOptions = ( defaultLabels: string[], options: string[], ) => { - return unique([...defaultLabels, ...options]).sort() + const customLabels = getCustomLabels() + return unique([...defaultLabels, ...options, ...(customLabels || [])]).sort() } diff --git a/lib/client-config.ts b/lib/client-config.ts index 78e7681a..45d1247b 100644 --- a/lib/client-config.ts +++ b/lib/client-config.ts @@ -34,6 +34,7 @@ export async function getConfig(labelerDid?: string): Promise { doc, meta, handle, + labeler: record, matching: { service: labelerUrl && meta @@ -98,7 +99,7 @@ async function getLabelerServiceRecord(pdsUrl: string, did: string) { if (!recordInfo?.['value'] || typeof recordInfo['value'] !== 'object') { return null } - return recordInfo['value'] as Record + return recordInfo['value'] as TemporaryLabelerServiceDef } function normalizeUrl(url: string) { @@ -112,9 +113,15 @@ export function withDocAndMeta(config: OzoneConfig) { } export type OzoneMeta = { did: string; url: string; publicKey: string } +export type TemporaryLabelerServiceDef = { + policies: { + labelValues: string[] + } +} export type OzoneConfig = { did: string + labeler: TemporaryLabelerServiceDef | null handle: string | null meta: OzoneMeta | null doc: DidDocData | null