tooot/src/components/Emojis/helpers/EmojisContext.tsx

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-18 23:28:14 +02:00
import { createContext, Dispatch, MutableRefObject, RefObject } from 'react'
import { TextInput } from 'react-native'
type inputProps = {
2022-09-18 23:28:14 +02:00
value: [string, (value: string) => void]
selection: [{ start: number; end?: number }, (selection: { start: number; end?: number }) => void]
isFocused: MutableRefObject<boolean>
2022-09-23 00:21:41 +02:00
ref: RefObject<TextInput> // For controlling focus
maxLength?: number
}
2021-05-12 15:40:55 +02:00
2022-09-20 22:23:01 +02:00
export type Emojis = MutableRefObject<
| {
title: string
data: Pick<Mastodon.Emoji, 'shortcode' | 'url' | 'static_url'>[][]
type?: 'frequent'
}[]
| null
>
2021-05-12 15:40:55 +02:00
export type EmojisState = {
inputProps: inputProps[]
2022-09-18 23:28:14 +02:00
targetIndex: number
2021-05-12 15:40:55 +02:00
}
export type EmojisAction =
| { type: 'input'; payload: EmojisState['inputProps'] }
2022-09-18 23:28:14 +02:00
| { type: 'target'; payload: EmojisState['targetIndex'] }
2021-05-12 15:40:55 +02:00
type ContextType = {
emojisState: EmojisState
emojisDispatch: Dispatch<EmojisAction>
}
const EmojisContext = createContext<ContextType>({} as ContextType)
2021-05-23 23:19:37 +02:00
export const emojisReducer = (state: EmojisState, action: EmojisAction) => {
switch (action.type) {
case 'input':
return { ...state, inputProps: action.payload }
2022-09-18 23:28:14 +02:00
case 'target':
return { ...state, targetIndex: action.payload }
2021-05-23 23:19:37 +02:00
}
}
2021-05-12 15:40:55 +02:00
export default EmojisContext