Skip to content

Commit

Permalink
feat(lib): make minimum interval to be one minute
Browse files Browse the repository at this point in the history
  • Loading branch information
piotr-suwala committed Sep 21, 2023
1 parent 08933f3 commit 4bed190
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
2 changes: 0 additions & 2 deletions samples/getting-started/.env.example

This file was deleted.

14 changes: 14 additions & 0 deletions samples/getting-started/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -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',
},
}
47 changes: 23 additions & 24 deletions samples/getting-started/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chat>()
Expand All @@ -29,33 +22,39 @@ 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
}, [messages])

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)
}

Expand Down

0 comments on commit 4bed190

Please sign in to comment.