@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
142 lines (141 loc) • 8.22 kB
JavaScript
"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { MagnifyingGlassIcon, PlusSmallIcon } from "@heroicons/react/24/outline";
import Link_mjs_default from "./common/Link.mjs";
import { useMemo } from "react";
import Loader_mjs_default from "../assets/icons/Loader.mjs";
import { useConfig } from "../context/ConfigContext.mjs";
import { useI18n } from "../context/I18nContext.mjs";
import { SPECIFIC_IDS_TO_RUN_ACTION } from "../hooks/useAction.mjs";
import { Permission } from "../types.mjs";
import { slugify } from "../utils/tools.mjs";
import external_ActionsDropdown_mjs_default from "./ActionsDropdown.mjs";
import external_Breadcrumb_mjs_default from "./Breadcrumb.mjs";
import external_ExportDropdown_mjs_default from "./ExportDropdown.mjs";
import { buttonVariants } from "./radix/Button.mjs";
import AdvancedSearchButton_mjs_default from "./advancedSearch/AdvancedSearchButton.mjs";
function ListHeader({ resource, isPending, onSearchChange, search, actions: actionsProp, selectedRows, getSelectedRowsIds, onDelete, title, icon, totalCount, schema }) {
const { basePath, options } = useConfig();
const { t } = useI18n();
const hasPermission = (permission)=>!modelOptions?.permissions || modelOptions?.permissions?.includes(permission);
const modelOptions = options?.model?.[resource];
const canCreate = hasPermission(Permission.CREATE);
const canDelete = hasPermission(Permission.DELETE);
const selectedRowsCount = Object.keys(selectedRows).length;
const actions = useMemo(()=>{
const defaultActions = canDelete ? [
{
type: "server",
id: SPECIFIC_IDS_TO_RUN_ACTION.DELETE,
title: t("actions.delete.label"),
icon: "TrashIcon",
style: "destructive",
action: async ()=>{
await onDelete();
}
}
] : [];
return [
...actionsProp ?? [],
...defaultActions
];
}, [
actionsProp,
onDelete,
t,
canDelete
]);
const resourcePluralName = t(`model.${resource}.plural`, {}, title);
return /*#__PURE__*/ jsx(Fragment, {
children: /*#__PURE__*/ jsxs("div", {
className: "bg-nextadmin-background-default dark:bg-dark-nextadmin-background-default dark:border-b-dark-nextadmin-border-default border-b-nextadmin-border-default sticky top-14 z-20 flex h-auto w-full flex-row flex-wrap items-start justify-between gap-3 border-b px-4 py-4 shadow-sm sm:w-auto sm:items-center md:py-3 lg:top-0",
children: [
/*#__PURE__*/ jsx(external_Breadcrumb_mjs_default, {
breadcrumbItems: [
{
label: resourcePluralName,
href: `${basePath}/${slugify(resource)}`,
current: true,
icon
}
]
}),
/*#__PURE__*/ jsxs("div", {
className: "flex w-full flex-wrap items-center gap-2 sm:w-auto sm:flex-nowrap md:gap-4",
children: [
(totalCount || false || search && totalCount || false) && /*#__PURE__*/ jsx("span", {
className: "text-nextadmin-content-default dark:text-dark-nextadmin-content-default hidden min-w-fit text-sm sm:block",
children: search ? t("list.header.search.result_filtered", {
count: totalCount
}) : t("list.header.search.result", {
count: totalCount
})
}),
onSearchChange && /*#__PURE__*/ jsxs("div", {
className: "bg-nextadmin-background-subtle dark:bg-dark-nextadmin-background-subtle ring-nextadmin-border-strong dark:ring-dark-nextadmin-border-strong flex min-w-0 items-center justify-end gap-2 rounded-md px-3 py-1 ring-1",
children: [
/*#__PURE__*/ jsx("input", {
id: "search",
name: "search",
onInput: onSearchChange,
defaultValue: search,
type: "search",
className: "text-nextadmin-content-subtle dark:text-dark-nextadmin-content-subtle w-full min-w-0 rounded-md bg-[transparent] py-1.5 text-sm focus:outline-none focus:ring-0 focus:ring-offset-0",
placeholder: `${t("list.header.search.placeholder")} ${resourcePluralName.toLowerCase()}`
}),
/*#__PURE__*/ jsx("label", {
htmlFor: "search",
children: isPending ? /*#__PURE__*/ jsx(Loader_mjs_default, {
className: "stroke-nextadmin-content-default dark:stroke-dark-nextadmin-content-default h-6 w-6 animate-spin dark:stroke-gray-300"
}) : /*#__PURE__*/ jsx(MagnifyingGlassIcon, {
className: "text-nextadmin-content-default dark:text-dark-nextadmin-content-default h-6 w-6",
"aria-hidden": "true"
})
})
]
}),
/*#__PURE__*/ jsx(AdvancedSearchButton_mjs_default, {
resource: resource,
schema: schema
}),
Boolean(selectedRowsCount) && !!actions.length && /*#__PURE__*/ jsx(external_ActionsDropdown_mjs_default, {
actions: actions,
resource: resource,
selectedIds: getSelectedRowsIds(),
selectedCount: selectedRowsCount
}),
modelOptions?.list?.exports && (Array.isArray(modelOptions?.list?.exports) ? /*#__PURE__*/ jsx(external_ExportDropdown_mjs_default, {
exports: modelOptions?.list?.exports
}) : /*#__PURE__*/ jsx(Link_mjs_default, {
href: modelOptions?.list?.exports.url,
target: "_blank",
className: "text-nextadmin-content-inverted dark:text-dark-nextadmin-brand-inverted border-nextadmin-border-default dark:border-dark-nextadmin-border-default dark:bg-dark-nextadmin-background-subtle flex min-w-fit items-center gap-x-2 rounded-md border bg-transparent px-3 py-2 text-sm",
children: t("list.row.actions.export", {
format: modelOptions?.list?.exports.format
})
})),
canCreate && /*#__PURE__*/ jsxs(Link_mjs_default, {
href: `${basePath}/${slugify(resource)}/new`,
role: "button",
"data-testid": "add-new-button",
className: buttonVariants({
variant: "default",
size: "sm"
}),
children: [
/*#__PURE__*/ jsx("span", {
children: t("list.header.add.label")
}),
/*#__PURE__*/ jsx(PlusSmallIcon, {
className: "ml-2 h-5 w-5",
"aria-hidden": "true"
})
]
})
]
})
]
})
});
}
export { ListHeader as default };