Skip to content

Commit

Permalink
Use nanoid for message ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Dec 11, 2024
1 parent 195e5fd commit 1f33399
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
31 changes: 25 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"dotenv": "^16.4.5",
"mobx-react-lite": "^4.0.7",
"mobx-state-tree": "^7.0.0",
"nanoid": "^5.0.9",
"openai": "^4.72.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
5 changes: 3 additions & 2 deletions src/models/chat-transcript-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Instance, types } from "mobx-state-tree";
import { createMessageId, timeStamp } from "../utils/utils";
import { nanoid } from "nanoid";
import { timeStamp } from "../utils/utils";
import { MessageContent } from "../types";

const MessageModel = types.model("MessageModel", {
Expand All @@ -26,7 +27,7 @@ export const ChatTranscriptModel = types
speaker,
messageContent,
timestamp: timeStamp(),
id: createMessageId(),
id: nanoid(),
});
},
clearTranscript() {
Expand Down
12 changes: 0 additions & 12 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ export const timeStamp = (): string => {
return now.toLocaleString();
};

const idChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
export const createMessageId = (): string => {
const result: string[] = [];

while (result.length < 10) {
const randomIndex = Math.floor(Math.random() * idChars.length);
result.push(idChars.charAt(randomIndex));
}

return result.join("");
};

export const formatJsonMessage = (json: any) => {
return JSON.stringify(json, null, 2);
};
Expand Down

0 comments on commit 1f33399

Please sign in to comment.