2022-09-18 23:28:14 +02:00
|
|
|
import { createContext, Dispatch, MutableRefObject, RefObject } from 'react'
|
2022-09-18 01:02:25 +02:00
|
|
|
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
|
2022-09-18 01:02:25 +02:00
|
|
|
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 = {
|
2022-09-18 01:02:25 +02:00
|
|
|
inputProps: inputProps[]
|
2022-09-18 23:28:14 +02:00
|
|
|
targetIndex: number
|
2021-05-12 15:40:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export type EmojisAction =
|
2022-09-18 01:02:25 +02:00
|
|
|
| { 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) {
|
2022-09-18 01:02:25 +02:00
|
|
|
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
|