-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: change toast hook to nanostores
- Loading branch information
Showing
19 changed files
with
170 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -1,3 +1,11 @@ | ||
import { vi } from 'vitest' | ||
|
||
import { chromeMock } from '@shared/browser/__mocks__/chrome' | ||
import * as toastModule from '@shared/ui/Toast' | ||
import { getErrorToastMock } from '@shared/ui/Toast/helpers/__mock__/getErrorToast' | ||
import { addToastMock } from '@shared/ui/Toast/model/__mock__/store' | ||
|
||
global.chrome = chromeMock | ||
|
||
vi.spyOn(toastModule, 'getErrorToast').mockImplementation(getErrorToastMock) | ||
vi.spyOn(toastModule, 'addToast').mockImplementation(addToastMock) |
This file contains 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 |
---|---|---|
@@ -1,3 +1,14 @@ | ||
import { SelectLanguages } from '@features/language/SelectLanguages' | ||
|
||
import { LANGUAGES } from '@entities/language' | ||
|
||
import { Toasts } from '@shared/ui/Toast' | ||
|
||
export const Popup = () => { | ||
return <main>popup</main> | ||
return ( | ||
<main> | ||
<SelectLanguages languages={LANGUAGES} /> | ||
<Toasts /> | ||
</main> | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,5 @@ | ||
import { MockedFunction, vi } from 'vitest' | ||
|
||
import { getErrorToast } from '../getErrorToast' | ||
|
||
export const getErrorToastMock: MockedFunction<typeof getErrorToast> = vi.fn() |
This file contains 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,16 @@ | ||
import { browser } from '@shared/browser' | ||
import { TBaseError } from '@shared/libs/operationResult' | ||
|
||
import { ErrorDetails } from '../../ErrorDetails' | ||
import { IToast } from '../model/types' | ||
|
||
export const getErrorToast = (error: TBaseError): Omit<IToast, 'id'> => ({ | ||
type: 'error', | ||
title: browser.i18n.getMessage(error.type), | ||
details: ( | ||
<ErrorDetails | ||
type={browser.i18n.getMessage(error.type)} | ||
content={error.error?.toString() ?? ''} | ||
/> | ||
), | ||
}) |
This file contains 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 @@ | ||
export { getErrorToast } from './getErrorToast' |
This file contains 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 |
---|---|---|
@@ -1 +1,3 @@ | ||
export { ToastProvider, useToast } from './ToastContext' | ||
export * from './ui' | ||
export * from './model' | ||
export * from './helpers' |
This file contains 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,5 @@ | ||
import { MockedFunction, vi } from 'vitest' | ||
|
||
import { addToast } from '../store' | ||
|
||
export const addToastMock: MockedFunction<typeof addToast> = vi.fn() |
This file contains 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 @@ | ||
export { addToast } from './store' |
This file contains 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,32 @@ | ||
import { nanoid } from 'nanoid' | ||
import { atom } from 'nanostores' | ||
|
||
import { IToast } from './types' | ||
|
||
const timeouts: Record<string, NodeJS.Timeout> = {} | ||
|
||
export const $toasts = atom<IToast[]>([]) | ||
|
||
export const addToast = (toast: Omit<IToast, 'id'>) => { | ||
const toastId = nanoid() | ||
|
||
$toasts.set([...$toasts.get(), { id: toastId, ...toast }]) | ||
|
||
timeouts[toastId] = setTimeout(() => { | ||
removeToast(toastId) | ||
}, 2000) | ||
} | ||
|
||
export const removeToast = (id: string) => { | ||
clearTimeout(timeouts[id]) | ||
delete timeouts[id] | ||
|
||
$toasts.set($toasts.get().filter((toast) => toast.id !== id)) | ||
} | ||
|
||
export const cleanupToasts = () => { | ||
for (const id in timeouts) { | ||
clearTimeout(timeouts[id]) | ||
} | ||
$toasts.set([]) | ||
} |
This file contains 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,10 @@ | ||
import { ComponentChildren } from 'preact' | ||
|
||
export type TToastType = 'error' | 'info' | 'success' | ||
export interface IToast { | ||
title: string | ||
message?: ComponentChildren | ||
details?: ComponentChildren | ||
id: string | ||
type: TToastType | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,37 @@ | ||
import { useStore } from '@nanostores/preact' | ||
import { cleanup } from '@testing-library/preact' | ||
import { ComponentChildren } from 'preact' | ||
import { useEffect, useState } from 'preact/hooks' | ||
|
||
import { Modal } from '../../Modal' | ||
import { $toasts, removeToast } from '../model/store' | ||
import { Toast } from './Toast' | ||
import styles from './styles.module.scss' | ||
|
||
export const Toasts = () => { | ||
const toasts = useStore($toasts) | ||
const [details, setDetails] = useState<ComponentChildren | null>(null) | ||
|
||
useEffect(() => { | ||
return () => cleanup() | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<ul className={styles.toasts}> | ||
{toasts.map((toast) => ( | ||
<Toast | ||
key={toast.id} | ||
toast={toast} | ||
removeToast={() => removeToast(toast.id)} | ||
openDetails={setDetails} | ||
/> | ||
))} | ||
</ul> | ||
|
||
<Modal open={!!details} onClose={() => setDetails(null)}> | ||
{details} | ||
</Modal> | ||
</> | ||
) | ||
} |
This file contains 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 @@ | ||
export { Toasts } from './Toasts' |
File renamed without changes.