|
| 1 | +import { Suspense, lazy, useCallback } from "react"; |
| 2 | +import { useNavigate, useParams } from "react-router-dom"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { RefreshCw } from "lucide-react"; |
| 5 | +import { KgPlanningProvider, useKgPlanning } from "./store"; |
| 6 | +import TabBar, { type KgTab } from "./TabBar"; |
| 7 | +import FilterBar, { EmployeeInfoBar } from "./views/FilterBar"; |
| 8 | +import TeamView from "./views/TeamView"; |
| 9 | +import GanttView from "./views/GanttView"; |
| 10 | +import GridView from "./views/GridView"; |
| 11 | + |
| 12 | +const DashboardsRoot = lazy(() => import("./dashboards/Root")); |
| 13 | + |
| 14 | +const VALID_KG_TABS: readonly KgTab[] = ["planning", "financial", "projects", "hr"]; |
| 15 | +function isValidKgTab(v: string | undefined): v is KgTab { |
| 16 | + return !!v && (VALID_KG_TABS as readonly string[]).includes(v); |
| 17 | +} |
| 18 | + |
| 19 | +export default function KgPlanningPage() { |
| 20 | + const navigate = useNavigate(); |
| 21 | + const { tab: tabParam } = useParams<{ tab?: string }>(); |
| 22 | + const tab: KgTab = isValidKgTab(tabParam) ? tabParam : "planning"; |
| 23 | + const setTab = useCallback((next: KgTab) => { |
| 24 | + navigate(next === "planning" ? "/" : `/${next}`); |
| 25 | + }, [navigate]); |
| 26 | + |
| 27 | + return ( |
| 28 | + <div className="flex flex-col h-full bg-white"> |
| 29 | + <TabBar active={tab} onChange={setTab} /> |
| 30 | + <div className="flex-1 overflow-hidden"> |
| 31 | + {tab === "planning" ? ( |
| 32 | + <KgPlanningProvider> |
| 33 | + <PlanningShell /> |
| 34 | + </KgPlanningProvider> |
| 35 | + ) : ( |
| 36 | + <Suspense fallback={<LoadingPanel />}> |
| 37 | + <DashboardsRoot tab={tab} /> |
| 38 | + </Suspense> |
| 39 | + )} |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function LoadingPanel() { |
| 46 | + const { t } = useTranslation(); |
| 47 | + return ( |
| 48 | + <div className="flex items-center justify-center h-full text-sm text-slate-500"> |
| 49 | + {t("extensions.kg_planning.loading")} |
| 50 | + </div> |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +function PlanningShell() { |
| 55 | + const { t } = useTranslation(); |
| 56 | + const { status, errorMessage, missingFields, reload, activeView, hydration } = useKgPlanning(); |
| 57 | + |
| 58 | + if (status === "loading") return <LoadingPanel />; |
| 59 | + |
| 60 | + if (status === "not_configured") { |
| 61 | + return ( |
| 62 | + <div className="flex flex-col items-center justify-center h-full gap-3 p-8 text-center"> |
| 63 | + <h2 className="text-lg font-semibold text-slate-800"> |
| 64 | + {t("extensions.kg_planning.not_configured_title")} |
| 65 | + </h2> |
| 66 | + <p className="max-w-md text-sm text-slate-600"> |
| 67 | + {t("extensions.kg_planning.not_configured_body")} |
| 68 | + </p> |
| 69 | + {missingFields.length > 0 && ( |
| 70 | + <code className="px-3 py-1.5 rounded bg-slate-100 text-xs text-slate-700"> |
| 71 | + {missingFields.join(", ")} |
| 72 | + </code> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + if (status === "error") { |
| 79 | + return ( |
| 80 | + <div className="m-6 p-4 bg-red-50 border border-red-200 rounded-lg"> |
| 81 | + <p className="text-sm font-medium text-red-700">{t("extensions.kg_planning.load_failed")}</p> |
| 82 | + <p className="mt-1 text-xs text-red-600 break-words">{errorMessage}</p> |
| 83 | + <button |
| 84 | + onClick={() => void reload()} |
| 85 | + className="mt-3 inline-flex items-center gap-1.5 px-3 py-1.5 bg-white border border-red-300 text-red-700 rounded-lg text-xs font-medium hover:bg-red-100 cursor-pointer" |
| 86 | + > |
| 87 | + <RefreshCw size={12} /> {t("webmail.retry")} |
| 88 | + </button> |
| 89 | + </div> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="flex flex-col h-full bg-slate-50"> |
| 95 | + <FilterBar /> |
| 96 | + <EmployeeInfoBar /> |
| 97 | + {hydration && hydration.loaded < hydration.total && ( |
| 98 | + <div className="relative h-1 bg-slate-200 overflow-hidden" title={`${hydration.loaded} / ${hydration.total}`}> |
| 99 | + <div |
| 100 | + className="absolute inset-y-0 left-0 bg-teal-500 transition-[width] duration-200 ease-out" |
| 101 | + style={{ width: `${Math.round((hydration.loaded / hydration.total) * 100)}%` }} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + )} |
| 105 | + <div className="flex-1 overflow-auto"> |
| 106 | + {activeView === "grid" && <GridView />} |
| 107 | + {activeView === "gantt" && <GanttView />} |
| 108 | + {activeView === "team" && <TeamView />} |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments