import { LoaderFunction, json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
import { withAuth } from "~/auth/withAuth";
import { DatasetsPage } from "~/pages/datasets/DatasetsPage";
import { DatasetsLoaderTypes } from "~/types/DatasetsLoader.types";

export const loader: LoaderFunction = withAuth(async ({ api, params }) => {
  const envId = params.envId!;
  const datasets = await api.dataset.datasetControllerGetAllDatasetsForEnv({
    envId,
  });
  return json<DatasetsLoaderTypes>({ datasets });
});

export default function DatasetsRoute() {
  const { datasets } = useLoaderData<DatasetsLoaderTypes>();
  return <DatasetsPage datasets={datasets} />;
}
