@tellescope/chat 0.0.57 → 0.0.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tellescope/chat",
3
- "version": "0.0.57",
3
+ "version": "0.0.58",
4
4
  "description": "",
5
5
  "main": "./lib/cjs/index.js",
6
6
  "module": "./lib/esm/index.js",
@@ -33,13 +33,13 @@
33
33
  "@fontsource/roboto": "^4.5.1",
34
34
  "@mui/icons-material": "^5.0.1",
35
35
  "@mui/material": "^5.0.2",
36
- "@tellescope/constants": "^0.0.57",
37
- "@tellescope/react-components": "^0.0.57",
38
- "@tellescope/sdk": "^0.0.57",
39
- "@tellescope/types-client": "^0.0.57",
40
- "@tellescope/types-models": "^0.0.57",
41
- "@tellescope/types-utilities": "^0.0.57",
42
- "@tellescope/utilities": "^0.0.57",
36
+ "@tellescope/constants": "^0.0.58",
37
+ "@tellescope/react-components": "^0.0.58",
38
+ "@tellescope/sdk": "^0.0.58",
39
+ "@tellescope/types-client": "^0.0.58",
40
+ "@tellescope/types-models": "^0.0.58",
41
+ "@tellescope/types-utilities": "^0.0.58",
42
+ "@tellescope/utilities": "^0.0.58",
43
43
  "@typescript-eslint/eslint-plugin": "^4.33.0",
44
44
  "@typescript-eslint/parser": "^4.33.0",
45
45
  "eslint": "^7.32.0",
@@ -50,7 +50,7 @@
50
50
  "react": "^17.0.2",
51
51
  "react-dom": "^17.0.2"
52
52
  },
53
- "gitHead": "7a830676f46cdc5d10961aa85f79ad669e4acffb",
53
+ "gitHead": "62c6def8118570898eebf960a615baca605f17c7",
54
54
  "publishConfig": {
55
55
  "access": "public"
56
56
  }
package/src/chat.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState, CSSProperties } from "react"
1
+ import React, { useState, CSSProperties, useEffect } from "react"
2
2
 
3
3
  import {
4
4
  AsyncIconButton,
@@ -316,6 +316,9 @@ interface SendMessage_T {
316
316
  placeholderText?: string;
317
317
  Icon?: React.ElementType<any>;
318
318
  style?: CSSProperties;
319
+
320
+ // web only
321
+ sendOnEnterPress?: boolean,
319
322
  }
320
323
  export const SendMessage = ({
321
324
  roomId,
@@ -324,22 +327,51 @@ export const SendMessage = ({
324
327
  onNewMessage,
325
328
  placeholderText="Enter a message",
326
329
  style={},
330
+ sendOnEnterPress,
327
331
  }: SendMessage_T) => {
328
332
  const [message, setMessage] = useState('')
329
333
  const [sending, setSending] = useState(false)
334
+
335
+ const [disabled, setDisabled] = useState(false)
336
+ const [chatFocused, setChatFocused] = React.useState(false)
330
337
 
331
338
  const [, { createElement: createMessage }] = useChats(roomId, type)
332
339
 
340
+ useEffect(() => {
341
+ if (!chatFocused) return
342
+ if (!sendOnEnterPress) return
343
+ if (typeof window === 'undefined') return
344
+
345
+ const handleSend = (e: any) => {
346
+ if (e.key !== 'Enter') return
347
+ setDisabled(true)
348
+
349
+ createMessage({ message, roomId })
350
+ .then(m => {
351
+ setMessage('')
352
+ onNewMessage?.(m)
353
+ })
354
+ .catch(console.error)
355
+ .finally(() => setDisabled(false))
356
+ }
357
+
358
+ window.addEventListener('keypress', handleSend)
359
+ return () => { window.removeEventListener('keypress', handleSend) }
360
+ }, [sendOnEnterPress, chatFocused, message, roomId])
361
+
333
362
  return (
334
363
  <Flex row flex={1} alignContent="center" style={style}>
335
364
  <Flex column flex={1}>
336
365
  <TextField variant="outlined" value={message} onChange={setMessage} disabled={sending}
337
366
  aria-label="Enter a message"
338
367
  placeholder={placeholderText}
368
+ onFocus={() => setChatFocused(true)}
369
+ onBlur={() => setChatFocused(false)}
339
370
  />
340
371
  </Flex>
341
372
  <Flex column alignSelf="center">
342
- <AsyncIconButton label="send" Icon={Icon} disabled={message === ''}
373
+ <AsyncIconButton label="send" Icon={Icon}
374
+ disabled={message === '' || disabled}
343
375
  action={() => createMessage({ message, roomId })}
344
376
  onSuccess={m => {
345
377
  setMessage('')