/* Copyright 2022 Gravitational, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useEffect, useRef, useState } from 'react'; import styled, { css, useTheme } from 'styled-components'; import { ButtonIcon, Flex, Link, Text } from 'design'; import { Close } from 'design/Icon'; import type { NotificationItem, NotificationItemContent } from './types'; interface NotificationProps { item: NotificationItem; onRemove(): void; Icon: React.ElementType; getColor(theme): string; isAutoRemovable: boolean; autoRemoveDurationMs?: number; // Workaround until `styled` gets types. // Once the types are available, we can switch the type of Notification props to: // // NotificationProps & React.ComponentProps // // and remove the next line. [key: string]: any; } const defaultAutoRemoveDurationMs = 10_000; // 10s export function Notification(props: NotificationProps) { const { item, onRemove, Icon, getColor, isAutoRemovable, autoRemoveDurationMs, ...styleProps } = props; const [isHovered, setIsHovered] = useState(false); const [isExpanded, setIsExpanded] = useState(false); const timeoutHandler = useRef(); const theme = useTheme(); useEffect(() => { if (!isHovered && isAutoRemovable) { timeoutHandler.current = setTimeout( onRemove, autoRemoveDurationMs || defaultAutoRemoveDurationMs ) as unknown as number; } return () => { if (timeoutHandler.current) { clearTimeout(timeoutHandler.current); } }; }, [isHovered]); function toggleIsExpanded() { setIsExpanded(wasExpanded => !wasExpanded); } const removeIcon = ( { e.stopPropagation(); onRemove(); }} > ); return ( { if (isHovered === false) { setIsHovered(true); } }} onMouseLeave={() => { if (isHovered === true) { setIsHovered(false); } }} onClick={toggleIsExpanded} {...styleProps} > {getRenderedContent(item.content, isExpanded, removeIcon)} ); } function getRenderedContent( content: NotificationItemContent, isExpanded: boolean, removeIcon: React.ReactNode ) { const longerTextCss = isExpanded ? textCss : shortTextCss; if (typeof content === 'string') { return ( {content} {removeIcon} ); } if (typeof content === 'object') { return (
{content.title}
{removeIcon}
{content.list && } {content.description} {content.link && ( {content.link.text} )}
); } } function List(props: { items: string[] }) { return ( ); } const textCss = css` line-height: 20px; overflow-wrap: break-word; white-space: pre-line; `; const shortTextCss = css` ${textCss}; display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; `; const Container = styled(Flex)` flex-direction: row; justify-content: space-between; background: ${props => props.theme.colors.primary.light}; min-height: 40px; width: 320px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.24); color: ${props => props.theme.colors.text.primary}; border-radius: 4px; cursor: pointer; `;