Files
plane/apps/web/core/components/readonly/module.tsx
chuan 8ebde8aa05
Some checks failed
Branch Build CE / Build Setup (push) Has been cancelled
Branch Build CE / Build-Push Admin Docker Image (push) Has been cancelled
Branch Build CE / Build-Push Web Docker Image (push) Has been cancelled
Branch Build CE / Build-Push Space Docker Image (push) Has been cancelled
Branch Build CE / Build-Push Live Collaboration Docker Image (push) Has been cancelled
Branch Build CE / Build-Push API Server Docker Image (push) Has been cancelled
Branch Build CE / Build-Push Proxy Docker Image (push) Has been cancelled
Branch Build CE / Build-Push AIO Docker Image (push) Has been cancelled
Branch Build CE / Upload Build Assets (push) Has been cancelled
Branch Build CE / Build Release (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Codespell / Check for spelling errors (push) Has been cancelled
Sync Repositories / sync_changes (push) Has been cancelled
Initial commit: Plane
Synced from upstream: 8853637e981ed7d8a6cff32bd98e7afe20f54362
2025-11-07 00:00:52 +08:00

76 lines
2.1 KiB
TypeScript

"use client";
import { useEffect } from "react";
import { observer } from "mobx-react";
import { Layers } from "lucide-react";
// plane imports
import { useTranslation } from "@plane/i18n";
import { cn } from "@plane/utils";
// hooks
import { useModule } from "@/hooks/store/use-module";
export type TReadonlyModuleProps = {
className?: string;
hideIcon?: boolean;
value: string | string[] | null;
placeholder?: string;
projectId: string | undefined;
multiple?: boolean;
showCount?: boolean;
workspaceSlug: string;
};
export const ReadonlyModule: React.FC<TReadonlyModuleProps> = observer((props) => {
const {
className,
hideIcon = false,
value,
placeholder,
multiple = false,
showCount = true,
workspaceSlug,
projectId,
} = props;
const { t } = useTranslation();
const { getModuleById, fetchModules } = useModule();
const moduleIds = Array.isArray(value) ? value : value ? [value] : [];
const modules = moduleIds.map((id) => getModuleById(id)).filter(Boolean);
useEffect(() => {
if (moduleIds.length > 0 && projectId) {
fetchModules(workspaceSlug, projectId);
}
}, [value, projectId, workspaceSlug]);
if (modules.length === 0) {
return (
<div className={cn("flex items-center gap-1 text-sm", className)}>
{!hideIcon && <Layers className="size-4 flex-shrink-0" />}
<span className="flex-grow truncate">{placeholder ?? t("common.none")}</span>
</div>
);
}
if (multiple) {
const displayText =
showCount && modules.length > 1 ? `${modules[0]?.name} +${modules.length - 1}` : modules[0]?.name;
return (
<div className={cn("flex items-center gap-1 text-sm", className)}>
{!hideIcon && <Layers className="size-4 flex-shrink-0" />}
<span className="flex-grow truncate">{displayText}</span>
</div>
);
}
const moduleItem = modules[0];
return (
<div className={cn("flex items-center gap-2 text-sm", className)}>
{!hideIcon && <Layers className="size-4 flex-shrink-0" />}
<span className="flex-grow truncate">{moduleItem?.name}</span>
</div>
);
});