ExploreActions.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { useRegisterActions, useKBar, Action, Priority } from 'kbar';
  2. import { FC, useEffect, useState } from 'react';
  3. import { useDispatch, useSelector } from 'react-redux';
  4. import { ExploreId } from 'app/types';
  5. import { splitOpen, splitClose } from './state/main';
  6. import { runQueries } from './state/query';
  7. import { isSplit } from './state/selectors';
  8. interface Props {
  9. exploreIdLeft: ExploreId;
  10. exploreIdRight?: ExploreId;
  11. }
  12. export const ExploreActions: FC<Props> = ({ exploreIdLeft, exploreIdRight }: Props) => {
  13. const [actions, setActions] = useState<Action[]>([]);
  14. const { query } = useKBar();
  15. const dispatch = useDispatch();
  16. const splitted = useSelector(isSplit);
  17. useEffect(() => {
  18. const exploreSection = {
  19. name: 'Explore',
  20. priority: Priority.HIGH + 1,
  21. };
  22. const actionsArr: Action[] = [];
  23. if (splitted) {
  24. actionsArr.push({
  25. id: 'explore/run-query-left',
  26. name: 'Run query (left)',
  27. keywords: 'query left',
  28. perform: () => {
  29. dispatch(runQueries(exploreIdLeft));
  30. },
  31. section: exploreSection,
  32. });
  33. if (exploreIdRight) {
  34. // we should always have the right exploreId if split
  35. actionsArr.push({
  36. id: 'explore/run-query-right',
  37. name: 'Run query (right)',
  38. keywords: 'query right',
  39. perform: () => {
  40. dispatch(runQueries(exploreIdRight));
  41. },
  42. section: exploreSection,
  43. });
  44. actionsArr.push({
  45. id: 'explore/split-view-close-left',
  46. name: 'Close split view left',
  47. keywords: 'split',
  48. perform: () => {
  49. dispatch(splitClose(exploreIdLeft));
  50. },
  51. section: exploreSection,
  52. });
  53. actionsArr.push({
  54. id: 'explore/split-view-close-right',
  55. name: 'Close split view right',
  56. keywords: 'split',
  57. perform: () => {
  58. dispatch(splitClose(exploreIdRight));
  59. },
  60. section: exploreSection,
  61. });
  62. }
  63. } else {
  64. actionsArr.push({
  65. id: 'explore/run-query',
  66. name: 'Run query',
  67. keywords: 'query',
  68. perform: () => {
  69. dispatch(runQueries(exploreIdLeft));
  70. },
  71. section: exploreSection,
  72. });
  73. actionsArr.push({
  74. id: 'explore/split-view-open',
  75. name: 'Open split view',
  76. keywords: 'split',
  77. perform: () => {
  78. dispatch(splitOpen());
  79. },
  80. section: exploreSection,
  81. });
  82. }
  83. setActions(actionsArr);
  84. }, [exploreIdLeft, exploreIdRight, splitted, query, dispatch]);
  85. useRegisterActions(!query ? [] : actions, [actions, query]);
  86. return null;
  87. };