Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show custom labels from labeler service in ozone #51

Merged
merged 22 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/common/labels/Grid.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from 'react'
import Select from 'react-tailwindcss-select'
import client from '@/lib/client'
import {
labelOptions,
groupLabelList,
Expand Down
32 changes: 29 additions & 3 deletions components/common/labels/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import client from '@/lib/client'
import { unique } from '@/lib/util'
import {
AppBskyActorDefs,
Expand Down Expand Up @@ -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: {
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
}
9 changes: 8 additions & 1 deletion lib/client-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function getConfig(labelerDid?: string): Promise<OzoneConfig> {
doc,
meta,
handle,
labeler: record,
matching: {
service:
labelerUrl && meta
Expand Down Expand Up @@ -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<string, undefined>
return recordInfo['value'] as TemporaryLabelerServiceDef
}

function normalizeUrl(url: string) {
Expand All @@ -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
Expand Down
Loading