207 lines
7.9 KiB
TypeScript
207 lines
7.9 KiB
TypeScript
// * Packages
|
|
import { FileTextIcon, UserIcon } from "lucide-react";
|
|
import { route } from "ziggy-js";
|
|
// * Components shadcn
|
|
import { Badge } from "@/components/ui/badge";
|
|
// * Other
|
|
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
|
|
import DataTable from "@/back/Components/Table/DataTable";
|
|
import DataTableSearch from "@/back/Components/Table/DataTableSearch";
|
|
import DataTableFilterButton from "@/back/Components/Table/DataTableFilterButton";
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import { Views } from "@/lib/utils";
|
|
import { ActivityLogModel, Resource } from "@/back/types/global";
|
|
import str from "@/hooks/use-string";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import { useFormatedDate } from "@/hooks/use-formated-value";
|
|
|
|
type ActivityLogsIndexProps = {
|
|
activitylogModels: Resource;
|
|
searchFields: string;
|
|
activityLogsEventEnum: Record<
|
|
string,
|
|
{ value: number; name: string; label: string; tailwindclass: string }
|
|
>;
|
|
};
|
|
|
|
export default function Index({
|
|
activitylogModels,
|
|
searchFields,
|
|
activityLogsEventEnum,
|
|
}: ActivityLogsIndexProps) {
|
|
const actionModels = [Views.VISUALIZATION];
|
|
const columns = [
|
|
{
|
|
accessorKey: "event",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
useTrans(`activity-logs.attributes.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({
|
|
row,
|
|
}: {
|
|
row: { original: ActivityLogModel; getValue: Function };
|
|
}) => {
|
|
const currentEvent = Object.values(
|
|
activityLogsEventEnum,
|
|
).filter((index) => index.value === row.getValue("event"));
|
|
const bgClass = `bg-${currentEvent[0].tailwindclass}`;
|
|
const hoverClass = `hover:bg-${currentEvent[0].tailwindclass}/90`;
|
|
const textClass = `text-${currentEvent[0].tailwindclass}-foreground`;
|
|
return currentEvent[0] ? (
|
|
<Badge className={`${textClass} ${bgClass} ${hoverClass}`}>
|
|
{str(useTrans(currentEvent[0].label)).ucFirst().value()}
|
|
</Badge>
|
|
) : (
|
|
useTrans("activity-logs.event_not_match")
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "user_id",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
useTrans(`activity-logs.attributes.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({
|
|
row,
|
|
}: {
|
|
row: { original: ActivityLogModel; getValue: Function };
|
|
}) => {
|
|
if (row.original.user)
|
|
return (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route("back.users.show", {
|
|
user: row.original.user_id,
|
|
})}
|
|
content={
|
|
<>
|
|
<UserIcon />
|
|
{`${row.original.user.first_name} ${row.original.user.last_name}`}
|
|
</>
|
|
}
|
|
tooltipContent={
|
|
<p>
|
|
{useTrans("activity-logs.access_model", {
|
|
params: {
|
|
model: useTrans(
|
|
"models.classes.user",
|
|
),
|
|
},
|
|
})}
|
|
</p>
|
|
}
|
|
size="sm"
|
|
variant="secondary"
|
|
/>
|
|
);
|
|
else if (row.original.is_console)
|
|
return useTrans("activity-logs.user_in_console");
|
|
else if (row.original.is_anonymous)
|
|
return useTrans("activity-logs.user_anonym");
|
|
else return useTrans("activity-logs.user_deleted");
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "model_class",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
useTrans(`activity-logs.attributes.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({
|
|
row,
|
|
}: {
|
|
row: { getValue: Function; original: ActivityLogModel };
|
|
}) => {
|
|
const modelSplit = row.getValue("model_class").split("\\");
|
|
const modelName = modelSplit[modelSplit.length - 1];
|
|
const model = str(modelName).kebabCase().value();
|
|
return row.original.event !==
|
|
activityLogsEventEnum.deleted.value ? (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route(
|
|
`back.${str(model).plural().value()}.show`,
|
|
row.original.model_id,
|
|
)}
|
|
content={
|
|
<>
|
|
<FileTextIcon />
|
|
{row.getValue("model_class")}
|
|
</>
|
|
}
|
|
tooltipContent={
|
|
<p>
|
|
{useTrans("activity-logs.access_model", {
|
|
params: {
|
|
model: useTrans(
|
|
`activity-logs.attributes.model_class`,
|
|
),
|
|
},
|
|
})}
|
|
</p>
|
|
}
|
|
size="sm"
|
|
variant="secondary"
|
|
/>
|
|
) : (
|
|
<Badge variant="outline">
|
|
{row.getValue("model_class")}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "created_at",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: { getValue: Function } }) => (
|
|
<Badge variant="secondary">
|
|
{useFormatedDate(row.getValue("created_at"))}
|
|
</Badge>
|
|
),
|
|
},
|
|
];
|
|
return (
|
|
<AuthenticatedLayout
|
|
title={str(useTrans("models.classes.activity-log"))
|
|
.ucFirst()
|
|
.plural()
|
|
.value()}
|
|
>
|
|
<DataTableSearch
|
|
modelRoute="activity-logs"
|
|
searchFields={searchFields}
|
|
/>
|
|
<DataTable
|
|
columns={columns}
|
|
modelRoute="activity-logs"
|
|
modelsPaginate={activitylogModels}
|
|
actions={actionModels}
|
|
/>
|
|
</AuthenticatedLayout>
|
|
);
|
|
}
|