refactor: rename

This commit is contained in:
kawamataryo 2024-11-19 21:12:58 +09:00
parent e0620f349d
commit bc1dcc32d3
6 changed files with 34 additions and 28 deletions

View File

@ -1,6 +1,6 @@
import React from "react"; import React from "react";
import { BSKY_USER_MATCH_TYPE, MATCH_TYPE_LABEL_AND_COLOR } from "../constants"; 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 = { export type MatchTypeFilterValue = {
[BSKY_USER_MATCH_TYPE.DESCRIPTION]: boolean; [BSKY_USER_MATCH_TYPE.DESCRIPTION]: boolean;

View File

@ -1,3 +1,4 @@
import type { CrawledUser } from "~types";
import { BSKY_DOMAIN } from "./constants"; import { BSKY_DOMAIN } from "./constants";
export const getUserCells = ({ export const getUserCells = ({
@ -21,13 +22,13 @@ export const getUserCells = ({
return Array.from(userCells); return Array.from(userCells);
}; };
export const getAccountNameAndDisplayName = (userCell: Element) => { export const extractUserData = (userCell: Element): CrawledUser => {
const anchors = Array.from(userCell.querySelectorAll("a")); const anchors = Array.from(userCell.querySelectorAll("a"));
const [avatarEl, displayNameEl] = anchors; const [avatarEl, displayNameEl] = anchors;
const twAccountName = avatarEl?.getAttribute("href")?.replace("/", ""); const accountName = avatarEl?.getAttribute("href")?.replace("/", "");
const twAccountNameRemoveUnderscore = twAccountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them. const accountNameRemoveUnderscore = accountName.replaceAll("_", ""); // bsky does not allow underscores in handle, so remove them.
const twAccountNameReplaceUnderscore = twAccountName.replaceAll("_", "-"); const accountNameReplaceUnderscore = accountName.replaceAll("_", "-");
const twDisplayName = displayNameEl?.textContent; const displayName = displayNameEl?.textContent;
const bskyHandle = const bskyHandle =
userCell.textContent?.match( userCell.textContent?.match(
new RegExp(`([^/\\s]+\\.${BSKY_DOMAIN})`), new RegExp(`([^/\\s]+\\.${BSKY_DOMAIN})`),
@ -35,13 +36,13 @@ export const getAccountNameAndDisplayName = (userCell: Element) => {
userCell.textContent userCell.textContent
?.match(/bsky\.app\/profile\/([^/\s]+)…?/)?.[1] ?.match(/bsky\.app\/profile\/([^/\s]+)…?/)?.[1]
?.replace("…", "") ?? ?.replace("…", "") ??
null; "";
return { return {
twAccountName, accountName,
twDisplayName, displayName,
twAccountNameRemoveUnderscore, accountNameRemoveUnderscore,
twAccountNameReplaceUnderscore, accountNameReplaceUnderscore,
bskyHandle, bskyHandle,
}; };
}; };

View File

@ -8,10 +8,10 @@ import {
MESSAGE_NAME_TO_QUERY_PARAM_MAP, MESSAGE_NAME_TO_QUERY_PARAM_MAP,
STORAGE_KEYS, STORAGE_KEYS,
} from "~lib/constants"; } from "~lib/constants";
import { getAccountNameAndDisplayName, getUserCells } from "~lib/domHelpers"; import { extractUserData, getUserCells } from "~lib/domHelpers";
import { searchBskyUser } from "~lib/searchBskyUsers"; import { searchBskyUser } from "~lib/searchBskyUsers";
import { wait } from "~lib/utils"; import { wait } from "~lib/utils";
import type { MatchType } from "~types"; import type { CrawledUser, MatchType } from "~types";
export type BskyUser = { export type BskyUser = {
did: string; did: string;
@ -32,7 +32,7 @@ const detectXUsers = (userCellQueryParam: string) => {
filterInsertedElement: true, filterInsertedElement: true,
}); });
return userCells.map((userCell) => { return userCells.map((userCell) => {
return getAccountNameAndDisplayName(userCell); return extractUserData(userCell);
}); });
}; };
@ -65,7 +65,7 @@ export const useRetrieveBskyUsers = () => {
}; };
const retrieveBskyUsers = React.useCallback( const retrieveBskyUsers = React.useCallback(
async (usersData: ReturnType<typeof getAccountNameAndDisplayName>[]) => { async (usersData: CrawledUser[]) => {
for (const userData of usersData) { for (const userData of usersData) {
const searchResult = await searchBskyUser({ const searchResult = await searchBskyUser({
client: bskyClient.current, client: bskyClient.current,
@ -130,9 +130,7 @@ export const useRetrieveBskyUsers = () => {
} }
const data = detectXUsers(queryParam).filter((u) => { const data = detectXUsers(queryParam).filter((u) => {
return !detectedXUsers.some( return !detectedXUsers.some((t) => t.accountName === u.accountName);
(t) => t.twAccountName === u.twAccountName,
);
}); });
setDetectedXUsers((prev) => [...prev, ...data]); setDetectedXUsers((prev) => [...prev, ...data]);
await retrieveBskyUsers(data); await retrieveBskyUsers(data);

View File

@ -1,7 +1,7 @@
import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs"; import type { ProfileView } from "@atproto/api/dist/client/types/app/bsky/actor/defs";
import { isSimilarUser } from "~lib/bskyHelpers"; import { isSimilarUser } from "~lib/bskyHelpers";
import type { getAccountNameAndDisplayName } from "~lib/domHelpers";
import { isOneSymbol } from "~lib/utils"; import { isOneSymbol } from "~lib/utils";
import type { CrawledUser } from "~types";
import type { BskyServiceWorkerClient } from "./bskyServiceWorkerClient"; import type { BskyServiceWorkerClient } from "./bskyServiceWorkerClient";
import { BSKY_PROFILE_LABEL } from "./constants"; import { BSKY_PROFILE_LABEL } from "./constants";
@ -16,13 +16,13 @@ export const searchBskyUser = async ({
userData, userData,
}: { }: {
client: BskyServiceWorkerClient; client: BskyServiceWorkerClient;
userData: ReturnType<typeof getAccountNameAndDisplayName>; userData: CrawledUser;
}) => { }) => {
const searchTerms = [ const searchTerms = [
...(userData.bskyHandle ? [userData.bskyHandle] : []), ...(userData.bskyHandle ? [userData.bskyHandle] : []),
userData.twAccountNameRemoveUnderscore, userData.accountNameRemoveUnderscore,
userData.twAccountNameReplaceUnderscore, userData.accountNameReplaceUnderscore,
userData.twDisplayName, userData.displayName,
]; ];
const uniqueSearchTerms = new Set(searchTerms); const uniqueSearchTerms = new Set(searchTerms);
@ -47,11 +47,10 @@ export const searchBskyUser = async ({
// TODO: simplify // TODO: simplify
{ {
bskyHandleInDescription: userData.bskyHandle, bskyHandleInDescription: userData.bskyHandle,
accountName: userData.twAccountName, accountName: userData.accountName,
accountNameRemoveUnderscore: userData.twAccountNameRemoveUnderscore, accountNameRemoveUnderscore: userData.accountNameRemoveUnderscore,
accountNameReplaceUnderscore: accountNameReplaceUnderscore: userData.accountNameReplaceUnderscore,
userData.twAccountNameReplaceUnderscore, displayName: userData.displayName,
displayName: userData.twDisplayName,
}, },
searchResult, searchResult,
); );

0
src/lib/services/x.ts Normal file
View File

View File

@ -22,3 +22,11 @@ export type MatchTypeFilterValue = {
[BSKY_USER_MATCH_TYPE.HANDLE]: boolean; [BSKY_USER_MATCH_TYPE.HANDLE]: boolean;
[BSKY_USER_MATCH_TYPE.FOLLOWING]: boolean; [BSKY_USER_MATCH_TYPE.FOLLOWING]: boolean;
}; };
export type CrawledUser = {
accountName: string;
displayName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
bskyHandle: string;
};