diff --git a/src/lib/components/MatchTypeFilter.tsx b/src/lib/components/MatchTypeFilter.tsx index d509ab5..0eb4b8b 100644 --- a/src/lib/components/MatchTypeFilter.tsx +++ b/src/lib/components/MatchTypeFilter.tsx @@ -1,6 +1,6 @@ import React from "react"; import { BSKY_USER_MATCH_TYPE, MATCH_TYPE_LABEL_AND_COLOR } from "../constants"; -import type { MatchType } from "../hooks/useRetrieveBskyUsers"; +import type { MatchType } from "../../types"; export type MatchTypeFilterValue = { [BSKY_USER_MATCH_TYPE.DESCRIPTION]: boolean; diff --git a/src/lib/domHelpers.ts b/src/lib/domHelpers.ts index f1fd28f..f5bb85b 100644 --- a/src/lib/domHelpers.ts +++ b/src/lib/domHelpers.ts @@ -1,3 +1,4 @@ +import type { CrawledUser } from "~types"; import { BSKY_DOMAIN } from "./constants"; export const getUserCells = ({ @@ -21,13 +22,13 @@ export const getUserCells = ({ return Array.from(userCells); }; -export const getAccountNameAndDisplayName = (userCell: Element) => { +export const extractUserData = (userCell: Element): CrawledUser => { const anchors = Array.from(userCell.querySelectorAll("a")); const [avatarEl, displayNameEl] = anchors; - const twAccountName = avatarEl?.getAttribute("href")?.replace("/", ""); - const twAccountNameRemoveUnderscore = twAccountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them. - const twAccountNameReplaceUnderscore = twAccountName.replaceAll("_", "-"); - const twDisplayName = displayNameEl?.textContent; + const accountName = avatarEl?.getAttribute("href")?.replace("/", ""); + const accountNameRemoveUnderscore = accountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them. + const accountNameReplaceUnderscore = accountName.replaceAll("_", "-"); + const displayName = displayNameEl?.textContent; const bskyHandle = userCell.textContent?.match( new RegExp(`([^/\\s]+\\.${BSKY_DOMAIN})`), @@ -35,13 +36,13 @@ export const getAccountNameAndDisplayName = (userCell: Element) => { userCell.textContent ?.match(/bsky\.app\/profile\/([^/\s]+)…?/)?.[1] ?.replace("…", "") ?? - null; + ""; return { - twAccountName, - twDisplayName, - twAccountNameRemoveUnderscore, - twAccountNameReplaceUnderscore, + accountName, + displayName, + accountNameRemoveUnderscore, + accountNameReplaceUnderscore, bskyHandle, }; }; diff --git a/src/lib/hooks/useRetrieveBskyUsers.ts b/src/lib/hooks/useRetrieveBskyUsers.ts index f2a4b7c..d1d8326 100644 --- a/src/lib/hooks/useRetrieveBskyUsers.ts +++ b/src/lib/hooks/useRetrieveBskyUsers.ts @@ -8,10 +8,10 @@ import { MESSAGE_NAME_TO_QUERY_PARAM_MAP, STORAGE_KEYS, } from "~lib/constants"; -import { getAccountNameAndDisplayName, getUserCells } from "~lib/domHelpers"; +import { extractUserData, getUserCells } from "~lib/domHelpers"; import { searchBskyUser } from "~lib/searchBskyUsers"; import { wait } from "~lib/utils"; -import type { MatchType } from "~types"; +import type { CrawledUser, MatchType } from "~types"; export type BskyUser = { did: string; @@ -32,7 +32,7 @@ const detectXUsers = (userCellQueryParam: string) => { filterInsertedElement: true, }); return userCells.map((userCell) => { - return getAccountNameAndDisplayName(userCell); + return extractUserData(userCell); }); }; @@ -65,7 +65,7 @@ export const useRetrieveBskyUsers = () => { }; const retrieveBskyUsers = React.useCallback( - async (usersData: ReturnType[]) => { + async (usersData: CrawledUser[]) => { for (const userData of usersData) { const searchResult = await searchBskyUser({ client: bskyClient.current, @@ -130,9 +130,7 @@ export const useRetrieveBskyUsers = () => { } const data = detectXUsers(queryParam).filter((u) => { - return !detectedXUsers.some( - (t) => t.twAccountName === u.twAccountName, - ); + return !detectedXUsers.some((t) => t.accountName === u.accountName); }); setDetectedXUsers((prev) => [...prev, ...data]); await retrieveBskyUsers(data); diff --git a/src/lib/searchBskyUsers.ts b/src/lib/searchBskyUsers.ts index 7b91cce..2b8d300 100644 --- a/src/lib/searchBskyUsers.ts +++ b/src/lib/searchBskyUsers.ts @@ -1,7 +1,7 @@ import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs"; import { isSimilarUser } from "~lib/bskyHelpers"; -import type { getAccountNameAndDisplayName } from "~lib/domHelpers"; import { isOneSymbol } from "~lib/utils"; +import type { CrawledUser } from "~types"; import type { BskyServiceWorkerClient } from "./bskyServiceWorkerClient"; import { BSKY_PROFILE_LABEL } from "./constants"; @@ -16,13 +16,13 @@ export const searchBskyUser = async ({ userData, }: { client: BskyServiceWorkerClient; - userData: ReturnType; + userData: CrawledUser; }) => { const searchTerms = [ ...(userData.bskyHandle ? [userData.bskyHandle] : []), - userData.twAccountNameRemoveUnderscore, - userData.twAccountNameReplaceUnderscore, - userData.twDisplayName, + userData.accountNameRemoveUnderscore, + userData.accountNameReplaceUnderscore, + userData.displayName, ]; const uniqueSearchTerms = new Set(searchTerms); @@ -47,11 +47,10 @@ export const searchBskyUser = async ({ // TODO: simplify { bskyHandleInDescription: userData.bskyHandle, - accountName: userData.twAccountName, - accountNameRemoveUnderscore: userData.twAccountNameRemoveUnderscore, - accountNameReplaceUnderscore: - userData.twAccountNameReplaceUnderscore, - displayName: userData.twDisplayName, + accountName: userData.accountName, + accountNameRemoveUnderscore: userData.accountNameRemoveUnderscore, + accountNameReplaceUnderscore: userData.accountNameReplaceUnderscore, + displayName: userData.displayName, }, searchResult, ); diff --git a/src/lib/services/x.ts b/src/lib/services/x.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/types.ts b/src/types.ts index d6ffefd..f802388 100644 --- a/src/types.ts +++ b/src/types.ts @@ -22,3 +22,11 @@ export type MatchTypeFilterValue = { [BSKY_USER_MATCH_TYPE.HANDLE]: boolean; [BSKY_USER_MATCH_TYPE.FOLLOWING]: boolean; }; + +export type CrawledUser = { + accountName: string; + displayName: string; + accountNameRemoveUnderscore: string; + accountNameReplaceUnderscore: string; + bskyHandle: string; +};