Skip to content

Commit

Permalink
Merge pull request Programmer-Network#9 from petitoff/master
Browse files Browse the repository at this point in the history
feat: twitch badges
  • Loading branch information
agjs authored Oct 23, 2023
2 parents 0131fd3 + ac90407 commit 7c03108
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 36 deletions.
56 changes: 28 additions & 28 deletions api/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fastify, {
FastifyInstance,
FastifyReply,
FastifyRequest,
FastifyInstance,
FastifyReply,
FastifyRequest,
} from "fastify";
import cors from "@fastify/cors";
import fs from "fs";
Expand All @@ -14,7 +14,7 @@ export interface IMessage {
content: string;
emojis: string[];
author: string;
badge: string;
badges: string[];
authorColor: string;
}

Expand All @@ -24,7 +24,7 @@ const savedMessages: IMessage[] = [];
const userColors: { [key: string]: string } = {};

server.register(cors, {
origin: true,
origin: true,
});

server.post(
Expand All @@ -42,7 +42,7 @@ server.post(
content: "Chat reload",
author: "System",
emojis: [],
badge: "",
badges: [],
authorColor: "text-indigo-500",
});
}
Expand All @@ -61,36 +61,36 @@ server.post(
);

server.get("/api/messages", async (_: FastifyRequest, reply: FastifyReply) => {
reply.send(messages);
reply.send(messages);
});

server.post(
"/api/save-message",
async (request: FastifyRequest, reply: FastifyReply) => {
const message = request.body as IMessage;
savedMessages.push(message);
"/api/save-message",
async (request: FastifyRequest, reply: FastifyReply) => {
const message = request.body as IMessage;
savedMessages.push(message);

fs.writeFile("messages.json", JSON.stringify(savedMessages), (err) => {
if (err) {
console.log(err);
}
});
fs.writeFile("messages.json", JSON.stringify(savedMessages), (err) => {
if (err) {
console.log(err);
}
});

reply.send(savedMessages);
}
reply.send(savedMessages);
}
);

const start = async () => {
try {
await server.listen({
port: 3000,
host: "::",
});
server.log.info(`Server listening on http://localhost:3000/`);
} catch (err) {
server.log.error(err);
process.exit(1);
}
try {
await server.listen({
port: 3000,
host: "::",
});
server.log.info(`Server listening on http://localhost:3000/`);
} catch (err) {
server.log.error(err);
process.exit(1);
}
};

start();
12 changes: 11 additions & 1 deletion chat/src/Components/Message/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const Message: FC<{
onAction: (action: string, message: unknown) => void;
focusedMessage: IMessage | null;
}> = ({ message, focusedMessage, onMessageClick, onAction, filter }) => {
const { content, author } = message;
const { content, author, badges } = message;
const [hoveredId, setHoveredId] = useState<string>("");

const handleSaveMessage = (message: IMessage) => {
Expand Down Expand Up @@ -59,6 +59,16 @@ export const Message: FC<{
}
)}
>
<div className="flex gap-1 items-center">
{badges?.map((badge, index) => (
<img
key={index}
className="w-7 h-7"
src={badge}
alt={badge}
/>
))}
</div>
{author}
{isHovered && (
<div className="flex gap-1 items-center">
Expand Down
2 changes: 1 addition & 1 deletion chat/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export interface IMessage {
content: string;
emojis: string[];
author: string;
badge: string;
badges: string[];
authorColor: string;
}
2 changes: 1 addition & 1 deletion src/content/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class ChatLoader {
content,
emojis: this.domUtils.getMessageEmojis(chatNode),
author: this.domUtils.getMessageAuthor(chatNode),
badge: this.domUtils.getMessageBadges(chatNode),
badges: this.domUtils.getMessageBadges(chatNode),
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/content/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface IDOMUtils {
getMessageText(node: Element): string;
getMessageEmojis(node: Element): string[];
getMessageAuthor(node: Element): string;
getMessageBadges(node: Element): string;
getMessageBadges(node: Element): string[];
}

export interface IDateTimeUtils {
Expand Down Expand Up @@ -40,5 +40,5 @@ export interface IMessage {
content: string;
emojis: string[];
author: string;
badge: string;
badges: string[];
}
11 changes: 8 additions & 3 deletions src/content/utils/DOMUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,16 @@ export default class DOMUtils {

getMessageBadges(node: Element) {
if (!this.selectors.messageAuthorBadge) {
return "";
return [];
}

return (
node.querySelector(this.selectors.messageAuthorBadge) as HTMLImageElement
)?.src;
Array.from(
node.querySelectorAll(
this.selectors.messageAuthorBadge
) as NodeListOf<HTMLImageElement>
).map((img) => img.src)

)
}
}

0 comments on commit 7c03108

Please sign in to comment.