Improvements to the MyStories row

This commit is contained in:
Josh Perez 2022-08-19 14:36:47 -04:00 committed by GitHub
parent 6dd6a64d6c
commit f7f65de322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 244 additions and 149 deletions

View file

@ -129,3 +129,7 @@
} }
} }
} }
.StoryListItem__button:hover .MyStories__avatar__add-story {
border-color: $color-gray-65;
}

View file

@ -83,15 +83,6 @@
position: relative; position: relative;
width: 46px; width: 46px;
&--add {
&::after {
content: '';
@include color-svg('../images/icons/v2/plus-20.svg', $color-gray-15);
height: 18px;
width: 18px;
}
}
&--image { &--image {
@include button-reset; @include button-reset;

View file

@ -2,9 +2,9 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import type { KeyboardEvent, ReactNode } from 'react'; import type { KeyboardEvent, ReactNode } from 'react';
import type { Options } from '@popperjs/core'; import type { Options, VirtualElement } from '@popperjs/core';
import FocusTrap from 'focus-trap-react'; import FocusTrap from 'focus-trap-react';
import React, { useEffect, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { usePopper } from 'react-popper'; import { usePopper } from 'react-popper';
import { noop } from 'lodash'; import { noop } from 'lodash';
@ -23,6 +23,7 @@ export type ContextMenuOptionType<T> = {
}; };
export type PropsType<T> = { export type PropsType<T> = {
readonly ariaLabel?: string;
readonly children?: ReactNode; readonly children?: ReactNode;
readonly i18n: LocalizerType; readonly i18n: LocalizerType;
readonly menuOptions: ReadonlyArray<ContextMenuOptionType<T>>; readonly menuOptions: ReadonlyArray<ContextMenuOptionType<T>>;
@ -37,7 +38,27 @@ export type PropsType<T> = {
let closeCurrentOpenContextMenu: undefined | (() => unknown); let closeCurrentOpenContextMenu: undefined | (() => unknown);
// https://popper.js.org/docs/v2/virtual-elements/
// Generating a virtual element here so that we can make the menu pop up
// right under the mouse cursor.
function generateVirtualElement(x: number, y: number): VirtualElement {
return {
getBoundingClientRect: () => ({
bottom: y,
height: 0,
left: x,
right: x,
toJSON: () => ({ x, y }),
top: y,
width: 0,
x,
y,
}),
};
}
export function ContextMenu<T>({ export function ContextMenu<T>({
ariaLabel,
children, children,
i18n, i18n,
menuOptions, menuOptions,
@ -56,14 +77,21 @@ export function ContextMenu<T>({
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>( const [popperElement, setPopperElement] = useState<HTMLDivElement | null>(
null null
); );
const virtualElement = useRef<VirtualElement>(generateVirtualElement(0, 0));
const [referenceElement, setReferenceElement] = const [referenceElement, setReferenceElement] =
useState<HTMLButtonElement | null>(null); useState<HTMLButtonElement | null>(null);
const { styles, attributes } = usePopper(referenceElement, popperElement, { const { styles, attributes } = usePopper(
virtualElement.current,
popperElement,
{
placement: 'top-start', placement: 'top-start',
strategy: 'fixed', strategy: 'fixed',
...popperOptions, ...popperOptions,
}); }
);
useEffect(() => { useEffect(() => {
if (onMenuShowingChanged) { if (onMenuShowingChanged) {
@ -129,9 +157,10 @@ export function ContextMenu<T>({
} }
}; };
const handleClick = (ev: KeyboardEvent | React.MouseEvent) => { const handleClick = (ev: React.MouseEvent) => {
closeCurrentOpenContextMenu?.(); closeCurrentOpenContextMenu?.();
closeCurrentOpenContextMenu = () => setIsMenuShowing(false); closeCurrentOpenContextMenu = () => setIsMenuShowing(false);
virtualElement.current = generateVirtualElement(ev.clientX, ev.clientY);
setIsMenuShowing(true); setIsMenuShowing(true);
ev.stopPropagation(); ev.stopPropagation();
ev.preventDefault(); ev.preventDefault();
@ -147,7 +176,7 @@ export function ContextMenu<T>({
)} )}
> >
<button <button
aria-label={i18n('ContextMenu--button')} aria-label={ariaLabel || i18n('ContextMenu--button')}
className={classNames( className={classNames(
getClassName('__button'), getClassName('__button'),
isMenuShowing ? getClassName('__button--active') : undefined isMenuShowing ? getClassName('__button--active') : undefined

View file

@ -36,6 +36,7 @@ export default {
onAddStory: { action: true }, onAddStory: { action: true },
onClick: { action: true }, onClick: { action: true },
queueStoryDownload: { action: true }, queueStoryDownload: { action: true },
showToast: { action: true },
}, },
} as Meta; } as Meta;
@ -50,7 +51,7 @@ const interactionTest: PlayFunction<ReactFramework, PropsType> = async ({
await userEvent.click(btnAddStory); await userEvent.click(btnAddStory);
await expect(args.onAddStory).toHaveBeenCalled(); await expect(args.onAddStory).toHaveBeenCalled();
const [btnStory] = canvas.getAllByLabelText('Story'); const [btnStory] = canvas.getAllByText('My Stories');
await userEvent.click(btnStory); await userEvent.click(btnStory);
await expect(args.onClick).toHaveBeenCalled(); await expect(args.onClick).toHaveBeenCalled();
}; };

View file

@ -5,11 +5,14 @@ import React from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import type { ConversationType } from '../state/ducks/conversations'; import type { ConversationType } from '../state/ducks/conversations';
import type { LocalizerType } from '../types/Util'; import type { LocalizerType } from '../types/Util';
import type { ShowToastActionCreatorType } from '../state/ducks/toast';
import type { StoryViewType } from '../types/Stories'; import type { StoryViewType } from '../types/Stories';
import { Avatar, AvatarSize } from './Avatar'; import { Avatar, AvatarSize } from './Avatar';
import { StoryImage } from './StoryImage'; import { StoryImage } from './StoryImage';
import { getAvatarColor } from '../types/Colors'; import { getAvatarColor } from '../types/Colors';
import { StoriesAddStoryButton } from './StoriesAddStoryButton';
export type PropsType = { export type PropsType = {
hasMultiple: boolean; hasMultiple: boolean;
i18n: LocalizerType; i18n: LocalizerType;
@ -18,6 +21,7 @@ export type PropsType = {
onAddStory: () => unknown; onAddStory: () => unknown;
onClick: () => unknown; onClick: () => unknown;
queueStoryDownload: (storyId: string) => unknown; queueStoryDownload: (storyId: string) => unknown;
showToast: ShowToastActionCreatorType;
}; };
export const MyStoriesButton = ({ export const MyStoriesButton = ({
@ -28,6 +32,7 @@ export const MyStoriesButton = ({
onAddStory, onAddStory,
onClick, onClick,
queueStoryDownload, queueStoryDownload,
showToast,
}: PropsType): JSX.Element => { }: PropsType): JSX.Element => {
const { const {
acceptedMessageRequest, acceptedMessageRequest,
@ -40,9 +45,14 @@ export const MyStoriesButton = ({
title, title,
} = me; } = me;
if (!newestStory) {
return ( return (
<div className="Stories__my-stories"> <StoriesAddStoryButton
<div className="StoryListItem__button"> i18n={i18n}
moduleClassName="StoryListItem"
onAddStory={onAddStory}
showToast={showToast}
>
<div className="MyStories__avatar-container"> <div className="MyStories__avatar-container">
<Avatar <Avatar
acceptedMessageRequest={acceptedMessageRequest} acceptedMessageRequest={acceptedMessageRequest}
@ -53,7 +63,6 @@ export const MyStoriesButton = ({
i18n={i18n} i18n={i18n}
isMe={Boolean(isMe)} isMe={Boolean(isMe)}
name={name} name={name}
onClick={onAddStory}
profileName={profileName} profileName={profileName}
sharedGroupNames={sharedGroupNames} sharedGroupNames={sharedGroupNames}
size={AvatarSize.FORTY_EIGHT} size={AvatarSize.FORTY_EIGHT}
@ -62,22 +71,50 @@ export const MyStoriesButton = ({
<div <div
aria-label={i18n('Stories__add')} aria-label={i18n('Stories__add')}
className="MyStories__avatar__add-story" className="MyStories__avatar__add-story"
onClick={ev => {
onAddStory();
ev.stopPropagation();
ev.preventDefault();
}}
onKeyDown={ev => {
if (ev.key === 'Enter') {
onAddStory();
ev.stopPropagation();
ev.preventDefault();
}
}}
role="button"
tabIndex={0}
/> />
</div> </div>
<div className="StoryListItem__info">
<>
<div className="StoryListItem__info--title">
{i18n('Stories__mine')}
</div>
<div className="StoryListItem__info--timestamp">
{i18n('Stories__add')}
</div>
</>
</div>
</StoriesAddStoryButton>
);
}
return (
<div className="StoryListItem__button">
<div className="MyStories__avatar-container">
<StoriesAddStoryButton
i18n={i18n}
onAddStory={onAddStory}
showToast={showToast}
>
<Avatar
acceptedMessageRequest={acceptedMessageRequest}
avatarPath={avatarPath}
badge={undefined}
color={getAvatarColor(color)}
conversationType="direct"
i18n={i18n}
isMe={Boolean(isMe)}
name={name}
profileName={profileName}
sharedGroupNames={sharedGroupNames}
size={AvatarSize.FORTY_EIGHT}
title={title}
/>
<div
aria-label={i18n('Stories__add')}
className="MyStories__avatar__add-story"
/>
</StoriesAddStoryButton>
</div>
<div <div
className="StoryListItem__click-container" className="StoryListItem__click-container"
onClick={onClick} onClick={onClick}
@ -96,14 +133,8 @@ export const MyStoriesButton = ({
<div className="StoryListItem__info--title"> <div className="StoryListItem__info--title">
{i18n('Stories__mine')} {i18n('Stories__mine')}
</div> </div>
{!newestStory && (
<div className="StoryListItem__info--timestamp">
{i18n('Stories__add')}
</div>
)}
</> </>
</div> </div>
<div <div
aria-label={i18n('StoryListItem__label')} aria-label={i18n('StoryListItem__label')}
className={classNames('StoryListItem__previews', { className={classNames('StoryListItem__previews', {
@ -111,7 +142,6 @@ export const MyStoriesButton = ({
})} })}
> >
{hasMultiple && <div className="StoryListItem__previews--more" />} {hasMultiple && <div className="StoryListItem__previews--more" />}
{newestStory ? (
<StoryImage <StoryImage
attachment={newestStory.attachment} attachment={newestStory.attachment}
firstName={i18n('you')} firstName={i18n('you')}
@ -123,13 +153,6 @@ export const MyStoriesButton = ({
queueStoryDownload={queueStoryDownload} queueStoryDownload={queueStoryDownload}
storyId={newestStory.messageId} storyId={newestStory.messageId}
/> />
) : (
<div
aria-label={i18n('Stories__add')}
className="StoryListItem__previews--add StoryListItem__previews--image"
/>
)}
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -0,0 +1,90 @@
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
import React from 'react';
import type { LocalizerType } from '../types/Util';
import type { ShowToastActionCreatorType } from '../state/ducks/toast';
import { ContextMenu } from './ContextMenu';
import { Theme } from '../util/theme';
import { ToastType } from '../state/ducks/toast';
import {
isVideoGoodForStories,
ReasonVideoNotGood,
} from '../util/isVideoGoodForStories';
export type PropsType = {
children?: ReactNode;
i18n: LocalizerType;
moduleClassName?: string;
onAddStory: (file?: File) => unknown;
showToast: ShowToastActionCreatorType;
};
export const StoriesAddStoryButton = ({
children,
i18n,
moduleClassName,
onAddStory,
showToast,
}: PropsType): JSX.Element => {
return (
<ContextMenu
ariaLabel={i18n('Stories__add')}
i18n={i18n}
menuOptions={[
{
label: i18n('Stories__add-story--media'),
onClick: () => {
const input = document.createElement('input');
input.accept = 'image/*,video/mp4';
input.type = 'file';
input.onchange = async () => {
const file = input.files ? input.files[0] : undefined;
if (!file) {
return;
}
const result = await isVideoGoodForStories(file);
if (
result === ReasonVideoNotGood.UnsupportedCodec ||
result === ReasonVideoNotGood.UnsupportedContainer
) {
showToast(ToastType.StoryVideoUnsupported);
return;
}
if (result === ReasonVideoNotGood.TooLong) {
showToast(ToastType.StoryVideoTooLong);
return;
}
if (result !== ReasonVideoNotGood.AllGoodNevermind) {
showToast(ToastType.StoryVideoError);
return;
}
onAddStory(file);
};
input.click();
},
},
{
label: i18n('Stories__add-story--text'),
onClick: () => onAddStory(),
},
]}
moduleClassName={moduleClassName}
popperOptions={{
placement: 'bottom',
strategy: 'absolute',
}}
theme={Theme.Dark}
>
{children}
</ContextMenu>
);
};

View file

@ -20,14 +20,10 @@ import type { ShowToastActionCreatorType } from '../state/ducks/toast';
import { ContextMenu } from './ContextMenu'; import { ContextMenu } from './ContextMenu';
import { MyStoriesButton } from './MyStoriesButton'; import { MyStoriesButton } from './MyStoriesButton';
import { SearchInput } from './SearchInput'; import { SearchInput } from './SearchInput';
import { StoriesAddStoryButton } from './StoriesAddStoryButton';
import { StoryListItem } from './StoryListItem'; import { StoryListItem } from './StoryListItem';
import { Theme } from '../util/theme'; import { Theme } from '../util/theme';
import { ToastType } from '../state/ducks/toast';
import { isNotNil } from '../util/isNotNil'; import { isNotNil } from '../util/isNotNil';
import {
isVideoGoodForStories,
ReasonVideoNotGood,
} from '../util/isVideoGoodForStories';
const FUSE_OPTIONS: Fuse.IFuseOptions<ConversationStoryType> = { const FUSE_OPTIONS: Fuse.IFuseOptions<ConversationStoryType> = {
getFn: (story, path) => { getFn: (story, path) => {
@ -126,58 +122,11 @@ export const StoriesPane = ({
<div className="Stories__pane__header--title"> <div className="Stories__pane__header--title">
{i18n('Stories__title')} {i18n('Stories__title')}
</div> </div>
<ContextMenu <StoriesAddStoryButton
i18n={i18n} i18n={i18n}
menuOptions={[
{
label: i18n('Stories__add-story--media'),
onClick: () => {
const input = document.createElement('input');
input.accept = 'image/*,video/mp4';
input.type = 'file';
input.onchange = async () => {
const file = input.files ? input.files[0] : undefined;
if (!file) {
return;
}
const result = await isVideoGoodForStories(file);
if (
result === ReasonVideoNotGood.UnsupportedCodec ||
result === ReasonVideoNotGood.UnsupportedContainer
) {
showToast(ToastType.StoryVideoUnsupported);
return;
}
if (result === ReasonVideoNotGood.TooLong) {
showToast(ToastType.StoryVideoTooLong);
return;
}
if (result !== ReasonVideoNotGood.AllGoodNevermind) {
showToast(ToastType.StoryVideoError);
return;
}
onAddStory(file);
};
input.click();
},
},
{
label: i18n('Stories__add-story--text'),
onClick: () => onAddStory(),
},
]}
moduleClassName="Stories__pane__add-story" moduleClassName="Stories__pane__add-story"
popperOptions={{ onAddStory={onAddStory}
placement: 'bottom', showToast={showToast}
strategy: 'absolute',
}}
theme={Theme.Dark}
/> />
<ContextMenu <ContextMenu
i18n={i18n} i18n={i18n}
@ -218,6 +167,7 @@ export const StoriesPane = ({
onAddStory={onAddStory} onAddStory={onAddStory}
onClick={onMyStoriesClicked} onClick={onMyStoriesClicked}
queueStoryDownload={queueStoryDownload} queueStoryDownload={queueStoryDownload}
showToast={showToast}
/> />
{renderedStories.map(story => ( {renderedStories.map(story => (
<StoryListItem <StoryListItem

View file

@ -8959,6 +8959,13 @@
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-07-30T16:57:33.618Z" "updated": "2021-07-30T16:57:33.618Z"
}, },
{
"rule": "React-useRef",
"path": "ts/components/ContextMenu.tsx",
"line": " const virtualElement = useRef<VirtualElement>(generateVirtualElement(0, 0));",
"reasonCategory": "usageTrusted",
"updated": "2022-08-19T17:09:38.534Z"
},
{ {
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/components/ConversationList.tsx", "path": "ts/components/ConversationList.tsx",
@ -9203,13 +9210,6 @@
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-11-30T10:15:33.662Z" "updated": "2021-11-30T10:15:33.662Z"
}, },
{
"rule": "React-useRef",
"path": "ts/components/TextStoryCreator.tsx",
"line": " const textEditorRef = useRef<HTMLInputElement | null>(null);",
"reasonCategory": "usageTrusted",
"updated": "2022-06-16T23:23:32.306Z"
},
{ {
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/components/StoryImage.tsx", "path": "ts/components/StoryImage.tsx",
@ -9252,6 +9252,13 @@
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2022-06-16T23:23:32.306Z" "updated": "2022-06-16T23:23:32.306Z"
}, },
{
"rule": "React-useRef",
"path": "ts/components/TextStoryCreator.tsx",
"line": " const textEditorRef = useRef<HTMLInputElement | null>(null);",
"reasonCategory": "usageTrusted",
"updated": "2022-06-16T23:23:32.306Z"
},
{ {
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/components/Tooltip.tsx", "path": "ts/components/Tooltip.tsx",