Skip to content

Commit

Permalink
Fix label resolution (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
thieflord06 authored Jan 17, 2025
2 parents 755b7d1 + bcbb34c commit a05e3bd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/api/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AtpAgent } from '@atproto/api';

export const oldXrpc = 'https://bsky.social/xrpc';
export const newXrpc = 'https://bsky.network/xrpc';
export const publicXrpc = 'https://public.api.bsky.app/xrpc';
export const publicXrpc = 'https://api.bsky.app/xrpc';

export const atClient = new AtpAgent({ service: oldXrpc });
patchBskyAgent(atClient);
Expand Down
27 changes: 16 additions & 11 deletions src/api/labled.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { fetchClearskyApi, unwrapShortDID, publicAtClient } from './core';
* @param {string[]} lablerDids
* @param {AbortSignal} signal
*/
export async function getLabeled(fullDid, lablerDids, signal) {
async function getLabeled(fullDid, lablerDids, signal) {
if (!fullDid) {
return [];
}
const req = await publicAtClient.getProfile(
const res = await publicAtClient.getProfile(
{ actor: fullDid },
{
headers: {
Expand All @@ -23,7 +23,7 @@ export async function getLabeled(fullDid, lablerDids, signal) {
}
);

return req.data.labels || [];
return res.data.labels || [];
}

/**
Expand Down Expand Up @@ -70,11 +70,8 @@ function* getDidSlices(fullDid, lablerDids, signal) {
if (start >= lablerDids.length) {
return;
}
yield getLabeled(
fullDid,
lablerDids.slice(start, start + BATCH_SIZE),
signal
);
const labelers = lablerDids.slice(start, start + BATCH_SIZE);
yield getLabeled(fullDid, labelers, signal);
page += 1;
}
}
Expand All @@ -84,14 +81,22 @@ function* getDidSlices(fullDid, lablerDids, signal) {
*
* @param {string|undefined} did
* @param {string[]|undefined} lablerDids
* @returns
*/
export function useLabeled(did, lablerDids) {
const fullDid = unwrapShortDID(did);
return useQuery({
enabled: !!fullDid && !!lablerDids?.length,
queryKey: ['labeled', fullDid, lablerDids],
queryFn: ({ signal }) =>
Promise.all(getDidSlices(fullDid || '', lablerDids, signal)),
queryFn: async ({ signal }) => {
const labels = await Promise.all(
getDidSlices(fullDid || '', lablerDids, signal)
);
const vals = new Set();
return labels.flat().filter((label) => {
if (vals.has(label.val)) return false;
vals.add(label.val);
return true;
});
},
});
}
22 changes: 3 additions & 19 deletions src/detail-panels/labeled/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,6 @@ export default function LabeledPanel({}) {
const { data: labelers, isLoading: isLoadingLabelers } = useLabelers();

const { data: labels, isLoading } = useLabeled(did, labelers);
//Reduce pages to array of labels
const flatLabels = useMemo(() => {
if (!labels) {
return [];
}
if (labels) {
return (
labels
.flat()
//unique label.val
.filter(
(label, i, arr) => arr.findIndex((l) => l.val === label.val) === i
)
);
}
}, [labels]);

return (
<div
Expand All @@ -122,14 +106,14 @@ export default function LabeledPanel({}) {
minHeight: '100%',
}}
>
<h3 className="labeled-header">Labeled {flatLabels?.length} Times</h3>
<h3 className="labeled-header">Labeled {labels?.length} Times</h3>

{isLoadingLabelers || isLoading ? (
<div>Loading...</div>
) : (
<>
{flatLabels && flatLabels.length ? (
<LabeledList labels={flatLabels} />
{labels?.length ? (
<LabeledList labels={labels} />
) : (
<div className="labeled-view no-labels">No labels</div>
)}
Expand Down

0 comments on commit a05e3bd

Please sign in to comment.