diff options
| author | Shin'ya Ueoka <ueokande@i-beam.org> | 2021-04-12 13:09:09 +0000 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-12 13:09:09 +0000 | 
| commit | d80d0f87b82ba4bd74ed9b2bb7354421a28a11b3 (patch) | |
| tree | 691185ad88418d0f44c236d0913cf5c425b29b23 /src/console/completion/reducer.ts | |
| parent | ea73c900f66107fd4a5b2f3b05080bcf643c94ea (diff) | |
| parent | 8a5bba1da639355a25da8c279a9f1cf0a7300a9f (diff) | |
Merge pull request #1098 from ueokande/replace-redux-with-react-hooks
Refactor state management with React Hooks on Console
Diffstat (limited to 'src/console/completion/reducer.ts')
| -rw-r--r-- | src/console/completion/reducer.ts | 96 | 
1 files changed, 96 insertions, 0 deletions
diff --git a/src/console/completion/reducer.ts b/src/console/completion/reducer.ts new file mode 100644 index 0000000..905451f --- /dev/null +++ b/src/console/completion/reducer.ts @@ -0,0 +1,96 @@ +import Completions from "../Completions"; +import CompletionType from "../../shared/CompletionType"; +import { +  INIT_COMPLETIONS, +  SET_COMPLETION_SOURCE, +  SET_COMPLETIONS, +  COMPLETION_NEXT, +  COMPLETION_PREV, +  CompletionAction, +} from "./actions"; + +export interface State { +  completionTypes?: CompletionType[]; +  completionSource: string; +  completions: Completions; +  select: number; +} + +export const defaultState = { +  completionTypes: undefined, +  completionSource: "", +  completions: [], +  select: -1, +}; + +const nextSelection = (state: State): number => { +  const length = state.completions +    .map((g) => g.items.length) +    .reduce((x, y) => x + y, 0); +  if (length === 0) { +    return -1; +  } +  if (state.select < 0) { +    return 0; +  } +  if (state.select + 1 < length) { +    return state.select + 1; +  } +  return -1; +}; + +const prevSelection = (state: State): number => { +  if (state.completions.length === 0) { +    return -1; +  } +  const length = state.completions +    .map((g) => g.items.length) +    .reduce((x, y) => x + y); +  if (state.select < 0) { +    return length - 1; +  } +  return state.select - 1; +}; + +// eslint-disable-next-line max-lines-per-function +export default function reducer( +  state: State = defaultState, +  action: CompletionAction +): State { +  switch (action.type) { +    case INIT_COMPLETIONS: +      return { +        ...state, +        completionTypes: action.completionTypes, +        completions: [], +        select: -1, +      }; +    case SET_COMPLETION_SOURCE: +      return { +        ...state, +        completionSource: action.completionSource, +        select: -1, +      }; +    case SET_COMPLETIONS: +      return { +        ...state, +        completions: action.completions, +      }; +    case COMPLETION_NEXT: { +      const select = nextSelection(state); +      return { +        ...state, +        select: select, +      }; +    } +    case COMPLETION_PREV: { +      const select = prevSelection(state); +      return { +        ...state, +        select: select, +      }; +    } +    default: +      return state; +  } +}  | 
