-
Notifications
You must be signed in to change notification settings - Fork 298
feat: add message reminders #2724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fae82a2
feat: add message reminders
MartinCupela 74c73d5
test: add message actions reminder test and fix broken tests
MartinCupela a364279
feat: allow for custom ReminderNotification component
MartinCupela de44853
feat: display reminder deadline after the reminder stops to refresh
MartinCupela 7ddc0ee
refactor: adapt code to LLC changes
MartinCupela c0d4a4b
chore(deps): upgrade stream-chat-js to version 9.5.0
MartinCupela 2482861
chore(deps): upgrade @stream-io/stream-chat-css to version 5.10.0
MartinCupela b9ba8f9
Merge branch 'refs/heads/master' into feat/snooze-message-reminders
MartinCupela 05ec453
refactor: change translation dueTimeElapsed to timeLeft
MartinCupela 2f0aa24
fix: prevent unnecessary memoization of isBehindRefreshBoundary in Re…
MartinCupela e1c0b9e
fix: calculate stopRefreshBoundaryMs in ReminderNotification from rem…
MartinCupela ab5d857
test: reflect removal of mutes from a user object in localMessage whe…
MartinCupela 8a5b821
Merge branch 'master' into feat/snooze-message-reminders
MartinCupela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import clsx from 'clsx'; | ||
| import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
| import { useDialog, useDialogIsOpen } from './hooks'; | ||
| import { useDialogAnchor } from './DialogAnchor'; | ||
| import type { ComponentProps, ComponentType } from 'react'; | ||
| import type { Placement } from '@popperjs/core'; | ||
|
|
||
| type ButtonWithSubmenu = ComponentProps<'button'> & { | ||
| children: React.ReactNode; | ||
| placement: Placement; | ||
| Submenu: ComponentType; | ||
| submenuContainerProps?: ComponentProps<'div'>; | ||
| }; | ||
| export const ButtonWithSubmenu = ({ | ||
| children, | ||
| className, | ||
| placement, | ||
| Submenu, | ||
| submenuContainerProps, | ||
| ...buttonProps | ||
| }: ButtonWithSubmenu) => { | ||
| const buttonRef = useRef<HTMLButtonElement | null>(null); | ||
| const [dialogContainer, setDialogContainer] = useState<HTMLDivElement | null>(null); | ||
| const keepSubmenuOpen = useRef(false); | ||
| const dialogCloseTimeout = useRef<NodeJS.Timeout | null>(null); | ||
| const dialogId = useMemo(() => `submenu-${Math.random().toString(36).slice(2)}`, []); | ||
| const dialog = useDialog({ id: dialogId }); | ||
| const dialogIsOpen = useDialogIsOpen(dialogId); | ||
| const { attributes, setPopperElement, styles } = useDialogAnchor<HTMLDivElement>({ | ||
| open: dialogIsOpen, | ||
| placement, | ||
| referenceElement: buttonRef.current, | ||
| }); | ||
|
|
||
| const closeDialogLazily = useCallback(() => { | ||
| if (dialogCloseTimeout.current) clearTimeout(dialogCloseTimeout.current); | ||
| dialogCloseTimeout.current = setTimeout(() => { | ||
| if (keepSubmenuOpen.current) return; | ||
| dialog.close(); | ||
| }, 100); | ||
| }, [dialog]); | ||
|
|
||
| const handleClose = useCallback( | ||
| (event: Event) => { | ||
| const parentButton = buttonRef.current; | ||
| if (!dialogIsOpen || !parentButton) return; | ||
| event.stopPropagation(); | ||
| closeDialogLazily(); | ||
| parentButton.focus(); | ||
| }, | ||
| [closeDialogLazily, dialogIsOpen, buttonRef], | ||
| ); | ||
|
|
||
| const handleFocusParentButton = () => { | ||
| if (dialogIsOpen) return; | ||
| dialog.open(); | ||
| keepSubmenuOpen.current = true; | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| const parentButton = buttonRef.current; | ||
| if (!dialogIsOpen || !parentButton) return; | ||
| const hideOnEscape = (event: KeyboardEvent) => { | ||
| if (event.key !== 'Escape') return; | ||
| handleClose(event); | ||
| keepSubmenuOpen.current = false; | ||
| }; | ||
|
|
||
| document.addEventListener('keyup', hideOnEscape, { capture: true }); | ||
|
|
||
| return () => { | ||
| document.removeEventListener('keyup', hideOnEscape, { capture: true }); | ||
| }; | ||
| }, [dialogIsOpen, handleClose]); | ||
|
|
||
| return ( | ||
| <> | ||
| <button | ||
| aria-selected='false' | ||
| className={clsx(className, 'str_chat__button-with-submenu', { | ||
| 'str_chat__button-with-submenu--submenu-open': dialogIsOpen, | ||
| })} | ||
| onBlur={() => { | ||
| keepSubmenuOpen.current = false; | ||
| closeDialogLazily(); | ||
| }} | ||
| onClick={(event) => { | ||
| event.stopPropagation(); | ||
| dialog.toggle(); | ||
| }} | ||
| onFocus={handleFocusParentButton} | ||
| onMouseEnter={handleFocusParentButton} | ||
| onMouseLeave={() => { | ||
| keepSubmenuOpen.current = false; | ||
| closeDialogLazily(); | ||
| }} | ||
| ref={buttonRef} | ||
| role='option' | ||
| {...buttonProps} | ||
| > | ||
| {children} | ||
| </button> | ||
| {dialogIsOpen && ( | ||
| <div | ||
| {...attributes.popper} | ||
| onBlur={(event) => { | ||
| const isBlurredDescendant = | ||
| event.relatedTarget instanceof Node && | ||
| dialogContainer?.contains(event.relatedTarget); | ||
| if (isBlurredDescendant) return; | ||
| keepSubmenuOpen.current = false; | ||
| closeDialogLazily(); | ||
| }} | ||
| onFocus={() => { | ||
| keepSubmenuOpen.current = true; | ||
| }} | ||
| onMouseEnter={() => { | ||
| keepSubmenuOpen.current = true; | ||
| }} | ||
| onMouseLeave={() => { | ||
| keepSubmenuOpen.current = false; | ||
| closeDialogLazily(); | ||
| }} | ||
| ref={(element) => { | ||
| setPopperElement(element); | ||
| setDialogContainer(element); | ||
| }} | ||
| style={styles.popper} | ||
| tabIndex={-1} | ||
| {...submenuContainerProps} | ||
| > | ||
| <Submenu /> | ||
| </div> | ||
| )} | ||
| </> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import React, { useMemo } from 'react'; | ||
| import { useChatContext, useTranslationContext } from '../../context'; | ||
| import { useStateStore } from '../../store'; | ||
| import type { Reminder, ReminderState } from 'stream-chat'; | ||
|
|
||
| export type ReminderNotificationProps = { | ||
| reminder?: Reminder; | ||
| }; | ||
|
|
||
| const reminderStateSelector = (state: ReminderState) => ({ | ||
| timeLeftMs: state.timeLeftMs, | ||
| }); | ||
|
|
||
| export const ReminderNotification = ({ reminder }: ReminderNotificationProps) => { | ||
| const { client } = useChatContext(); | ||
| const { t } = useTranslationContext(); | ||
| const { timeLeftMs } = useStateStore(reminder?.state, reminderStateSelector) ?? {}; | ||
|
|
||
| const isBehindRefreshBoundary = useMemo(() => { | ||
| const stopRefreshBoundaryMs = client.reminders.stopTimerRefreshBoundaryMs; | ||
| const stopRefreshTimeStamp = | ||
| reminder?.remindAt && stopRefreshBoundaryMs | ||
| ? reminder?.remindAt.getTime() + stopRefreshBoundaryMs | ||
| : undefined; | ||
| return !!stopRefreshTimeStamp && new Date().getTime() > stopRefreshTimeStamp; | ||
| }, [client, reminder]); | ||
|
|
||
| return ( | ||
| <p className='str-chat__message-reminder'> | ||
| <span>{t<string>('Saved for later')}</span> | ||
| {reminder?.remindAt && timeLeftMs !== null && ( | ||
| <> | ||
| <span> | </span> | ||
| <span> | ||
| {isBehindRefreshBoundary | ||
| ? t<string>('Due since {{ dueSince }}', { | ||
| dueSince: t<string>(`timestamp/ReminderNotification`, { | ||
| timestamp: reminder.remindAt, | ||
| }), | ||
| }) | ||
| : t<string>(`Due {{ dueTimeElapsed }}`, { | ||
| dueTimeElapsed: t<string>('duration/Message reminder', { | ||
| milliseconds: timeLeftMs, | ||
| }), | ||
| })} | ||
| </span> | ||
| </> | ||
| )} | ||
| </p> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.