import { ReactNode } from "react";
import { Briefcase, CalendarDays, CircleUser, Tag } from "lucide-react";
// hooks
import { useProject, useModule, useCycle, useProjectState, useMember, useLabel } from "hooks/store";
// ui
import {
Avatar,
ContrastIcon,
CycleGroupIcon,
DiceIcon,
DoubleCircleIcon,
PriorityIcon,
StateGroupIcon,
} from "@plane/ui";
// types
import { TIssuePriorities, TStateGroups, IIssueFilterOptions } from "@plane/types";
// constants
import { STATE_GROUP_PROPERTY, PRIORITIES_PROPERTY, DATE_PROPERTY } from "constants/views/filters";
// helpers
import { renderEmoji } from "helpers/emoji.helper";
import { renderFormattedDate } from "helpers/date-time.helper";
type TFilterPropertyDetails = {
icon: ReactNode;
label: string;
};
type TFilterPropertyDefaultDetails = {
icon: ReactNode;
label: string;
};
export const useViewFilter = (workspaceSlug: string, projectId: string | undefined) => {
const { getProjectById } = useProject();
const { getModuleById } = useModule();
const { getCycleById } = useCycle();
const { getStateById } = useProjectState();
const { getUserDetails } = useMember();
const { getLabelById } = useLabel();
if (!workspaceSlug || !projectId) return undefined;
const propertyDefaultDetails = (filterKey: keyof IIssueFilterOptions): TFilterPropertyDefaultDetails | undefined => {
if (!filterKey) return undefined;
switch (filterKey) {
case "project":
return {
icon: ,
label: "Projects",
};
case "module":
return {
icon: ,
label: "Modules",
};
case "cycle":
return {
icon: ,
label: "Cycles",
};
case "priority":
return {
icon: ,
label: "Priorities",
};
case "state":
return {
icon: ,
label: "States",
};
case "state_group":
return {
icon: ,
label: "State Groups",
};
case "assignees":
return {
icon: ,
label: "Assignees",
};
case "mentions":
return {
icon: ,
label: "Mentions",
};
case "subscriber":
return {
icon: ,
label: "Subscribers",
};
case "created_by":
return {
icon: ,
label: "Creators",
};
case "labels":
return {
icon: ,
label: "Labels",
};
case "start_date":
return {
icon: ,
label: "Start Dates",
};
case "target_date":
return {
icon: ,
label: "Target Dates",
};
default:
return undefined;
}
};
const propertyDetails = (
filterKey: keyof IIssueFilterOptions,
propertyId: string
): TFilterPropertyDetails | undefined => {
if (!filterKey || !propertyId) return undefined;
switch (filterKey) {
case "project":
const projectPropertyDetail = getProjectById(propertyId);
if (!projectPropertyDetail) return undefined;
return {
icon: (
<>
{projectPropertyDetail?.logo_props?.in_use === "emoji" &&
projectPropertyDetail?.logo_props?.emoji?.value ? (
{projectPropertyDetail?.logo_props?.emoji.value
?.split("-")
.map((emoji) => String.fromCodePoint(parseInt(emoji, 10)))}
) : projectPropertyDetail?.logo_props?.in_use === "icon" &&
projectPropertyDetail?.logo_props?.icon?.name ? (
{renderEmoji(projectPropertyDetail?.logo_props?.icon?.name)}
) : (
)}
>
),
label: projectPropertyDetail.name,
};
case "module":
const modulePropertyDetail = getModuleById(propertyId);
if (!modulePropertyDetail) return undefined;
return {
icon: ,
label: modulePropertyDetail.name,
};
case "cycle":
const cyclePropertyDetail = getCycleById(propertyId);
if (!cyclePropertyDetail) return undefined;
return {
icon: ,
label: cyclePropertyDetail.name,
};
case "priority":
const priorityPropertyDetail = PRIORITIES_PROPERTY?.[propertyId as TIssuePriorities];
if (!priorityPropertyDetail) return undefined;
return {
icon: ,
label: priorityPropertyDetail.label,
};
case "state":
const statePropertyDetail = getStateById(propertyId);
if (!statePropertyDetail) return undefined;
return {
icon: ,
label: statePropertyDetail.name,
};
case "state_group":
const stateGroupPropertyDetail = STATE_GROUP_PROPERTY?.[propertyId as TStateGroups];
if (!stateGroupPropertyDetail) return undefined;
return {
icon: ,
label: stateGroupPropertyDetail.label,
};
case "assignees":
const assigneePropertyDetail = getUserDetails(propertyId);
if (!assigneePropertyDetail) return undefined;
return {
icon: (
),
label: assigneePropertyDetail.display_name,
};
case "mentions":
const mentionPropertyDetail = getUserDetails(propertyId);
if (!mentionPropertyDetail) return undefined;
return {
icon: (
),
label: mentionPropertyDetail.display_name,
};
case "subscriber":
const subscribedPropertyDetail = getUserDetails(propertyId);
if (!subscribedPropertyDetail) return undefined;
return {
icon: (
),
label: subscribedPropertyDetail.display_name,
};
case "created_by":
const createdByPropertyDetail = getUserDetails(propertyId);
if (!createdByPropertyDetail) return undefined;
return {
icon: (
),
label: createdByPropertyDetail.display_name,
};
case "labels":
const labelPropertyDetail = getLabelById(propertyId);
if (!labelPropertyDetail) return undefined;
return {
icon: (
),
label: labelPropertyDetail.name,
};
case "start_date":
if (propertyId.includes("-")) {
const customDateString = propertyId.split(";");
return {
icon: ,
label: `${customDateString[1].charAt(0).toUpperCase()}${customDateString[1].slice(1)} ${renderFormattedDate(
customDateString[0]
)}`,
};
} else {
const startDatePropertyDetail = DATE_PROPERTY?.[propertyId];
if (!startDatePropertyDetail) return undefined;
return {
icon: ,
label: startDatePropertyDetail.label,
};
}
case "target_date":
if (propertyId.includes("-")) {
const customDateString = propertyId.split(";");
return {
icon: ,
label: `${customDateString[1].charAt(0).toUpperCase()}${customDateString[1].slice(1)} ${renderFormattedDate(
customDateString[0]
)}`,
};
} else {
const targetDatePropertyDetail = DATE_PROPERTY?.[propertyId];
if (!targetDatePropertyDetail) return undefined;
return {
icon: ,
label: targetDatePropertyDetail.label,
};
}
default:
return undefined;
}
};
return {
propertyDefaultDetails,
propertyDetails,
};
};