{"ast":null,"code":"function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }\n\nfunction _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }\n\nfunction _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }\n\nimport { useCallback, useEffect, useMemo, useReducer } from 'react';\nimport groupBy from 'lodash/groupBy';\nimport intersection from 'lodash/intersection';\nimport uniq from 'lodash/uniq';\nimport { useRouter } from 'next/router';\nimport { useRunningRequest } from '~/hooks/useRequest';\nvar ActionType;\n\n(function (ActionType) {\n  ActionType[ActionType[\"setRuns\"] = 0] = \"setRuns\";\n  ActionType[ActionType[\"initTags\"] = 1] = \"initTags\";\n  ActionType[ActionType[\"setTags\"] = 2] = \"setTags\";\n  ActionType[ActionType[\"setFilteredTags\"] = 3] = \"setFilteredTags\";\n})(ActionType || (ActionType = {}));\n\nconst groupTags = (runs, tags) => Object.entries(groupBy(runs // get tags of selected runs\n.filter(run => runs.includes(run)) // group by runs\n.reduce((prev, run) => {\n  if (tags && tags[run]) {\n    Array.prototype.push.apply(prev, tags[run].map(label => ({\n      label,\n      run\n    })));\n  }\n\n  return prev;\n}, []), tag => tag.label)).map(([label, tags]) => ({\n  label,\n  runs: tags.map(tag => tag.run)\n}));\n\nconst reducer = (state, action) => {\n  switch (action.type) {\n    case ActionType.setRuns:\n      const runTags = groupTags(action.payload, state.initTags);\n      return _objectSpread({}, state, {\n        runs: action.payload,\n        tags: runTags,\n        filteredTags: runTags\n      });\n\n    case ActionType.initTags:\n      const newTags = groupTags(state.runs, action.payload);\n      return _objectSpread({}, state, {\n        initTags: action.payload,\n        tags: newTags,\n        filteredTags: newTags\n      });\n\n    case ActionType.setTags:\n      return _objectSpread({}, state, {\n        tags: action.payload,\n        filteredTags: action.payload\n      });\n\n    case ActionType.setFilteredTags:\n      return _objectSpread({}, state, {\n        filteredTags: action.payload\n      });\n\n    default:\n      throw new Error();\n  }\n};\n\nconst useTagFilter = (type, running) => {\n  const router = useRouter();\n  const {\n    data: runs,\n    loading: loadingRuns\n  } = useRunningRequest('/runs', running);\n  const {\n    data: tags,\n    loading: loadingTags\n  } = useRunningRequest(`/${type}/tags`, running);\n  const selectedRuns = useMemo(() => runs ? router.query.runs ? intersection(uniq(Array.isArray(router.query.runs) ? router.query.runs : router.query.runs.split(',')), runs) : runs : [], [router, runs]);\n  const {\n    0: state,\n    1: dispatch\n  } = useReducer(reducer, {\n    runs: selectedRuns,\n    initTags: {},\n    tags: groupTags(selectedRuns, tags)\n  }, initArgs => _objectSpread({}, initArgs, {\n    filteredTags: initArgs.tags\n  }));\n  const onChangeRuns = useCallback(runs => dispatch({\n    type: ActionType.setRuns,\n    payload: runs\n  }), [dispatch]);\n  const onInitTags = useCallback(tags => dispatch({\n    type: ActionType.initTags,\n    payload: tags\n  }), [dispatch]);\n  const onFilterTags = useCallback(tags => dispatch({\n    type: ActionType.setFilteredTags,\n    payload: tags\n  }), [dispatch]);\n  useEffect(() => onInitTags(tags || {}), [onInitTags, tags]);\n  useEffect(() => onChangeRuns(selectedRuns), [onChangeRuns, selectedRuns]);\n  return {\n    runs,\n    tags: state.tags,\n    selectedRuns: state.runs,\n    selectedTags: state.filteredTags,\n    onChangeRuns,\n    onFilterTags,\n    loadingRuns,\n    loadingTags\n  };\n};\n\nexport default useTagFilter;","map":null,"metadata":{},"sourceType":"module"}