2024-03-12 19:36:40 +05:30
|
|
|
import sortBy from "lodash/sortBy";
|
|
|
|
|
// types
|
2025-02-06 20:41:31 +05:30
|
|
|
import { EUserPermissions } from "@plane/constants";
|
2024-10-18 11:08:07 +05:30
|
|
|
import { TProjectDisplayFilters, TProjectFilters, TProjectOrderByOptions } from "@plane/types";
|
2024-03-21 20:59:34 +05:30
|
|
|
// helpers
|
|
|
|
|
import { getDate } from "@/helpers/date-time.helper";
|
|
|
|
|
import { satisfiesDateFilter } from "@/helpers/filter.helper";
|
2024-10-18 11:08:07 +05:30
|
|
|
// plane web constants
|
|
|
|
|
// types
|
|
|
|
|
import { TProject } from "@/plane-web/types";
|
2024-02-26 19:42:36 +05:30
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the sort order of the project.
|
|
|
|
|
* @param sortIndex
|
|
|
|
|
* @param destinationIndex
|
|
|
|
|
* @param projectId
|
|
|
|
|
* @returns number | undefined
|
|
|
|
|
*/
|
|
|
|
|
export const orderJoinedProjects = (
|
|
|
|
|
sourceIndex: number,
|
|
|
|
|
destinationIndex: number,
|
|
|
|
|
currentProjectId: string,
|
2024-10-18 11:08:07 +05:30
|
|
|
joinedProjects: TProject[]
|
2024-02-26 19:42:36 +05:30
|
|
|
): number | undefined => {
|
|
|
|
|
if (!currentProjectId || sourceIndex < 0 || destinationIndex < 0 || joinedProjects.length <= 0) return undefined;
|
|
|
|
|
|
|
|
|
|
let updatedSortOrder: number | undefined = undefined;
|
|
|
|
|
const sortOrderDefaultValue = 10000;
|
|
|
|
|
|
|
|
|
|
if (destinationIndex === 0) {
|
|
|
|
|
// updating project at the top of the project
|
|
|
|
|
const currentSortOrder = joinedProjects[destinationIndex].sort_order || 0;
|
|
|
|
|
updatedSortOrder = currentSortOrder - sortOrderDefaultValue;
|
2024-05-27 19:20:26 +05:30
|
|
|
} else if (destinationIndex === joinedProjects.length) {
|
2024-02-26 19:42:36 +05:30
|
|
|
// updating project at the bottom of the project
|
|
|
|
|
const currentSortOrder = joinedProjects[destinationIndex - 1].sort_order || 0;
|
|
|
|
|
updatedSortOrder = currentSortOrder + sortOrderDefaultValue;
|
|
|
|
|
} else {
|
|
|
|
|
// updating project in the middle of the project
|
2024-05-27 19:20:26 +05:30
|
|
|
const destinationTopProjectSortOrder = joinedProjects[destinationIndex - 1].sort_order || 0;
|
|
|
|
|
const destinationBottomProjectSortOrder = joinedProjects[destinationIndex].sort_order || 0;
|
|
|
|
|
const updatedValue = (destinationTopProjectSortOrder + destinationBottomProjectSortOrder) / 2;
|
|
|
|
|
updatedSortOrder = updatedValue;
|
2024-02-26 19:42:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return updatedSortOrder;
|
|
|
|
|
};
|
2024-03-06 19:15:48 +05:30
|
|
|
|
|
|
|
|
export const projectIdentifierSanitizer = (identifier: string): string =>
|
|
|
|
|
identifier.replace(/[^ÇŞĞIİÖÜA-Za-z0-9]/g, "");
|
2024-03-12 19:36:40 +05:30
|
|
|
|
2024-03-15 17:44:01 +05:30
|
|
|
/**
|
|
|
|
|
* @description Checks if the project should be rendered or not based on the user role
|
2024-10-18 11:08:07 +05:30
|
|
|
* @param {TProject} project
|
2024-03-15 17:44:01 +05:30
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
2024-10-18 11:08:07 +05:30
|
|
|
export const shouldRenderProject = (project: TProject): boolean =>
|
2024-09-11 17:10:15 +05:30
|
|
|
!!project.member_role && project.member_role >= EUserPermissions.MEMBER;
|
2024-03-15 17:44:01 +05:30
|
|
|
|
2024-03-12 19:36:40 +05:30
|
|
|
/**
|
|
|
|
|
* @description filters projects based on the filter
|
2024-10-18 11:08:07 +05:30
|
|
|
* @param {TProject} project
|
2024-03-12 19:36:40 +05:30
|
|
|
* @param {TProjectFilters} filters
|
|
|
|
|
* @param {TProjectDisplayFilters} displayFilters
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
*/
|
|
|
|
|
export const shouldFilterProject = (
|
2024-10-18 11:08:07 +05:30
|
|
|
project: TProject,
|
2024-03-12 19:36:40 +05:30
|
|
|
displayFilters: TProjectDisplayFilters,
|
|
|
|
|
filters: TProjectFilters
|
|
|
|
|
): boolean => {
|
|
|
|
|
let fallsInFilters = true;
|
|
|
|
|
Object.keys(filters).forEach((key) => {
|
|
|
|
|
const filterKey = key as keyof TProjectFilters;
|
|
|
|
|
if (filterKey === "access" && filters.access && filters.access.length > 0)
|
|
|
|
|
fallsInFilters = fallsInFilters && filters.access.includes(`${project.network}`);
|
|
|
|
|
if (filterKey === "lead" && filters.lead && filters.lead.length > 0)
|
|
|
|
|
fallsInFilters = fallsInFilters && filters.lead.includes(`${project.project_lead}`);
|
|
|
|
|
if (filterKey === "members" && filters.members && filters.members.length > 0) {
|
2025-02-04 16:02:07 +05:30
|
|
|
const memberIds = project.members;
|
|
|
|
|
fallsInFilters = fallsInFilters && filters.members.some((memberId) => memberIds?.includes(memberId));
|
2024-03-12 19:36:40 +05:30
|
|
|
}
|
|
|
|
|
if (filterKey === "created_at" && filters.created_at && filters.created_at.length > 0) {
|
2024-03-20 13:44:08 +05:30
|
|
|
const createdDate = getDate(project.created_at);
|
2024-03-12 19:36:40 +05:30
|
|
|
filters.created_at.forEach((dateFilter) => {
|
2024-03-20 13:44:08 +05:30
|
|
|
fallsInFilters = fallsInFilters && !!createdDate && satisfiesDateFilter(createdDate, dateFilter);
|
2024-03-12 19:36:40 +05:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-02-05 15:18:02 +05:30
|
|
|
if (displayFilters.my_projects && !project.member_role) fallsInFilters = false;
|
2024-03-21 20:59:34 +05:30
|
|
|
if (displayFilters.archived_projects && !project.archived_at) fallsInFilters = false;
|
|
|
|
|
if (project.archived_at) fallsInFilters = displayFilters.archived_projects ? fallsInFilters : false;
|
2024-03-12 19:36:40 +05:30
|
|
|
|
|
|
|
|
return fallsInFilters;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @description orders projects based on the orderByKey
|
2024-10-18 11:08:07 +05:30
|
|
|
* @param {TProject[]} projects
|
2024-03-12 19:36:40 +05:30
|
|
|
* @param {TProjectOrderByOptions | undefined} orderByKey
|
2024-10-18 11:08:07 +05:30
|
|
|
* @returns {TProject[]}
|
2024-03-12 19:36:40 +05:30
|
|
|
*/
|
2024-10-18 11:08:07 +05:30
|
|
|
export const orderProjects = (projects: TProject[], orderByKey: TProjectOrderByOptions | undefined): TProject[] => {
|
|
|
|
|
let orderedProjects: TProject[] = [];
|
2024-03-12 19:36:40 +05:30
|
|
|
if (projects.length === 0) return orderedProjects;
|
|
|
|
|
|
|
|
|
|
if (orderByKey === "sort_order") orderedProjects = sortBy(projects, [(p) => p.sort_order]);
|
|
|
|
|
if (orderByKey === "name") orderedProjects = sortBy(projects, [(p) => p.name.toLowerCase()]);
|
|
|
|
|
if (orderByKey === "-name") orderedProjects = sortBy(projects, [(p) => p.name.toLowerCase()]).reverse();
|
|
|
|
|
if (orderByKey === "created_at") orderedProjects = sortBy(projects, [(p) => p.created_at]);
|
|
|
|
|
if (orderByKey === "-created_at") orderedProjects = sortBy(projects, [(p) => !p.created_at]);
|
2025-02-04 16:02:07 +05:30
|
|
|
if (orderByKey === "members_length") orderedProjects = sortBy(projects, [(p) => p.members?.length]);
|
|
|
|
|
if (orderByKey === "-members_length") orderedProjects = sortBy(projects, [(p) => p.members?.length]).reverse();
|
2024-03-12 19:36:40 +05:30
|
|
|
|
|
|
|
|
return orderedProjects;
|
|
|
|
|
};
|