|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +import * as React from 'react'; |
| 4 | +import { useState } from 'react'; |
| 5 | + |
| 6 | +import Button from '~components/button'; |
| 7 | +import Flashbar, { FlashbarProps } from '~components/flashbar'; |
| 8 | +import { setPersistenceFunctionsForTesting } from '~components/internal/persistence'; |
| 9 | +import SpaceBetween from '~components/space-between'; |
| 10 | +import Toggle from '~components/toggle'; |
| 11 | + |
| 12 | +import FocusTarget from '../common/focus-target'; |
| 13 | +import ScreenshotArea from '../utils/screenshot-area'; |
| 14 | + |
| 15 | +const params = new URLSearchParams(window.location.hash.split('?')[1] || ''); |
| 16 | +setPersistenceFunctionsForTesting({ |
| 17 | + retrieveFlashbarDismiss: async (persistenceConfig: FlashbarProps.PersistenceConfig) => { |
| 18 | + const dismissed = Boolean(params.get('dismissedKeys')?.includes(persistenceConfig.uniqueKey)); |
| 19 | + const result = await new Promise<boolean>(resolve => |
| 20 | + setTimeout(() => resolve(dismissed), Math.min(parseInt(params.get('mockRetrieveDelay') || '0'), 150)) |
| 21 | + ); |
| 22 | + return result; |
| 23 | + }, |
| 24 | +}); |
| 25 | + |
| 26 | +export default function FlashbarTest() { |
| 27 | + const [items, setItems] = useState<FlashbarProps.MessageDefinition[]>([ |
| 28 | + { |
| 29 | + type: 'success', |
| 30 | + dismissible: true, |
| 31 | + dismissLabel: 'Dismiss message', |
| 32 | + content: 'Success flash message without persistence', |
| 33 | + id: 'message_1', |
| 34 | + onDismiss: () => setItems(items => items.filter(item => item.id !== 'message_1')), |
| 35 | + }, |
| 36 | + { |
| 37 | + type: 'warning', |
| 38 | + dismissible: true, |
| 39 | + dismissLabel: 'Dismiss message', |
| 40 | + content: 'Warning flash message without persistence', |
| 41 | + id: 'message_2', |
| 42 | + onDismiss: () => setItems(items => items.filter(item => item.id !== 'message_2')), |
| 43 | + }, |
| 44 | + { |
| 45 | + type: 'info', |
| 46 | + dismissible: true, |
| 47 | + dismissLabel: 'Dismiss message', |
| 48 | + content: 'Notification flash message 1 with persistence', |
| 49 | + id: 'message_3', |
| 50 | + onDismiss: () => setItems(items => items.filter(item => item.id !== 'message_3')), |
| 51 | + persistenceConfig: { |
| 52 | + uniqueKey: 'persistence_1', |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + type: 'in-progress', |
| 57 | + dismissible: true, |
| 58 | + dismissLabel: 'Dismiss message', |
| 59 | + content: 'Notification flash message 2 with persistence', |
| 60 | + id: 'message_4', |
| 61 | + onDismiss: () => setItems(items => items.filter(item => item.id !== 'message_4')), |
| 62 | + persistenceConfig: { |
| 63 | + uniqueKey: 'persistence_2', |
| 64 | + }, |
| 65 | + }, |
| 66 | + ]); |
| 67 | + const [stackItems, setStackItems] = useState(params.get('stackItems') === 'true'); |
| 68 | + |
| 69 | + const addFlashItem = (withPersistence: boolean) => { |
| 70 | + const id = `message_${Date.now()}`; |
| 71 | + const newItem: FlashbarProps.MessageDefinition = { |
| 72 | + type: 'info', |
| 73 | + dismissible: true, |
| 74 | + dismissLabel: 'Dismiss message', |
| 75 | + content: `New flash message ${withPersistence ? 'with' : 'without'} persistence`, |
| 76 | + id, |
| 77 | + ariaRole: 'status', |
| 78 | + onDismiss: () => setItems(items => items.filter(item => item.id !== id)), |
| 79 | + ...(withPersistence && { |
| 80 | + persistenceConfig: { |
| 81 | + uniqueKey: `new_${id}`, |
| 82 | + }, |
| 83 | + }), |
| 84 | + }; |
| 85 | + setItems(items => [...items, newItem]); |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + <h1>Flashbar test with Persistence</h1> |
| 91 | + <SpaceBetween size="xs"> |
| 92 | + <div>This page is to test Persistence with retrival delay (the maximum possible delay is 150ms)</div> |
| 93 | + <SpaceBetween direction="horizontal" size="xs"> |
| 94 | + <Button data-id="add-no-persistence-item" onClick={() => addFlashItem(false)}> |
| 95 | + Add without persistence |
| 96 | + </Button> |
| 97 | + <Button data-id="add-persistence-item" onClick={() => addFlashItem(true)}> |
| 98 | + Add with persistence |
| 99 | + </Button> |
| 100 | + <Toggle |
| 101 | + data-id="stack-items" |
| 102 | + checked={stackItems} |
| 103 | + onChange={({ detail }) => { |
| 104 | + setStackItems(detail.checked); |
| 105 | + const url = new URL(window.location.href); |
| 106 | + const params = new URLSearchParams(url.hash.split('?')[1] || ''); |
| 107 | + params.set('stackItems', detail.checked.toString()); |
| 108 | + window.history.replaceState({}, '', `${url.pathname}${url.hash.split('?')[0]}?${params}`); |
| 109 | + }} |
| 110 | + > |
| 111 | + Stack items |
| 112 | + </Toggle> |
| 113 | + </SpaceBetween> |
| 114 | + <FocusTarget /> |
| 115 | + </SpaceBetween> |
| 116 | + <ScreenshotArea> |
| 117 | + <Flashbar items={items} stackItems={stackItems} /> |
| 118 | + </ScreenshotArea> |
| 119 | + </> |
| 120 | + ); |
| 121 | +} |
0 commit comments