From 1d5771141568451ed180ef5b75fe3eb5575a5c2e Mon Sep 17 00:00:00 2001 From: Piotr Suwala Date: Wed, 20 Sep 2023 13:23:45 +0200 Subject: [PATCH] feat(lib): revert files --- samples/getting-started/.env.example | 2 -- samples/getting-started/.eslintrc.cjs | 14 ++++++++ samples/getting-started/src/App.tsx | 47 +++++++++++++-------------- 3 files changed, 37 insertions(+), 26 deletions(-) delete mode 100644 samples/getting-started/.env.example create mode 100644 samples/getting-started/.eslintrc.cjs diff --git a/samples/getting-started/.env.example b/samples/getting-started/.env.example deleted file mode 100644 index 9d62ab88..00000000 --- a/samples/getting-started/.env.example +++ /dev/null @@ -1,2 +0,0 @@ -VITE_PUBNUB_SUB_KEY= -VITE_PUBNUB_PUB_KEY= \ No newline at end of file diff --git a/samples/getting-started/.eslintrc.cjs b/samples/getting-started/.eslintrc.cjs new file mode 100644 index 00000000..9ec2dd10 --- /dev/null +++ b/samples/getting-started/.eslintrc.cjs @@ -0,0 +1,14 @@ +module.exports = { + env: { browser: true, es2020: true }, + extends: [ + 'eslint:recommended', + 'plugin:@typescript-eslint/recommended', + 'plugin:react-hooks/recommended', + ], + parser: '@typescript-eslint/parser', + parserOptions: { ecmaVersion: 'latest', sourceType: 'module' }, + plugins: ['react-refresh'], + rules: { + 'react-refresh/only-export-components': 'warn', + }, +} diff --git a/samples/getting-started/src/App.tsx b/samples/getting-started/src/App.tsx index ccc373ab..9db994c3 100644 --- a/samples/getting-started/src/App.tsx +++ b/samples/getting-started/src/App.tsx @@ -2,17 +2,10 @@ import React, { useCallback, useEffect, useRef, useState } from "react" import { Channel, Chat, Message, MixedTextTypedElement, TimetokenUtils, User } from "@pubnub/chat" import "./App.css" -const userData = [ - { - id: "support-agent", - data: { name: "John (Support Agent)", custom: { initials: "SA", avatar: "#9fa7df" } }, - }, - { - id: "supported-user", - data: { name: "Mary Watson", custom: { initials: "MW", avatar: "#ffab91" } }, - }, -] -const randomizedUsers = Math.random() < 0.5 ? userData : userData.reverse() +const userData = { + "support-agent": { name: "John (Support Agent)", custom: { initials: "SA", avatar: "#9fa7df" } }, + "supported-user": { name: "Mary Watson", custom: { initials: "MW", avatar: "#ffab91" } }, +} export default function App() { const [chat, setChat] = useState() @@ -29,6 +22,14 @@ export default function App() { setText("") } + async function handleMessage(message: Message) { + if (chat && !users.find((user) => user.id === message.userId)) { + const user = await chat.getUser(message.userId) + if (user) setUsers((users) => [...users, user]) + } + setMessages((messages) => [...messages, message]) + } + useEffect(() => { if (!messageListRef.current) return messageListRef.current.scrollTop = messageListRef.current.scrollHeight @@ -36,26 +37,24 @@ export default function App() { useEffect(() => { if (!channel) return - return channel.connect((message) => setMessages((messages) => [...messages, message])) + return channel.connect(handleMessage) }, [channel]) useEffect(() => { async function initalizeChat() { + const userId = Math.random() < 0.5 ? "support-agent" : "supported-user" + const channelId = "support-channel" const chat = await Chat.init({ - subscribeKey: import.meta.env.VITE_PUBNUB_SUB_KEY, - publishKey: import.meta.env.VITE_PUBNUB_PUB_KEY, - userId: randomizedUsers[0].id, - }) - const currentUser = await chat.currentUser.update(randomizedUsers[0].data) - const interlocutor = - (await chat.getUser(randomizedUsers[1].id)) || - (await chat.createUser(randomizedUsers[1].id, randomizedUsers[1].data)) - const { channel } = await chat.createDirectConversation({ - user: interlocutor, - channelData: { name: "Support Channel" }, + subscribeKey: "sub-c-2e5fa5c4-fd65-4ef8-9246-286dde521c20", + publishKey: "pub-c-58c29876-cff9-4f15-bb16-6bd785739fe4", + userId, }) + const user = await chat.currentUser.update(userData[userId]) + const channel = + (await chat.getChannel(channelId)) || + (await chat.createChannel(channelId, { name: "Support Channel" })) setChat(chat) - setUsers([currentUser, interlocutor]) + setUsers([user]) setChannel(channel) }