import { type FormEvent, useState } from "react" import { P, match } from "ts-pattern" import "./style.css" import { sendToContentScript } from "@plasmohq/messaging" import { useStorage } from "@plasmohq/storage/hook" import { MESSAGE_NAMES, MESSAGE_TYPE, STORAGE_KEYS, TARGET_URLS_REGEX } from "~lib/constants" import { debugLog } from "./lib/utils" function IndexPopup() { const [isLoading, setIsLoading] = useState(false) const [password, setPassword] = useStorage(STORAGE_KEYS.BSKY_PASSWORD, "") const [userId, setUserId] = useStorage(STORAGE_KEYS.BSKY_USER_ID, "") const [message, setMessage] = useState(null) const isDisabled = !password || !userId || isLoading const isShowErrorMessage = message?.type === MESSAGE_TYPE.ERROR const isShowSuccessMessage = message?.type === MESSAGE_TYPE.SUCCESS const setErrorMessage = (message: string) => { setMessage({ type: MESSAGE_TYPE.ERROR, message }) } const searchBskyUser = async (e: FormEvent) => { e.preventDefault() const [{ url: currentUrl }] = await chrome.tabs.query({ active: true, currentWindow: true }) if (!Object.values(TARGET_URLS_REGEX).some((r) => r.test(currentUrl))) { setErrorMessage( "Error: Invalid page. please open the Twitter following or blocking page." ) return } const messageName = match(currentUrl) .with( P.when((url) => TARGET_URLS_REGEX.FOLLOW.test(url)), () => MESSAGE_NAMES.SEARCH_BSKY_USER_ON_FOLLOW_PAGE ) .with( P.when((url) => TARGET_URLS_REGEX.BLOCK.test(url)), () => MESSAGE_NAMES.SEARCH_BSKY_USER_ON_BLOCK_PAGE ) .with( P.when((url) => TARGET_URLS_REGEX.LIST.test(url)), () => MESSAGE_NAMES.SEARCH_BSKY_USER_ON_LIST_MEMBERS_PAGE ) .run() setMessage(null) setIsLoading(true) try { const res: { hasError: boolean; message: string } = await sendToContentScript({ name: messageName, body: { password, userId } }) if (res.hasError) { setErrorMessage(res.message) } else { setMessage({ type: MESSAGE_TYPE.SUCCESS, message: "Completed. Try again if no results found.”" }) } } catch (e) { setErrorMessage( "Error: Something went wrong. Please reload the web page and try again." ) console.error(e) } finally { setIsLoading(false) } } return (

Sky Follower Bridge

{isShowErrorMessage && (
{message.message}
)} {isShowSuccessMessage && (
Success. Try again if no results found.
)}
) } export default IndexPopup