import {
  getNodeEntries,
  isElement,
  TNodeEntry,
  useEditorSelector,
} from '@udecode/plate-common';

import { isAttachment } from '../utils/isAttachment';
import { TAttachmentElement } from './types';

export function useAttachmentList() {
  const fileEntry: Generator<TNodeEntry<TAttachmentElement>> =
    useEditorSelector((editor) => {
      return getNodeEntries(editor, {
        at: [],
        match: (n) => isElement(n) && isAttachment(n),
      });
    }, []);

  return fileEntry;
}
