From a4984de6ab8c102ecbf5da0c402033e440346977 Mon Sep 17 00:00:00 2001 From: Foysal Ahamed Date: Sat, 9 Mar 2024 21:58:55 +0000 Subject: [PATCH] :sparkles: Use labels from 3p labeler --- components/common/labels/Grid.tsx | 13 +++++++---- lib/client.ts | 39 +++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/components/common/labels/Grid.tsx b/components/common/labels/Grid.tsx index 5e683a79..1cef1874 100644 --- a/components/common/labels/Grid.tsx +++ b/components/common/labels/Grid.tsx @@ -1,6 +1,7 @@ +import client from '@/lib/client' import { useState } from 'react' import Select from 'react-tailwindcss-select' -import { buildAllLabelOptions, ALL_LABELS, LabelGroupInfo } from './util' +import { ALL_LABELS, LabelGroupInfo } from './util' const EMPTY_ARR = [] type SelectProps = React.ComponentProps @@ -21,10 +22,12 @@ export const LabelSelector = (props: LabelsProps) => { value: label, })), ) - const allOptions = buildAllLabelOptions(defaultLabels, options) - const selectorOptions = Object.values(ALL_LABELS).map((labelOption) => ({ - label: labelOption.identifier, - value: labelOption.identifier, + const selectorOptions = [ + ...(client.session?.config.labeler?.policies?.['labelValues'] || []), + ...Object.values(ALL_LABELS).map(({ identifier }) => identifier), + ].map((label) => ({ + label, + value: label, })) // TODO: selected label text doesn't feel very nice here diff --git a/lib/client.ts b/lib/client.ts index bb549e65..41b3d211 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -1,7 +1,22 @@ -import { AtpAgent, AtpServiceClient, AtpSessionData } from '@atproto/api' +import { + AppBskyLabelerDefs, + AtpAgent, + AtpServiceClient, + AtpSessionData, +} from '@atproto/api' interface ClientSession extends AtpSessionData { service: string + config: { + labeler: + | AppBskyLabelerDefs.LabelerView + | AppBskyLabelerDefs.LabelerViewDetailed + | { + $type: string + [k: string]: unknown + } + | null + } } // exported api @@ -54,8 +69,24 @@ class ClientManager extends EventTarget { { did: login.did }, { headers: this.proxyHeaders() }, ) + let labeler + const serviceDid = this.getServiceDid() + if (serviceDid) { + const { + data: { views }, + } = await agent.api.app.bsky.labeler.getServices( + { + dids: [serviceDid.split('#')[0]], + }, + { headers: this.proxyHeaders() }, + ) + labeler = views[0] + } this._session = { service, + config: { + labeler, + }, accessJwt: login.accessJwt, refreshJwt: login.refreshJwt, handle: login.handle, @@ -76,8 +107,12 @@ class ClientManager extends EventTarget { this._emit('change') } + getServiceDid(override?: string) { + return override ?? process.env.NEXT_PUBLIC_OZONE_SERVICE_DID + } + proxyHeaders(override?: string): Record { - const proxy = override ?? process.env.NEXT_PUBLIC_OZONE_SERVICE_DID + const proxy = this.getServiceDid(override) return proxy ? { 'atproto-proxy': proxy } : {} }