Sha256: 0933bca35b797dfbe927ee440c0a6040ca30f21fce66de3733dfddd322e0bb85

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

import { useReducer } from "react";

import * as Tags from "../../types/Tags";

function onlyUnique(value: string, index: number, self: string[]) {
  return self.indexOf(value) === index;
}

export function allTags(state: Tags.State) {
  return [...state.tags, ...state.enabled].filter(onlyUnique);
}

function normalize(tag: string, state: Tags.State) {
  return (
    allTags(state).filter((t) => t.toLowerCase() == tag.toLowerCase())[0] || tag
  );
}

export function isEnabled(tag: string, state: Tags.State) {
  return (
    state.enabled.map((t) => t.toLowerCase()).indexOf(tag.toLowerCase()) !== -1
  );
}

function toggle(tag: string, state: Tags.State) {
  if (isEnabled(tag, state)) {
    return { ...state, enabled: state.enabled.filter((t) => t !== tag) };
  } else {
    return { ...state, enabled: [...state.enabled, tag] };
  }
}

function reducer(state: Tags.State, action: Tags.Action) {
  const { type, payload } = action;
  switch (type) {
    case "addTag": {
      const normalized = normalize(payload, state);
      return {
        tags: [...state.tags, normalized].filter(onlyUnique),
        enabled: [...state.enabled, normalized].filter(onlyUnique)
      };
    }
    case "toggleTag":
      return toggle(normalize(payload, state), state);
    case "update":
      return payload;
    default:
      return state;
  }
}

export default function useTags(
  initTags: string[],
  initEnabled: string[]
): [Tags.State, React.Dispatch<Tags.Action>] {
  const [state, dispatch] = useReducer(reducer, {
    tags: initTags,
    enabled: initEnabled
  });
  return [state, dispatch];
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/javascript/components/TagEditor/useTags.ts