From 7722c416806ac0a09353d8c4520db1f32712df90 Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 26 Sep 2023 23:46:58 +0800 Subject: [PATCH] chore: add edit button to memo detail page --- web/src/components/MemoResourceListView.tsx | 2 +- web/src/pages/MemoDetail.tsx | 15 ++++++++++++ web/src/router/index.tsx | 26 +++++++++------------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/web/src/components/MemoResourceListView.tsx b/web/src/components/MemoResourceListView.tsx index e6e861344..42f2bb130 100644 --- a/web/src/components/MemoResourceListView.tsx +++ b/web/src/components/MemoResourceListView.tsx @@ -43,7 +43,7 @@ const MemoResourceListView: React.FC = (props: Props) => { <> {imageResourceList.length > 0 && (imageResourceList.length === 1 ? ( -
+
{ const t = useTranslate(); const memoStore = useMemoStore(); const userV1Store = useUserV1Store(); + const currentUser = useCurrentUser(); const [user, setUser] = useState(); const memoId = Number(params.memoId); const memo = memoStore.state.memos.find((memo) => memo.id === memoId); + const allowEdit = memo?.creatorUsername === currentUser?.username; useEffect(() => { if (memoId && !isNaN(memoId)) { @@ -65,6 +69,12 @@ const MemoDetail = () => { }); }; + const handleEditMemoClick = () => { + showMemoEditorDialog({ + memoId: memo.id, + }); + }; + const handleCopyLinkBtnClick = () => { copy(`${window.location.origin}/m/${memo.id}`); toast.success(t("message.succeed-copy-link")); @@ -113,6 +123,11 @@ const MemoDetail = () => {
+ {allowEdit && ( + + + + )} diff --git a/web/src/router/index.tsx b/web/src/router/index.tsx index 10b2abf5e..e5114cb38 100644 --- a/web/src/router/index.tsx +++ b/web/src/router/index.tsx @@ -33,7 +33,7 @@ const initialGlobalStateLoader = (() => { }; })(); -const userStateLoader = async () => { +const initialUserStateLoader = async (redirectWhenNotFound = true) => { let user = undefined; try { user = await initialUserState(); @@ -41,7 +41,7 @@ const userStateLoader = async () => { // do nothing. } - if (!user) { + if (!user && redirectWhenNotFound) { return redirect("/explore"); } return null; @@ -71,53 +71,49 @@ const router = createBrowserRouter([ { path: "", element: , - loader: userStateLoader, + loader: () => initialUserStateLoader(), }, { path: "explore", element: , - loader: async () => { - try { - await initialUserState(); - } catch (error) { - // do nothing. - } - return null; - }, + loader: () => initialUserStateLoader(false), }, { path: "review", element: , - loader: userStateLoader, + loader: () => initialUserStateLoader(), }, { path: "resources", element: , - loader: userStateLoader, + loader: () => initialUserStateLoader(), }, { path: "archived", element: , - loader: userStateLoader, + loader: () => initialUserStateLoader(), }, { path: "setting", element: , - loader: userStateLoader, + loader: () => initialUserStateLoader(), }, ], }, { path: "/m/:memoId", element: , + loader: () => initialUserStateLoader(false), }, { path: "/m/:memoId/embed", element: , + loader: () => initialUserStateLoader(false), }, { path: "/u/:username", element: , + loader: () => initialUserStateLoader(false), }, { path: "*",