tinloof/sanity-kit

Option for custom sort, to allow sorting folders above pages

Closed this issue · 1 comments

Thanks for the excellent work with this package, and for making it available for use and collaboration.

I have had feedback that folders and pages would benefit from the ability to be sorted with folders on top, and pages underneath. I also understand this isn't to everyone's liking.

I wanted to ask if you'd be happy if I added the ability to override the sort function for this list, allowing it to be passed in via configuration?

A customSort function can be passed to createPagesNavigator within the plugin index, and then passed in to the NavigatorProvider, where it can be used as below:

e.g.

// packages/sanity-studio/src/plugins/navigator/context/index.tsx 
const items = Object.values(currentTree || rootTree).sort((a, b) => {
  const defaultSort = a.pathname && b.pathname ? a.pathname.localeCompare(b.pathname) : 0;

  return customSort ? customSort(a, b, defaultSort) : defaultSort
});

This could then be used as:

function sortFoldersPages(a: TreeNode, b: TreeNode, defaultSort: number) {
  if (a._type !== b._type) {
    return a._type === 'folder' ? -1 : 1;
  }

  return defaultSort;
}

export default defineConfig({
  plugins: [
    pages({
      customSort: sortFoldersPages,
    }),
  ],
});

We could provide this as an importable method from the package if desired, so it's simple for others to include.

Let me know if this would be a welcome addition, and I will open a PR.

I'll close this issue as it doesn't seem to be desired functionality, but if we do want to pick this up I am happy to open a PR for this.