From d354030056d3ef09aee338f21ed147783240c1be Mon Sep 17 00:00:00 2001 From: kawamataryo Date: Thu, 28 Nov 2024 21:42:43 +0900 Subject: [PATCH] refactor: import list --- src/background/messages/getMyProfile.ts | 21 +++++++ src/contents/App.tsx | 6 +- src/lib/bskyClient.ts | 8 +++ src/lib/bskyServiceWorkerClient.ts | 14 +++++ src/lib/components/AsyncButton.tsx | 5 +- src/lib/components/Sidebar.stories.tsx | 20 ++++-- src/lib/components/Sidebar.tsx | 41 ++++++------ src/lib/constants.ts | 1 + src/lib/hooks/useBskyUserManager.ts | 42 ++++++++----- src/lib/hooks/useRetrieveBskyUsers.ts | 13 +++- src/lib/services/xService.ts | 2 +- src/options.tsx | 83 ++++++++++++++++++++++--- 12 files changed, 200 insertions(+), 56 deletions(-) create mode 100644 src/background/messages/getMyProfile.ts diff --git a/src/background/messages/getMyProfile.ts b/src/background/messages/getMyProfile.ts new file mode 100644 index 0000000..7ff7216 --- /dev/null +++ b/src/background/messages/getMyProfile.ts @@ -0,0 +1,21 @@ +import type { PlasmoMessaging } from "@plasmohq/messaging"; +import { BskyClient } from "~lib/bskyClient"; + +const handler: PlasmoMessaging.MessageHandler = async (req, res) => { + const { session } = req.body; + const client = await BskyClient.createAgentFromSession(session); + + try { + res.send({ + result: await client.getMyProfile(), + }); + } catch (e) { + res.send({ + error: { + message: e.message, + }, + }); + } +}; + +export default handler; diff --git a/src/contents/App.tsx b/src/contents/App.tsx index ae7a62b..a0230b1 100644 --- a/src/contents/App.tsx +++ b/src/contents/App.tsx @@ -71,12 +71,8 @@ const App = () => { sendToBackground({ name: "openOptionPage" }); }; - const stopAndShowDetectedUsers = async () => { + const stopAndShowDetectedUsers = () => { stopRetrieveLoop(); - await chrome.storage.local.set({ - users: JSON.stringify(users), - listName: listName, - }); openOptionPage(); }; diff --git a/src/lib/bskyClient.ts b/src/lib/bskyClient.ts index daa1a77..12370d7 100644 --- a/src/lib/bskyClient.ts +++ b/src/lib/bskyClient.ts @@ -169,4 +169,12 @@ export class BskyClient { await this.addUserToList({ userDid, listUri }); } }; + + public getMyProfile = async () => { + return { + pdsUrl: this.agent.pdsUrl, + did: this.agent.session.did, + handle: this.agent.session.handle, + }; + }; } diff --git a/src/lib/bskyServiceWorkerClient.ts b/src/lib/bskyServiceWorkerClient.ts index 1cb7ede..6c7c74e 100644 --- a/src/lib/bskyServiceWorkerClient.ts +++ b/src/lib/bskyServiceWorkerClient.ts @@ -164,5 +164,19 @@ export class BskyServiceWorkerClient { for (const userDid of userDids) { await this.addUserToList({ userDid, listUri }); } + const listId = listUri.split("/").pop(); + return listId; + }; + + public getMyProfile = async () => { + const { result, error } = await sendToBackground({ + name: "getMyProfile", + body: { + session: this.session, + }, + }); + if (error) throw new Error(error.message); + + return result; }; } diff --git a/src/lib/components/AsyncButton.tsx b/src/lib/components/AsyncButton.tsx index 306081a..351f692 100644 --- a/src/lib/components/AsyncButton.tsx +++ b/src/lib/components/AsyncButton.tsx @@ -3,9 +3,10 @@ import React from "react"; type Props = { onClick: () => Promise; label: string; + className?: string; }; -const AsyncButton = ({ onClick, label }: Props) => { +const AsyncButton = ({ onClick, label, className }: Props) => { const [loading, setLoading] = React.useState(false); const handleClick = async () => { @@ -17,7 +18,7 @@ const AsyncButton = ({ onClick, label }: Props) => { return (