-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TS] Add types to flash, clean up some logic (#1824)
* feat: add types to flash, clean up some logic * feat: set allowJs to false in web * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashReducer.ts Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashReducer.ts Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashReducer.ts Co-authored-by: Tobbe Lundberg <[email protected]> * clarify type import * remove unnecessary comment * remove unnecessary style assignment * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashContext.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/Flash.tsx Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/src/flash/FlashReducer.ts Co-authored-by: Tobbe Lundberg <[email protected]> * Update packages/web/tsconfig.json Co-authored-by: Tobbe Lundberg <[email protected]> Co-authored-by: Tobbe Lundberg <[email protected]> Co-authored-by: Daniel Choudhury <[email protected]> Co-authored-by: David Price <[email protected]>
- Loading branch information
1 parent
40e145d
commit 53bcdb2
Showing
7 changed files
with
135 additions
and
123 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
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,64 @@ | ||
import React, { createContext, useReducer, useMemo } from 'react' | ||
|
||
import type { FlashMessage } from 'src/flash/FlashReducer' | ||
import FlashReducer from 'src/flash/FlashReducer' | ||
|
||
type Message = { id: number; text: string } | ||
type MessageOptions = Omit<FlashMessage, 'text' | 'id'> | ||
|
||
type FlashContext = { | ||
messages: Message[] | ||
addMessage(text: string, options?: MessageOptions): void | ||
dismissMessage(messageId: number): void | ||
cycleMessage(messageId: number): void | ||
} | ||
|
||
export const FlashContext = createContext<FlashContext | null>(null) | ||
|
||
export const FlashProvider: React.FunctionComponent = ({ children }) => { | ||
const [messages, dispatch] = useReducer(FlashReducer, []) | ||
|
||
const actions = useMemo(() => { | ||
function addMessage(text: string, options: MessageOptions = {}) { | ||
const message = { text, ...options } | ||
dispatch({ | ||
type: 'ADD_MESSAGE', | ||
message, | ||
}) | ||
} | ||
|
||
function dismissMessage(messageId: number) { | ||
dispatch({ | ||
type: 'DISMISS_MESSAGE', | ||
messageId, | ||
}) | ||
} | ||
|
||
function cycleMessage(messageId: number) { | ||
dispatch({ | ||
type: 'CYCLE_MESSAGE', | ||
messageId, | ||
}) | ||
} | ||
return { addMessage, dismissMessage, cycleMessage } | ||
}, [dispatch]) | ||
|
||
const flashContextValue = useMemo(() => ({ messages, ...actions }), [ | ||
messages, | ||
actions, | ||
]) | ||
|
||
return ( | ||
<FlashContext.Provider value={flashContextValue}> | ||
{children} | ||
</FlashContext.Provider> | ||
) | ||
} | ||
|
||
export const useFlash = () => { | ||
const flash = React.useContext(FlashContext) | ||
if (!flash) { | ||
throw Error('useFlash must be used within a FlashProvider') | ||
} | ||
return flash | ||
} |
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,54 @@ | ||
export interface FlashMessage { | ||
id: number | ||
text: string | ||
style?: React.HTMLAttributes<HTMLDivElement>['style'] | ||
classes?: string | ||
persist?: boolean | ||
viewed?: boolean | ||
} | ||
|
||
type FlashAction = | ||
| { | ||
type: 'ADD_MESSAGE' | ||
message: Omit<FlashMessage, 'id'> | ||
} | ||
| { | ||
type: 'DISMISS_MESSAGE' | ||
messageId: number | ||
} | ||
| { | ||
type: 'CYCLE_MESSAGE' | ||
messageId: number | ||
} | ||
|
||
export default (messages: FlashMessage[] = [], action: FlashAction) => { | ||
switch (action.type) { | ||
case 'ADD_MESSAGE': { | ||
const newMessage = { | ||
...action.message, | ||
id: messages.length, | ||
} | ||
|
||
return [...messages, newMessage] | ||
} | ||
case 'DISMISS_MESSAGE': { | ||
return messages.filter((msg) => msg.id !== action.messageId) | ||
} | ||
case 'CYCLE_MESSAGE': { | ||
// find the message | ||
// if viewed and not persist, remove it | ||
// else mark as viewed | ||
return messages.reduce<FlashMessage[]>((acc, msg) => { | ||
if (msg.id !== action.messageId) { | ||
return [...acc, msg] | ||
} | ||
|
||
if (msg.viewed && !msg.persist) { | ||
return acc | ||
} | ||
|
||
return [...acc, { ...msg, viewed: true }] | ||
}, []) | ||
} | ||
} | ||
} |
File renamed without changes.
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