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 { 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;

View File

@ -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,
};
};

View File

@ -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<typeof getAccountNameAndDisplayName>[]) => {
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);

View File

@ -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<typeof getAccountNameAndDisplayName>;
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,
);

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.FOLLOWING]: boolean;
};
export type CrawledUser = {
accountName: string;
displayName: string;
accountNameRemoveUnderscore: string;
accountNameReplaceUnderscore: string;
bskyHandle: string;
};