Skip to content

Commit

Permalink
feat(legacy): update state when the character name is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
uonr committed Aug 30, 2024
1 parent d1b092c commit 725df27
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 deletions apps/legacy/src/components/chat/ChannelMemberButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import doorOpen from '../../assets/icons/door-open.svg';
import editIcon from '../../assets/icons/edit.svg';
import ninja from '../../assets/icons/ninja.svg';
import { useChannelId } from '../../hooks/useChannelId';
import { useDispatch, useSelector } from '../../store';
import store, { useDispatch, useSelector } from '../../store';
import { alignRight, mL, mR, mT } from '../../styles/atoms';
import { recordNext } from '../../utils/browser';
import { throwErr } from '../../utils/errors';
Expand Down Expand Up @@ -94,6 +94,7 @@ function ChannelMemberButton({ className }: Props) {
);
const member = useSelector((state) => state.profile?.channels.get(channelId)?.member);
const nickname = user?.nickname;
const dispatch = useDispatch();
const name = chatName(member?.characterName, nickname);
const buttonRef = useRef<HTMLButtonElement | null>(null);
const [menu, setMenu] = useState(false);
Expand Down Expand Up @@ -123,8 +124,14 @@ function ChannelMemberButton({ className }: Props) {
setLeaveConfirmDialog(false);
setDialog(false);
};
const onSubmitEdit = ({ characterName }: FormData) => {
edit(characterName).then(dismissDialog);
const onSubmitEdit = async ({ characterName }: FormData) => {
await edit(characterName);
const oldCharacterName = member?.characterName;
const inputName = store.getState().chatStates.get(channelId)?.compose.inputName;
if (inputName && oldCharacterName && inputName === oldCharacterName) {
dispatch({ type: 'SET_INPUT_NAME', name: '', pane: channelId });
}
dismissDialog();
};

const characterNameField = (
Expand Down
12 changes: 9 additions & 3 deletions apps/legacy/src/reducers/chatState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,12 @@ const handleSetWhisperTo = (state: ChatState, { whisperTo }: SetWhisperTo): Chat
return { ...state, compose: { ...state.compose, whisperTo } };
};

const handleChatInitialized = (myId: Id, channelId: Id, itemSet: ChatItemSet): Compose => {
const handleChatInitialized = (
myId: Id,
channelId: Id,
itemSet: ChatItemSet,
myMember: Member | undefined,
): Compose => {
const item = itemSet.previews.get(myId);
const compose: Compose = {
messageId: newId(),
Expand Down Expand Up @@ -312,7 +317,7 @@ const handleChatInitialized = (myId: Id, channelId: Id, itemSet: ChatItemSet): C
}
compose.source = preview.text;
compose.inGame = preview.inGame;
if (compose.inGame && preview.name) {
if (compose.inGame && preview.name && myMember && preview.name !== myMember.channel.characterName) {
compose.inputName = preview.name;
}
return compose;
Expand Down Expand Up @@ -401,7 +406,8 @@ const handleChannelEvent = (chat: ChatState, event: Events, myId: Id | undefined
if (!initialized) {
initialized = true;
if (myId) {
compose = handleChatInitialized(myId, channel.id, itemSet);
const myMember = members.find((member) => member.channel.userId === myId);
compose = handleChatInitialized(myId, channel.id, itemSet, myMember);
}
}
break;
Expand Down

0 comments on commit 725df27

Please sign in to comment.