useHoverIndentGuide.ts 1.2 KB

123456789101112131415161718192021222324252627282930
  1. import { useCallback, useState } from 'react';
  2. /**
  3. * This is used internally to handle hover state of indent guide. As indent guides are separate
  4. * components per each row/span and you need to highlight all in multiple rows to make the effect of single line
  5. * they need this kind of common imperative state changes.
  6. *
  7. * Ideally would be changed to trace view internal state.
  8. */
  9. export function useHoverIndentGuide() {
  10. const [hoverIndentGuideIds, setHoverIndentGuideIds] = useState(new Set<string>());
  11. const addHoverIndentGuideId = useCallback(function addHoverIndentGuideId(spanID: string) {
  12. setHoverIndentGuideIds((prevState) => {
  13. const newHoverIndentGuideIds = new Set(prevState);
  14. newHoverIndentGuideIds.add(spanID);
  15. return newHoverIndentGuideIds;
  16. });
  17. }, []);
  18. const removeHoverIndentGuideId = useCallback(function removeHoverIndentGuideId(spanID: string) {
  19. setHoverIndentGuideIds((prevState) => {
  20. const newHoverIndentGuideIds = new Set(prevState);
  21. newHoverIndentGuideIds.delete(spanID);
  22. return newHoverIndentGuideIds;
  23. });
  24. }, []);
  25. return { hoverIndentGuideIds, addHoverIndentGuideId, removeHoverIndentGuideId };
  26. }