chore: fix memo comment inbox

This commit is contained in:
Steven 2024-04-01 00:26:46 +08:00
parent ec35a42fb5
commit b79f626a74
3 changed files with 19 additions and 24 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/pkg/errors"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
@ -90,24 +89,11 @@ func (s *APIV2Service) DeleteInbox(ctx context.Context, request *apiv2pb.DeleteI
return &apiv2pb.DeleteInboxResponse{}, nil return &apiv2pb.DeleteInboxResponse{}, nil
} }
func (s *APIV2Service) convertInboxFromStore(ctx context.Context, inbox *store.Inbox) (*apiv2pb.Inbox, error) { func (s *APIV2Service) convertInboxFromStore(_ context.Context, inbox *store.Inbox) (*apiv2pb.Inbox, error) {
sender, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &inbox.SenderID,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get sender")
}
receiver, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &inbox.ReceiverID,
})
if err != nil {
return nil, errors.Wrap(err, "failed to get receiver")
}
return &apiv2pb.Inbox{ return &apiv2pb.Inbox{
Name: fmt.Sprintf("%s%d", InboxNamePrefix, inbox.ID), Name: fmt.Sprintf("%s%d", InboxNamePrefix, inbox.ID),
Sender: fmt.Sprintf("%s%d", UserNamePrefix, sender.ID), Sender: fmt.Sprintf("%s%d", UserNamePrefix, inbox.SenderID),
Receiver: fmt.Sprintf("%s%d", UserNamePrefix, receiver.ID), Receiver: fmt.Sprintf("%s%d", UserNamePrefix, inbox.ReceiverID),
Status: convertInboxStatusFromStore(inbox.Status), Status: convertInboxStatusFromStore(inbox.Status),
CreateTime: timestamppb.New(time.Unix(inbox.CreatedTs, 0)), CreateTime: timestamppb.New(time.Unix(inbox.CreatedTs, 0)),
Type: apiv2pb.Inbox_Type(inbox.Message.Type), Type: apiv2pb.Inbox_Type(inbox.Message.Type),

View file

@ -4,9 +4,10 @@ import { useEffect, useState } from "react";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import { activityServiceClient } from "@/grpcweb"; import { activityServiceClient } from "@/grpcweb";
import useNavigateTo from "@/hooks/useNavigateTo"; import useNavigateTo from "@/hooks/useNavigateTo";
import { MemoNamePrefix, useInboxStore, useMemoStore } from "@/store/v1"; import { MemoNamePrefix, useInboxStore, useMemoStore, useUserStore } from "@/store/v1";
import { Inbox, Inbox_Status } from "@/types/proto/api/v2/inbox_service"; import { Inbox, Inbox_Status } from "@/types/proto/api/v2/inbox_service";
import { Memo } from "@/types/proto/api/v2/memo_service"; import { Memo } from "@/types/proto/api/v2/memo_service";
import { User } from "@/types/proto/api/v2/user_service";
import { useTranslate } from "@/utils/i18n"; import { useTranslate } from "@/utils/i18n";
import Icon from "../Icon"; import Icon from "../Icon";
@ -19,7 +20,9 @@ const MemoCommentMessage = ({ inbox }: Props) => {
const navigateTo = useNavigateTo(); const navigateTo = useNavigateTo();
const inboxStore = useInboxStore(); const inboxStore = useInboxStore();
const memoStore = useMemoStore(); const memoStore = useMemoStore();
const userStore = useUserStore();
const [relatedMemo, setRelatedMemo] = useState<Memo | undefined>(undefined); const [relatedMemo, setRelatedMemo] = useState<Memo | undefined>(undefined);
const [sender, setSender] = useState<User | undefined>(undefined);
useEffect(() => { useEffect(() => {
if (!inbox.activityId) { if (!inbox.activityId) {
@ -33,11 +36,15 @@ const MemoCommentMessage = ({ inbox }: Props) => {
if (!activity) { if (!activity) {
return; return;
} }
if (activity.payload?.memoComment?.relatedMemoId) { if (activity.payload?.memoComment) {
const memo = await memoStore.getOrFetchMemoByName(`${MemoNamePrefix}${activity.payload?.memoComment?.relatedMemoId}`, { const memoCommentPayload = activity.payload.memoComment;
const relatedMemoId = memoCommentPayload.relatedMemoId;
const memo = await memoStore.getOrFetchMemoByName(`${MemoNamePrefix}${relatedMemoId}`, {
skipStore: true, skipStore: true,
}); });
setRelatedMemo(memo); setRelatedMemo(memo);
const sender = await userStore.getOrFetchUserByName(inbox.sender);
setSender(sender);
} }
})(); })();
}, [inbox.activityId]); }, [inbox.activityId]);
@ -47,7 +54,7 @@ const MemoCommentMessage = ({ inbox }: Props) => {
return; return;
} }
navigateTo(`/m/${relatedMemo.name}`); navigateTo(`/m/${relatedMemo.uid}`);
if (inbox.status === Inbox_Status.UNREAD) { if (inbox.status === Inbox_Status.UNREAD) {
handleArchiveMessage(true); handleArchiveMessage(true);
} }
@ -104,8 +111,9 @@ const MemoCommentMessage = ({ inbox }: Props) => {
onClick={handleNavigateToMemo} onClick={handleNavigateToMemo}
> >
{t("inbox.memo-comment", { {t("inbox.memo-comment", {
user: inbox.sender, user: sender?.nickname || sender?.username,
memo: `memos#${relatedMemo?.name}`, memo: `memos\/${relatedMemo?.uid}`,
interpolation: { escapeValue: false },
})} })}
</p> </p>
</div> </div>

View file

@ -1,4 +1,5 @@
import { Button, Input } from "@mui/joy"; import { Button, Input } from "@mui/joy";
import { ClientError } from "nice-grpc-web";
import { useState } from "react"; import { useState } from "react";
import { toast } from "react-hot-toast"; import { toast } from "react-hot-toast";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
@ -64,7 +65,7 @@ const SignUp = () => {
} }
} catch (error: any) { } catch (error: any) {
console.error(error); console.error(error);
toast.error(error.response.data.message || error.message || t("message.signup-failed")); toast.error((error as ClientError).details || t("message.signup-failed"));
} }
actionBtnLoadingState.setFinish(); actionBtnLoadingState.setFinish();
}; };