Skip to content

Commit

Permalink
Merge pull request #91 from TeamATM/fix/change-characterid-type
Browse files Browse the repository at this point in the history
Fix: change type of characterId from string to number
  • Loading branch information
MustSave authored Oct 12, 2023
2 parents c634e2f + 29528cd commit 645dd67
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/chat/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import useSocketStore from '@/store/socket';
import Dialog from '../common/dialog/Dialog';

interface CharacterState {
characterId: string,
characterId: number,
characterName: string
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/chats/[character].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Character = ({
/>
<Main characterId={characterId} characterName={characterName} imageUrl={imageUrl} />
</div>
<MessageInput characterId={characterId} characterName={characterName} />
<MessageInput characterId={Number(characterId)} characterName={characterName} />
</section>
{ settingModal && (
<Dialog closeModal={() => { setSettingModal(false); }} theme="white">
Expand Down
14 changes: 9 additions & 5 deletions src/store/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface SocketState {
chatStore?: ChatState;
processingmessageTime: Array<number>;
connect: (accessToken: string) => void;
sendMessage: (characterId: string, characterName:string, message:string) => void;
sendMessage: (characterId: number, characterName:string, message:string) => void;
setChatStore: (chatSate:ChatState) => void;
}

Expand All @@ -19,14 +19,14 @@ export interface Chat {

export interface MessageFromClient {
content: string;
characterId: string;
characterId: number;
createdAt: number;
characterName: string;
}

export interface MessageToClient extends Chat {
messageId: string;
characterId: string;
characterId: number;
createdAt: number;
characterName: string;
/**
Expand Down Expand Up @@ -71,7 +71,7 @@ const useSocketStore = create<SocketState>((set, get) => ({
get().socket!.on('connect', async () => {
get().socket!.on('subscribe', (msg) => {
const characterId = getCurrentCharacterId();
if (!characterId || msg.characterId === characterId) return;
if (characterId === null || msg.characterId !== characterId) return;
if (msg.fromUser) {
get().chatStore?.addChatContents({
speaker: 'me', content: msg.content, timestamp: msg.createdAt, loading: false,
Expand Down Expand Up @@ -123,7 +123,11 @@ const useSocketStore = create<SocketState>((set, get) => ({
function getCurrentCharacterId() {
const pathMatcher = window.location.pathname.match(/\/chats\/([\d]+)$/);
if (pathMatcher && pathMatcher.length === 2) {
return pathMatcher[1];
try {
return Number(pathMatcher[1]);
} catch (error) {
return null;
}
}
return null;
}
Expand Down

0 comments on commit 645dd67

Please sign in to comment.