MaintenancePage.tsx 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. import { useState, useMemo } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  4. import {
  5. Wrench,
  6. Loader2,
  7. Check,
  8. AlertTriangle,
  9. Clock,
  10. Plus,
  11. Trash2,
  12. ChevronDown,
  13. ChevronUp,
  14. Droplet,
  15. Flame,
  16. Ruler,
  17. Sparkles,
  18. Square,
  19. Cable,
  20. Edit3,
  21. RotateCcw,
  22. Calendar,
  23. Timer,
  24. Cog,
  25. Fan,
  26. Zap,
  27. Wind,
  28. Thermometer,
  29. Layers,
  30. Box,
  31. Target,
  32. RefreshCw,
  33. Settings,
  34. Filter,
  35. CircleDot,
  36. Printer,
  37. ExternalLink,
  38. } from 'lucide-react';
  39. import { api } from '../api/client';
  40. import type { MaintenanceStatus, PrinterMaintenanceOverview, MaintenanceType, Permission } from '../api/client';
  41. import { Card, CardContent } from '../components/Card';
  42. import { Button } from '../components/Button';
  43. import { Toggle } from '../components/Toggle';
  44. import { ConfirmModal } from '../components/ConfirmModal';
  45. import { useToast } from '../contexts/ToastContext';
  46. import { useAuth } from '../contexts/AuthContext';
  47. // Icon mapping for maintenance types
  48. const iconMap: Record<string, React.ComponentType<{ className?: string }>> = {
  49. Droplet,
  50. Flame,
  51. Ruler,
  52. Sparkles,
  53. Square,
  54. Cable,
  55. Wrench,
  56. Calendar,
  57. Timer,
  58. Cog,
  59. Fan,
  60. Zap,
  61. Wind,
  62. Thermometer,
  63. Layers,
  64. Box,
  65. Target,
  66. RefreshCw,
  67. Settings,
  68. Filter,
  69. CircleDot,
  70. };
  71. function getIcon(iconName: string | null) {
  72. if (!iconName) return Wrench;
  73. return iconMap[iconName] || Wrench;
  74. }
  75. type TFunction = (key: string, options?: Record<string, unknown>) => string;
  76. function formatDuration(value: number, type: 'hours' | 'days', t?: TFunction): string {
  77. if (type === 'days') {
  78. if (value < 1) return t ? t('common.today') : 'Today';
  79. if (value === 1) return t ? t('maintenance.day') : '1 day';
  80. if (value < 7) {
  81. const days = Math.round(value);
  82. return t ? t('maintenance.days', { count: days }) : `${days} days`;
  83. }
  84. // Show weeks for anything under 6 months for better precision
  85. if (value < 180) {
  86. const weeks = Math.round(value / 7);
  87. if (weeks === 1) return t ? t('maintenance.week') : '1 week';
  88. return t ? t('maintenance.weeks', { count: weeks }) : `${weeks} weeks`;
  89. }
  90. // 6+ months show as months
  91. const months = Math.round(value / 30);
  92. if (months === 1) return t ? t('maintenance.month') : '1 month';
  93. return t ? t('maintenance.months', { count: months }) : `${months} months`;
  94. } else {
  95. // Print hours - convert to readable units
  96. if (value < 1) return `${Math.round(value * 60)}m`;
  97. if (value < 24) return `${value < 10 ? value.toFixed(1) : Math.round(value)}h`;
  98. // 24+ hours: show as days of print time
  99. const days = value / 24;
  100. if (days < 7) return `${days < 2 ? days.toFixed(1) : Math.round(days)}d`;
  101. // 7+ days: show as weeks of print time
  102. const weeks = days / 7;
  103. if (weeks < 12) return `${weeks < 2 ? weeks.toFixed(1) : Math.round(weeks)}w`;
  104. // 12+ weeks: show as months of print time
  105. return `${Math.round(weeks / 4)}mo`;
  106. }
  107. }
  108. function formatIntervalLabel(value: number, type: 'hours' | 'days', t?: TFunction): string {
  109. if (type === 'days') {
  110. if (value === 1) return t ? t('maintenance.day') : '1 day';
  111. if (value === 7) return t ? t('maintenance.week') : '1 week';
  112. if (value === 14) return t ? t('maintenance.weeks', { count: 2 }) : '2 weeks';
  113. if (value === 30) return t ? t('maintenance.month') : '1 month';
  114. if (value === 60) return t ? t('maintenance.months', { count: 2 }) : '2 months';
  115. if (value === 90) return t ? t('maintenance.months', { count: 3 }) : '3 months';
  116. if (value === 180) return t ? t('maintenance.months', { count: 6 }) : '6 months';
  117. if (value === 365) return t ? t('maintenance.year') : '1 year';
  118. return t ? t('maintenance.days', { count: value }) : `${value} days`;
  119. }
  120. return `${value}h`;
  121. }
  122. // Get Bambu Lab wiki URL for a maintenance task based on printer model
  123. function getMaintenanceWikiUrl(typeName: string, printerModel: string | null): string | null {
  124. const model = (printerModel || '').toUpperCase().replace(/[- ]/g, '');
  125. // Helper to match model families
  126. const isX1 = model.includes('X1');
  127. const isP1 = model.includes('P1');
  128. const isA1Mini = model.includes('A1MINI');
  129. const isA1 = model.includes('A1') && !isA1Mini;
  130. const isH2D = model.includes('H2D');
  131. const isH2C = model.includes('H2C');
  132. const isH2S = model.includes('H2S');
  133. const isH2 = isH2D || isH2C || isH2S;
  134. const isP2S = model.includes('P2S');
  135. switch (typeName) {
  136. case 'Lubricate Carbon Rods':
  137. // X1, P1 series have carbon rods
  138. if (isX1) return 'https://wiki.bambulab.com/en/x1/maintenance/basic-maintenance';
  139. if (isP1) return 'https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance';
  140. return null;
  141. case 'Lubricate Steel Rods':
  142. // P2S has hardened steel rods
  143. if (isP2S) return 'https://wiki.bambulab.com/en/p2s/maintenance/lubricate-x-y-z-axis';
  144. return null;
  145. case 'Clean Steel Rods':
  146. // P2S has hardened steel rods
  147. if (isP2S) return 'https://wiki.bambulab.com/en/p2s/maintenance/lubricate-x-y-z-axis';
  148. return null;
  149. case 'Lubricate Linear Rails':
  150. // A1 and H2 series have linear rails
  151. if (isA1Mini) return 'https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis';
  152. if (isA1) return 'https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis';
  153. if (isH2) return 'https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication';
  154. return null;
  155. case 'Clean Nozzle/Hotend':
  156. if (isX1 || isP1) return 'https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog';
  157. if (isA1Mini || isA1) return 'https://wiki.bambulab.com/en/a1-mini/troubleshooting/nozzle-clog';
  158. if (isH2) return 'https://wiki.bambulab.com/en/h2/maintenance/nozzl-cold-pull-maintenance-and-cleaning';
  159. if (isP2S) return 'https://wiki.bambulab.com/en/p2s/maintenance/cold-pull-maintenance-hotend';
  160. return 'https://wiki.bambulab.com/en/x1/troubleshooting/nozzle-clog';
  161. case 'Check Belt Tension':
  162. if (isX1) return 'https://wiki.bambulab.com/en/x1/maintenance/belt-tension';
  163. if (isP1) return 'https://wiki.bambulab.com/en/p1/maintenance/p1p-maintenance';
  164. if (isA1Mini) return 'https://wiki.bambulab.com/en/a1-mini/maintenance/belt_tension';
  165. if (isA1) return 'https://wiki.bambulab.com/en/a1/maintenance/belt_tension';
  166. if (isH2D) return 'https://wiki.bambulab.com/en/h2/maintenance/belt-tension';
  167. if (isH2C) return 'https://wiki.bambulab.com/en/h2c/maintenance/belt-tension';
  168. if (isH2S) return 'https://wiki.bambulab.com/en/h2s/maintenance/belt-tension';
  169. if (isP2S) return 'https://wiki.bambulab.com/en/p2s/maintenance/belt-tension';
  170. return 'https://wiki.bambulab.com/en/x1/maintenance/belt-tension';
  171. case 'Clean Carbon Rods':
  172. // X1, P1 series have carbon rods
  173. if (isX1 || isP1) return 'https://wiki.bambulab.com/en/general/carbon-rods-clearance';
  174. return null;
  175. case 'Clean Linear Rails':
  176. // A1 and H2 series have linear rails
  177. if (isA1Mini) return 'https://wiki.bambulab.com/en/a1-mini/maintenance/lubricate-y-axis';
  178. if (isA1) return 'https://wiki.bambulab.com/en/a1/maintenance/lubricate-y-axis';
  179. if (isH2) return 'https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication';
  180. return null;
  181. case 'Clean Build Plate':
  182. // Same for all printers
  183. return 'https://wiki.bambulab.com/en/filament-acc/acc/pei-plate-clean-guide';
  184. case 'Check PTFE Tube':
  185. if (isX1 || isP1) return 'https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube';
  186. if (isA1Mini || isA1) return 'https://wiki.bambulab.com/en/a1-mini/maintenance/ptfe-tube';
  187. if (isH2D) return 'https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer';
  188. if (isH2S) return 'https://wiki.bambulab.com/en/h2s/maintenance/replace-ptfe-tube-on-h2s-printer';
  189. if (isH2C) return 'https://wiki.bambulab.com/en/h2/maintenance/replace-ptfe-tube-on-h2d-printer'; // H2C uses H2D guide
  190. if (isP2S) return 'https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube'; // P2S uses similar PTFE
  191. return 'https://wiki.bambulab.com/en/x1/maintenance/replace-ptfe-tube';
  192. case 'Replace HEPA Filter':
  193. case 'HEPA Filter':
  194. case 'Replace Carbon Filter':
  195. case 'Carbon Filter':
  196. if (isH2) return 'https://wiki.bambulab.com/en/h2/maintenance/replace-smoke-purifier-air-filte';
  197. // X1/P1 use the activated carbon filter
  198. return 'https://wiki.bambulab.com/en/x1/maintenance/replace-carbon-filter';
  199. case 'Lubricate Left Nozzle Rail':
  200. case 'Left Nozzle Rail':
  201. // H2 series specific - dual nozzle system
  202. if (isH2) return 'https://wiki.bambulab.com/en/h2/maintenance/x-axis-lubrication';
  203. return null;
  204. default:
  205. // Custom maintenance types don't have wiki URLs
  206. return null;
  207. }
  208. }
  209. // Maintenance item card - cleaner, more visual design
  210. function MaintenanceCard({
  211. item,
  212. onPerform,
  213. onToggle,
  214. hasPermission,
  215. t,
  216. }: {
  217. item: MaintenanceStatus;
  218. onPerform: (id: number) => void;
  219. onToggle: (id: number, enabled: boolean) => void;
  220. hasPermission: (permission: Permission) => boolean;
  221. t: TFunction;
  222. }) {
  223. const Icon = getIcon(item.maintenance_type_icon);
  224. const intervalType = item.interval_type || 'hours';
  225. // Calculate progress based on interval type
  226. const getProgress = () => {
  227. if (intervalType === 'days') {
  228. const daysSince = item.days_since_maintenance ?? 0;
  229. return Math.max(0, Math.min(100, (daysSince / item.interval_hours) * 100));
  230. }
  231. return Math.max(0, Math.min(100,
  232. ((item.interval_hours - item.hours_until_due) / item.interval_hours) * 100
  233. ));
  234. };
  235. const progressPercent = getProgress();
  236. const getStatusColor = () => {
  237. if (!item.enabled) return 'text-bambu-gray';
  238. if (item.is_due) return 'text-red-400';
  239. if (item.is_warning) return 'text-amber-400';
  240. return 'text-bambu-green';
  241. };
  242. const getProgressColor = () => {
  243. if (!item.enabled) return 'bg-bambu-gray/30';
  244. if (item.is_due) return 'bg-red-500';
  245. if (item.is_warning) return 'bg-amber-500';
  246. return 'bg-bambu-green';
  247. };
  248. const getBgColor = () => {
  249. if (!item.enabled) return 'bg-bambu-dark-secondary/50';
  250. if (item.is_due) return 'bg-red-500/5 border-red-500/20';
  251. if (item.is_warning) return 'bg-amber-500/5 border-amber-500/20';
  252. return 'bg-bambu-dark-secondary border-bambu-dark-tertiary';
  253. };
  254. const getStatusText = () => {
  255. if (!item.enabled) return t('common.disabled');
  256. if (intervalType === 'days') {
  257. const daysUntil = item.days_until_due ?? 0;
  258. if (item.is_due) return t('maintenance.overdueBy', { duration: formatDuration(Math.abs(daysUntil), 'days', t) });
  259. if (item.is_warning) return t('maintenance.dueIn', { duration: formatDuration(daysUntil, 'days', t) });
  260. return t('maintenance.timeLeft', { duration: formatDuration(daysUntil, 'days', t) });
  261. } else {
  262. if (item.is_due) return t('maintenance.overdueBy', { duration: formatDuration(Math.abs(item.hours_until_due), 'hours', t) });
  263. if (item.is_warning) return t('maintenance.dueIn', { duration: formatDuration(item.hours_until_due, 'hours', t) });
  264. return t('maintenance.timeLeft', { duration: formatDuration(item.hours_until_due, 'hours', t) });
  265. }
  266. };
  267. return (
  268. <div className={`rounded-xl border p-4 transition-all ${getBgColor()}`}>
  269. <div className="flex items-start gap-3 max-[550px]:flex-wrap">
  270. {/* Icon with status indicator */}
  271. <div className={`relative p-2.5 rounded-lg shrink-0 ${
  272. item.is_due ? 'bg-red-500/20' :
  273. item.is_warning ? 'bg-amber-500/20' :
  274. item.enabled ? 'bg-bambu-dark' : 'bg-bambu-dark/50'
  275. }`}>
  276. <Icon className={`w-5 h-5 ${getStatusColor()}`} />
  277. {item.enabled && (item.is_due || item.is_warning) && (
  278. <span className={`absolute -top-1 -right-1 w-2.5 h-2.5 rounded-full ${
  279. item.is_due ? 'bg-red-500' : 'bg-amber-500'
  280. } animate-pulse`} />
  281. )}
  282. </div>
  283. {/* Content */}
  284. <div className="flex-1 min-w-0">
  285. <div className="flex items-center gap-2">
  286. <h3 className={`font-medium truncate ${item.enabled ? 'text-white' : 'text-bambu-gray'}`}>
  287. {item.maintenance_type_name}
  288. </h3>
  289. {intervalType === 'days' && (
  290. <span title={t('maintenance.timeBasedInterval')}>
  291. <Calendar className="w-3.5 h-3.5 text-bambu-gray shrink-0" />
  292. </span>
  293. )}
  294. {/* Wiki link - next to name */}
  295. {(() => {
  296. // Use custom wiki_url from type if available, otherwise use computed URL
  297. const wikiUrl = item.maintenance_type_wiki_url || getMaintenanceWikiUrl(item.maintenance_type_name, item.printer_model);
  298. return wikiUrl ? (
  299. <a
  300. href={wikiUrl}
  301. target="_blank"
  302. rel="noopener noreferrer"
  303. className="text-bambu-gray hover:text-bambu-green transition-colors shrink-0"
  304. title={t('maintenance.viewDocumentation')}
  305. onClick={(e) => e.stopPropagation()}
  306. >
  307. <ExternalLink className="w-3.5 h-3.5" />
  308. </a>
  309. ) : null;
  310. })()}
  311. </div>
  312. {/* Progress bar */}
  313. <div className="mt-2 mb-1.5">
  314. <div className="w-full h-1.5 bg-bambu-dark rounded-full overflow-hidden">
  315. <div
  316. className={`h-full rounded-full transition-all duration-500 ${getProgressColor()}`}
  317. style={{ width: `${progressPercent}%` }}
  318. />
  319. </div>
  320. </div>
  321. {/* Status text */}
  322. <div className={`text-xs flex items-center gap-1 ${getStatusColor()}`}>
  323. {item.is_due && <AlertTriangle className="w-3 h-3" />}
  324. {item.is_warning && !item.is_due && <Clock className="w-3 h-3" />}
  325. {!item.is_due && !item.is_warning && item.enabled && <Check className="w-3 h-3" />}
  326. {getStatusText()}
  327. </div>
  328. </div>
  329. {/* Actions */}
  330. <div className="flex items-center gap-2 shrink-0 max-[550px]:w-full max-[550px]:justify-end max-[550px]:mt-1">
  331. <span title={!hasPermission('maintenance:update') ? t('maintenance.noPermissionUpdate') : undefined}>
  332. <Toggle
  333. checked={item.enabled}
  334. onChange={(checked) => onToggle(item.id, checked)}
  335. disabled={!hasPermission('maintenance:update')}
  336. />
  337. </span>
  338. <Button
  339. size="sm"
  340. variant={item.is_due ? 'primary' : 'secondary'}
  341. onClick={() => onPerform(item.id)}
  342. disabled={!item.enabled || !hasPermission('maintenance:update')}
  343. title={!hasPermission('maintenance:update') ? t('maintenance.noPermissionPerform') : undefined}
  344. className="!px-3"
  345. >
  346. <RotateCcw className="w-3.5 h-3.5" />
  347. {t('common.reset')}
  348. </Button>
  349. </div>
  350. </div>
  351. </div>
  352. );
  353. }
  354. // Printer section with improved visual hierarchy
  355. function PrinterSection({
  356. overview,
  357. onPerform,
  358. onToggle,
  359. onSetHours,
  360. hasPermission,
  361. t,
  362. }: {
  363. overview: PrinterMaintenanceOverview;
  364. onPerform: (id: number) => void;
  365. onToggle: (id: number, enabled: boolean) => void;
  366. onSetHours: (printerId: number, hours: number) => void;
  367. hasPermission: (permission: Permission) => boolean;
  368. t: TFunction;
  369. }) {
  370. const [expanded, setExpanded] = useState(false);
  371. const [editingHours, setEditingHours] = useState(false);
  372. const [hoursInput, setHoursInput] = useState(overview.total_print_hours.toFixed(1));
  373. const sortedItems = [...overview.maintenance_items].sort((a, b) => {
  374. // Sort by urgency first, then by type
  375. if (a.is_due && !b.is_due) return -1;
  376. if (!a.is_due && b.is_due) return 1;
  377. if (a.is_warning && !b.is_warning) return -1;
  378. if (!a.is_warning && b.is_warning) return 1;
  379. return a.maintenance_type_id - b.maintenance_type_id;
  380. });
  381. const nextTask = sortedItems.find(item => item.enabled && (item.is_due || item.is_warning));
  382. const handleSaveHours = () => {
  383. const hours = parseFloat(hoursInput);
  384. if (!isNaN(hours) && hours >= 0) {
  385. onSetHours(overview.printer_id, hours);
  386. setEditingHours(false);
  387. }
  388. };
  389. return (
  390. <Card className="overflow-hidden">
  391. {/* Header */}
  392. <div className="p-5">
  393. <div className="flex items-center justify-between">
  394. <div className="flex items-center gap-4">
  395. <h2 className="text-xl font-semibold text-white">{overview.printer_name}</h2>
  396. <div className="flex items-center gap-2">
  397. {overview.due_count > 0 && (
  398. <span className="px-2.5 py-1 bg-red-500/20 text-red-400 text-xs font-medium rounded-full flex items-center gap-1.5">
  399. <AlertTriangle className="w-3 h-3" />
  400. {t('maintenance.overdueCount', { count: overview.due_count })}
  401. </span>
  402. )}
  403. {overview.warning_count > 0 && (
  404. <span className="px-2.5 py-1 bg-amber-500/20 text-amber-400 text-xs font-medium rounded-full flex items-center gap-1.5">
  405. <Clock className="w-3 h-3" />
  406. {t('maintenance.dueSoonCount', { count: overview.warning_count })}
  407. </span>
  408. )}
  409. {overview.due_count === 0 && overview.warning_count === 0 && (
  410. <span className="px-2.5 py-1 bg-bambu-green/20 text-bambu-green text-xs font-medium rounded-full flex items-center gap-1.5">
  411. <Check className="w-3 h-3" />
  412. {t('maintenance.allGood')}
  413. </span>
  414. )}
  415. </div>
  416. </div>
  417. <button
  418. onClick={() => setExpanded(!expanded)}
  419. className="flex items-center gap-1.5 px-3 py-1.5 text-sm text-bambu-gray hover:text-white hover:bg-bambu-dark rounded-lg transition-colors"
  420. >
  421. {expanded ? <ChevronUp className="w-4 h-4" /> : <ChevronDown className="w-4 h-4" />}
  422. {expanded ? t('common.collapse') : t('common.expand')}
  423. </button>
  424. </div>
  425. {/* Quick stats row */}
  426. <div className="flex items-center gap-6 mt-4">
  427. {/* Print Hours */}
  428. <div className="flex items-center gap-3">
  429. <div className="p-2 bg-bambu-dark/50 rounded-lg">
  430. <Timer className="w-4 h-4 text-bambu-gray" />
  431. </div>
  432. {editingHours ? (
  433. <div className="flex items-center gap-2">
  434. <input
  435. type="number"
  436. value={hoursInput}
  437. onChange={(e) => setHoursInput(e.target.value)}
  438. onKeyDown={(e) => {
  439. if (e.key === 'Enter') handleSaveHours();
  440. if (e.key === 'Escape') setEditingHours(false);
  441. }}
  442. className="w-24 px-2 py-1 bg-bambu-dark border border-bambu-dark-tertiary rounded text-white text-sm"
  443. min="0"
  444. step="1"
  445. autoFocus
  446. />
  447. <span className="text-xs text-bambu-gray">{t('common.hours')}</span>
  448. <Button size="sm" onClick={handleSaveHours}>{t('common.save')}</Button>
  449. <Button size="sm" variant="secondary" onClick={() => setEditingHours(false)}>{t('common.cancel')}</Button>
  450. </div>
  451. ) : (
  452. <button
  453. onClick={() => {
  454. if (!hasPermission('maintenance:update')) return;
  455. setHoursInput(Math.round(overview.total_print_hours).toString());
  456. setEditingHours(true);
  457. }}
  458. className={`group ${!hasPermission('maintenance:update') ? 'cursor-not-allowed opacity-60' : ''}`}
  459. title={!hasPermission('maintenance:update') ? t('maintenance.noPermissionEditHours') : undefined}
  460. >
  461. <div className={`text-sm font-medium text-white ${hasPermission('maintenance:update') ? 'group-hover:text-bambu-green' : ''} transition-colors flex items-center gap-1`}>
  462. {Math.round(overview.total_print_hours)} {t('common.hours')}
  463. <Edit3 className={`w-3 h-3 text-bambu-gray ${hasPermission('maintenance:update') ? 'group-hover:text-bambu-green' : ''}`} />
  464. </div>
  465. <div className="text-xs text-bambu-gray">{t('maintenance.totalPrintTime')}</div>
  466. </button>
  467. )}
  468. </div>
  469. {/* Divider */}
  470. <div className="w-px h-10 bg-bambu-dark-tertiary" />
  471. {/* Next Maintenance */}
  472. {nextTask && (
  473. <div className="flex items-center gap-3">
  474. <div className={`p-2 rounded-lg ${
  475. nextTask.is_due ? 'bg-red-500/20' : 'bg-amber-500/20'
  476. }`}>
  477. {(() => {
  478. const Icon = getIcon(nextTask.maintenance_type_icon);
  479. return <Icon className={`w-4 h-4 ${nextTask.is_due ? 'text-red-400' : 'text-amber-400'}`} />;
  480. })()}
  481. </div>
  482. <div>
  483. <div className={`text-sm font-medium ${nextTask.is_due ? 'text-red-400' : 'text-amber-400'}`}>
  484. {nextTask.maintenance_type_name}
  485. </div>
  486. <div className={`text-xs ${nextTask.is_due ? 'text-red-400/70' : 'text-amber-400/70'}`}>
  487. {nextTask.is_due ? t('common.overdue') : t('maintenance.dueSoon')}
  488. </div>
  489. </div>
  490. </div>
  491. )}
  492. </div>
  493. </div>
  494. {/* Maintenance items */}
  495. {expanded && (
  496. <CardContent className="pt-0 border-t border-bambu-dark-tertiary">
  497. <div className="grid grid-cols-1 lg:grid-cols-2 gap-3 pt-4">
  498. {sortedItems.map((item) => (
  499. <MaintenanceCard
  500. key={item.id}
  501. item={item}
  502. onPerform={onPerform}
  503. onToggle={onToggle}
  504. hasPermission={hasPermission}
  505. t={t}
  506. />
  507. ))}
  508. </div>
  509. </CardContent>
  510. )}
  511. </Card>
  512. );
  513. }
  514. // Settings section - maintenance types configuration
  515. function SettingsSection({
  516. overview,
  517. types,
  518. onUpdateInterval,
  519. onAddType,
  520. onUpdateType,
  521. onDeleteType,
  522. onRestoreDefaults,
  523. isRestoringDefaults,
  524. onAssignType,
  525. onRemoveItem,
  526. hasPermission,
  527. t,
  528. }: {
  529. overview: PrinterMaintenanceOverview[] | undefined;
  530. types: MaintenanceType[];
  531. onUpdateInterval: (id: number, data: { custom_interval_hours?: number | null; custom_interval_type?: 'hours' | 'days' | null }) => void;
  532. onAddType: (data: { name: string; description?: string; default_interval_hours: number; interval_type: 'hours' | 'days'; icon?: string; wiki_url?: string | null }, printerIds: number[]) => void;
  533. onUpdateType: (id: number, data: { name?: string; default_interval_hours?: number; interval_type?: 'hours' | 'days'; icon?: string; wiki_url?: string | null }) => void;
  534. onDeleteType: (id: number) => void;
  535. onRestoreDefaults: () => void;
  536. isRestoringDefaults: boolean;
  537. onAssignType: (printerId: number, typeId: number) => void;
  538. onRemoveItem: (itemId: number) => void;
  539. hasPermission: (permission: Permission) => boolean;
  540. t: TFunction;
  541. }) {
  542. const [editingInterval, setEditingInterval] = useState<number | null>(null);
  543. const [intervalInput, setIntervalInput] = useState('');
  544. const [intervalTypeInput, setIntervalTypeInput] = useState<'hours' | 'days'>('hours');
  545. const [showAddType, setShowAddType] = useState(false);
  546. const [newTypeName, setNewTypeName] = useState('');
  547. const [newTypeInterval, setNewTypeInterval] = useState('100');
  548. const [newTypeIntervalType, setNewTypeIntervalType] = useState<'hours' | 'days'>('hours');
  549. const [newTypeIcon, setNewTypeIcon] = useState('Wrench');
  550. const [newTypeWikiUrl, setNewTypeWikiUrl] = useState('');
  551. const [selectedPrinters, setSelectedPrinters] = useState<Set<number>>(new Set());
  552. const [expandedType, setExpandedType] = useState<number | null>(null);
  553. const [pendingSystemDelete, setPendingSystemDelete] = useState<MaintenanceType | null>(null);
  554. // Get unique printers from overview
  555. const printers = useMemo(() => {
  556. if (!overview) return [];
  557. return overview.map(o => ({ id: o.printer_id, name: o.printer_name }));
  558. }, [overview]);
  559. // Get which printers have a specific maintenance type assigned
  560. const getAssignedPrinters = (typeId: number) => {
  561. if (!overview) return [];
  562. return overview
  563. .filter(p => p.maintenance_items.some(item => item.maintenance_type_id === typeId))
  564. .map(p => ({
  565. printerId: p.printer_id,
  566. printerName: p.printer_name,
  567. itemId: p.maintenance_items.find(item => item.maintenance_type_id === typeId)?.id,
  568. }));
  569. };
  570. // Get printers that DON'T have a specific type assigned
  571. const getUnassignedPrinters = (typeId: number) => {
  572. if (!overview) return [];
  573. const assignedIds = new Set(getAssignedPrinters(typeId).map(p => p.printerId));
  574. return printers.filter(p => !assignedIds.has(p.id));
  575. };
  576. // Edit type state
  577. const [editingType, setEditingType] = useState<MaintenanceType | null>(null);
  578. const [editTypeName, setEditTypeName] = useState('');
  579. const [editTypeInterval, setEditTypeInterval] = useState('');
  580. const [editTypeIntervalType, setEditTypeIntervalType] = useState<'hours' | 'days'>('hours');
  581. const [editTypeIcon, setEditTypeIcon] = useState('Wrench');
  582. const [editTypeWikiUrl, setEditTypeWikiUrl] = useState('');
  583. const startEditType = (type: MaintenanceType) => {
  584. setEditingType(type);
  585. setEditTypeName(type.name);
  586. setEditTypeInterval(type.default_interval_hours.toString());
  587. setEditTypeIntervalType(type.interval_type || 'hours');
  588. setEditTypeIcon(type.icon || 'Wrench');
  589. setEditTypeWikiUrl(type.wiki_url || '');
  590. };
  591. const handleSaveEditType = () => {
  592. if (editingType && editTypeName.trim() && parseFloat(editTypeInterval) > 0) {
  593. onUpdateType(editingType.id, {
  594. name: editTypeName.trim(),
  595. default_interval_hours: parseFloat(editTypeInterval),
  596. interval_type: editTypeIntervalType,
  597. icon: editTypeIcon,
  598. wiki_url: editTypeWikiUrl.trim() || null,
  599. });
  600. setEditingType(null);
  601. }
  602. };
  603. const handleSaveInterval = (itemId: number, defaultInterval: number, defaultIntervalType: 'hours' | 'days') => {
  604. const newInterval = parseFloat(intervalInput);
  605. if (!isNaN(newInterval) && newInterval > 0) {
  606. const customInterval = Math.abs(newInterval - defaultInterval) < 0.01 ? null : newInterval;
  607. const customIntervalType = intervalTypeInput !== defaultIntervalType ? intervalTypeInput : null;
  608. onUpdateInterval(itemId, {
  609. custom_interval_hours: customInterval,
  610. custom_interval_type: customIntervalType
  611. });
  612. }
  613. setEditingInterval(null);
  614. };
  615. const handleAddType = (e: React.FormEvent) => {
  616. e.preventDefault();
  617. if (newTypeName.trim() && parseFloat(newTypeInterval) > 0 && selectedPrinters.size > 0) {
  618. onAddType({
  619. name: newTypeName.trim(),
  620. default_interval_hours: parseFloat(newTypeInterval),
  621. interval_type: newTypeIntervalType,
  622. icon: newTypeIcon,
  623. wiki_url: newTypeWikiUrl.trim() || null,
  624. }, Array.from(selectedPrinters));
  625. setNewTypeName('');
  626. setNewTypeInterval('100');
  627. setNewTypeIntervalType('hours');
  628. setNewTypeWikiUrl('');
  629. setSelectedPrinters(new Set());
  630. setShowAddType(false);
  631. }
  632. };
  633. const togglePrinterSelection = (printerId: number) => {
  634. setSelectedPrinters(prev => {
  635. const next = new Set(prev);
  636. if (next.has(printerId)) {
  637. next.delete(printerId);
  638. } else {
  639. next.add(printerId);
  640. }
  641. return next;
  642. });
  643. };
  644. const printerItems = overview?.map(p => ({
  645. printerId: p.printer_id,
  646. printerName: p.printer_name,
  647. items: p.maintenance_items.sort((a, b) => a.maintenance_type_id - b.maintenance_type_id),
  648. })).sort((a, b) => a.printerName.localeCompare(b.printerName)) || [];
  649. const systemTypes = types.filter(t => t.is_system);
  650. const customTypes = types.filter(t => !t.is_system);
  651. return (
  652. <div className="space-y-8">
  653. {/* Maintenance Types */}
  654. <div>
  655. <div className="flex items-center justify-between mb-4">
  656. <div>
  657. <h2 className="text-lg font-semibold text-white">{t('maintenance.maintenanceTypes')}</h2>
  658. <p className="text-sm text-bambu-gray mt-1">{t('maintenance.maintenanceTypesDescription')}</p>
  659. </div>
  660. <div className="flex items-center gap-2">
  661. <Button
  662. variant="secondary"
  663. onClick={onRestoreDefaults}
  664. disabled={!hasPermission('maintenance:delete') || isRestoringDefaults}
  665. title={!hasPermission('maintenance:delete') ? t('maintenance.noPermissionDeleteTypes') : undefined}
  666. >
  667. {t('maintenance.restoreDefaults')}
  668. </Button>
  669. <Button
  670. onClick={() => setShowAddType(!showAddType)}
  671. disabled={!hasPermission('maintenance:create')}
  672. title={!hasPermission('maintenance:create') ? t('maintenance.noPermissionEditTypes') : undefined}
  673. >
  674. <Plus className="w-4 h-4" />
  675. {t('maintenance.addCustomType')}
  676. </Button>
  677. </div>
  678. </div>
  679. {/* Add custom type form */}
  680. {showAddType && (
  681. <Card className="mb-6">
  682. <CardContent className="py-4">
  683. <form onSubmit={handleAddType}>
  684. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
  685. <div className="lg:col-span-2">
  686. <label className="block text-xs text-bambu-gray mb-1.5">{t('common.name')}</label>
  687. <input
  688. type="text"
  689. value={newTypeName}
  690. onChange={(e) => setNewTypeName(e.target.value)}
  691. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  692. placeholder={t('maintenance.exampleName')}
  693. autoFocus
  694. />
  695. </div>
  696. <div>
  697. <label className="block text-xs text-bambu-gray mb-1.5">{t('maintenance.intervalType')}</label>
  698. <select
  699. value={newTypeIntervalType}
  700. onChange={(e) => {
  701. setNewTypeIntervalType(e.target.value as 'hours' | 'days');
  702. // Set sensible default based on type
  703. if (e.target.value === 'days') {
  704. setNewTypeInterval('30');
  705. } else {
  706. setNewTypeInterval('100');
  707. }
  708. }}
  709. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  710. >
  711. <option value="hours">{t('maintenance.printHours')}</option>
  712. <option value="days">{t('maintenance.calendarDays')}</option>
  713. </select>
  714. </div>
  715. <div>
  716. <label className="block text-xs text-bambu-gray mb-1.5">
  717. {t('maintenance.intervalValue', { type: newTypeIntervalType === 'days' ? t('maintenance.calendarDays').toLowerCase() : t('common.hours') })}
  718. </label>
  719. <input
  720. type="number"
  721. value={newTypeInterval}
  722. onChange={(e) => setNewTypeInterval(e.target.value)}
  723. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  724. min="1"
  725. />
  726. </div>
  727. </div>
  728. <div className="mt-4 flex items-end justify-between">
  729. <div>
  730. <label className="block text-xs text-bambu-gray mb-1.5">{t('maintenance.icon')}</label>
  731. <div className="flex gap-1">
  732. {Object.keys(iconMap).map((iconName) => {
  733. const IconComp = iconMap[iconName];
  734. return (
  735. <button
  736. key={iconName}
  737. type="button"
  738. onClick={() => setNewTypeIcon(iconName)}
  739. className={`p-2 rounded-lg transition-colors ${
  740. newTypeIcon === iconName
  741. ? 'bg-bambu-green text-white'
  742. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  743. }`}
  744. >
  745. <IconComp className="w-4 h-4" />
  746. </button>
  747. );
  748. })}
  749. </div>
  750. </div>
  751. </div>
  752. {/* Wiki URL */}
  753. <div className="mt-4">
  754. <label className="block text-xs text-bambu-gray mb-1.5">{t('maintenance.documentationLink')}</label>
  755. <input
  756. type="url"
  757. value={newTypeWikiUrl}
  758. onChange={(e) => setNewTypeWikiUrl(e.target.value)}
  759. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  760. placeholder="https://wiki.bambulab.com/..."
  761. />
  762. </div>
  763. {/* Printer selection */}
  764. <div className="mt-4">
  765. <label className="block text-xs text-bambu-gray mb-1.5">{t('maintenance.assignToPrinters')}</label>
  766. <div className="flex flex-wrap gap-2">
  767. {printers.map(p => (
  768. <button
  769. key={p.id}
  770. type="button"
  771. onClick={() => togglePrinterSelection(p.id)}
  772. className={`px-3 py-1.5 rounded-lg text-sm transition-colors ${
  773. selectedPrinters.has(p.id)
  774. ? 'bg-bambu-green text-white'
  775. : 'bg-bambu-dark text-bambu-gray hover:text-white hover:bg-bambu-dark-tertiary'
  776. }`}
  777. >
  778. {p.name}
  779. </button>
  780. ))}
  781. </div>
  782. {selectedPrinters.size === 0 && (
  783. <p className="text-xs text-orange-400 mt-1">{t('maintenance.selectAtLeastOnePrinter')}</p>
  784. )}
  785. </div>
  786. <div className="mt-4 flex justify-end gap-2">
  787. <Button type="button" variant="secondary" onClick={() => { setShowAddType(false); setSelectedPrinters(new Set()); }}>
  788. {t('common.cancel')}
  789. </Button>
  790. <Button type="submit" disabled={!newTypeName.trim() || selectedPrinters.size === 0}>
  791. {t('maintenance.addType')}
  792. </Button>
  793. </div>
  794. </form>
  795. </CardContent>
  796. </Card>
  797. )}
  798. {/* Types grid */}
  799. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
  800. {/* System types */}
  801. {systemTypes.map((type) => {
  802. const Icon = getIcon(type.icon);
  803. const intervalType = type.interval_type || 'hours';
  804. return (
  805. <div key={type.id} className="bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-dark-tertiary">
  806. <div className="flex items-center gap-3">
  807. <div className="p-2.5 bg-bambu-dark rounded-lg">
  808. <Icon className="w-5 h-5 text-bambu-gray" />
  809. </div>
  810. <div className="flex-1 min-w-0">
  811. <div className="text-sm font-medium text-white truncate">{type.name}</div>
  812. <div className="text-xs text-bambu-gray mt-0.5 flex items-center gap-1">
  813. {intervalType === 'days' ? <Calendar className="w-3 h-3" /> : <Timer className="w-3 h-3" />}
  814. {formatIntervalLabel(type.default_interval_hours, intervalType, t)}
  815. </div>
  816. </div>
  817. <button
  818. onClick={() => {
  819. if (!hasPermission('maintenance:delete')) return;
  820. setPendingSystemDelete(type);
  821. }}
  822. disabled={!hasPermission('maintenance:delete')}
  823. title={!hasPermission('maintenance:delete') ? t('maintenance.noPermissionDeleteTypes') : undefined}
  824. className={`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${!hasPermission('maintenance:delete') ? 'opacity-50 cursor-not-allowed' : ''}`}
  825. >
  826. <Trash2 className="w-4 h-4" />
  827. </button>
  828. </div>
  829. </div>
  830. );
  831. })}
  832. {/* Custom types */}
  833. {customTypes.map((type) => {
  834. const Icon = getIcon(type.icon);
  835. const intervalType = type.interval_type || 'hours';
  836. const isEditing = editingType?.id === type.id;
  837. if (isEditing) {
  838. return (
  839. <div key={type.id} className="bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green">
  840. <div className="space-y-3">
  841. <input
  842. type="text"
  843. value={editTypeName}
  844. onChange={(e) => setEditTypeName(e.target.value)}
  845. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  846. placeholder={t('common.name')}
  847. autoFocus
  848. />
  849. <div className="flex gap-2">
  850. <select
  851. value={editTypeIntervalType}
  852. onChange={(e) => setEditTypeIntervalType(e.target.value as 'hours' | 'days')}
  853. className="flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  854. >
  855. <option value="hours">{t('maintenance.printHours')}</option>
  856. <option value="days">{t('maintenance.calendarDays')}</option>
  857. </select>
  858. <input
  859. type="number"
  860. value={editTypeInterval}
  861. onChange={(e) => setEditTypeInterval(e.target.value)}
  862. className="w-24 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  863. min="1"
  864. />
  865. </div>
  866. <div className="flex flex-wrap gap-1">
  867. {Object.keys(iconMap).map((iconName) => {
  868. const IconComp = iconMap[iconName];
  869. return (
  870. <button
  871. key={iconName}
  872. type="button"
  873. onClick={() => setEditTypeIcon(iconName)}
  874. className={`p-1.5 rounded transition-colors ${
  875. editTypeIcon === iconName
  876. ? 'bg-bambu-green text-white'
  877. : 'bg-bambu-dark text-bambu-gray hover:text-white'
  878. }`}
  879. >
  880. <IconComp className="w-3.5 h-3.5" />
  881. </button>
  882. );
  883. })}
  884. </div>
  885. <input
  886. type="url"
  887. value={editTypeWikiUrl}
  888. onChange={(e) => setEditTypeWikiUrl(e.target.value)}
  889. className="w-full px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none"
  890. placeholder={t('maintenance.documentationLink')}
  891. />
  892. <div className="flex gap-2">
  893. <Button size="sm" onClick={handleSaveEditType} disabled={!editTypeName.trim()}>
  894. {t('common.save')}
  895. </Button>
  896. <Button size="sm" variant="secondary" onClick={() => setEditingType(null)}>
  897. {t('common.cancel')}
  898. </Button>
  899. </div>
  900. </div>
  901. </div>
  902. );
  903. }
  904. const assignedPrinters = getAssignedPrinters(type.id);
  905. const unassignedPrinters = getUnassignedPrinters(type.id);
  906. const isExpanded = expandedType === type.id;
  907. return (
  908. <div key={type.id} className="bg-bambu-dark-secondary rounded-xl p-4 border border-bambu-green/30">
  909. <div className="flex items-center gap-3">
  910. <div className="p-2.5 bg-bambu-green/20 rounded-lg">
  911. <Icon className="w-5 h-5 text-bambu-green" />
  912. </div>
  913. <div className="flex-1 min-w-0">
  914. <div className="flex items-center gap-2">
  915. <span className="text-sm font-medium text-white truncate">{type.name}</span>
  916. <span className="px-1.5 py-0.5 bg-bambu-green/20 text-bambu-green text-[10px] font-medium rounded">
  917. {t('maintenance.custom')}
  918. </span>
  919. </div>
  920. <div className="text-xs text-bambu-gray mt-0.5 flex items-center gap-1">
  921. {intervalType === 'days' ? <Calendar className="w-3 h-3" /> : <Timer className="w-3 h-3" />}
  922. {formatIntervalLabel(type.default_interval_hours, intervalType, t)}
  923. </div>
  924. </div>
  925. <button
  926. onClick={() => setExpandedType(isExpanded ? null : type.id)}
  927. className={`px-2 py-1 rounded-lg border transition-colors flex items-center gap-1 ${
  928. assignedPrinters.length > 0
  929. ? 'border-bambu-green/50 bg-bambu-green/10 text-bambu-green hover:bg-bambu-green/20'
  930. : 'border-orange-400/50 bg-orange-400/10 text-orange-400 hover:bg-orange-400/20'
  931. }`}
  932. title={t('maintenance.printersAssignedClick', { count: assignedPrinters.length })}
  933. >
  934. <Printer className="w-3 h-3" />
  935. <span className="text-xs font-medium">{assignedPrinters.length}</span>
  936. <ChevronDown className={`w-3 h-3 transition-transform ${isExpanded ? 'rotate-180' : ''}`} />
  937. </button>
  938. <button
  939. onClick={() => startEditType(type)}
  940. disabled={!hasPermission('maintenance:update')}
  941. title={!hasPermission('maintenance:update') ? t('maintenance.noPermissionEditTypes') : undefined}
  942. className={`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-white transition-colors ${!hasPermission('maintenance:update') ? 'opacity-50 cursor-not-allowed' : ''}`}
  943. >
  944. <Edit3 className="w-4 h-4" />
  945. </button>
  946. <button
  947. onClick={() => {
  948. if (confirm(t('maintenance.deleteTypeConfirm', { name: type.name }))) {
  949. onDeleteType(type.id);
  950. }
  951. }}
  952. disabled={!hasPermission('maintenance:delete')}
  953. title={!hasPermission('maintenance:delete') ? t('maintenance.noPermissionDeleteTypes') : undefined}
  954. className={`p-2 rounded-lg hover:bg-bambu-dark text-bambu-gray hover:text-red-400 transition-colors ${!hasPermission('maintenance:delete') ? 'opacity-50 cursor-not-allowed' : ''}`}
  955. >
  956. <Trash2 className="w-4 h-4" />
  957. </button>
  958. </div>
  959. {/* Printer assignment management */}
  960. {isExpanded && (
  961. <div className="mt-3 pt-3 border-t border-bambu-dark-tertiary">
  962. <p className="text-xs text-bambu-gray mb-2">{t('maintenance.assignedToPrinters')}</p>
  963. {assignedPrinters.length === 0 ? (
  964. <p className="text-xs text-orange-400">{t('maintenance.noPrintersAssigned')}</p>
  965. ) : (
  966. <div className="flex flex-wrap gap-1 mb-2">
  967. {assignedPrinters.map(p => (
  968. <span
  969. key={p.printerId}
  970. className="inline-flex items-center gap-1 px-2 py-1 bg-bambu-dark rounded text-xs text-white"
  971. >
  972. {p.printerName}
  973. <button
  974. onClick={() => p.itemId && onRemoveItem(p.itemId)}
  975. disabled={!hasPermission('maintenance:delete')}
  976. title={!hasPermission('maintenance:delete') ? t('maintenance.noPermissionRemovePrinter') : t('maintenance.removeFromPrinter')}
  977. className={`ml-1 ${hasPermission('maintenance:delete') ? 'hover:text-red-400' : 'opacity-50 cursor-not-allowed'}`}
  978. >
  979. ×
  980. </button>
  981. </span>
  982. ))}
  983. </div>
  984. )}
  985. {unassignedPrinters.length > 0 && (
  986. <div className="flex flex-wrap gap-1">
  987. <span className="text-xs text-bambu-gray mr-1">{t('maintenance.addPrinterShort')}</span>
  988. {unassignedPrinters.map(p => (
  989. <button
  990. key={p.id}
  991. onClick={() => onAssignType(p.id, type.id)}
  992. disabled={!hasPermission('maintenance:create')}
  993. title={!hasPermission('maintenance:create') ? t('maintenance.noPermissionAssignPrinter') : undefined}
  994. className={`px-2 py-1 bg-bambu-dark rounded text-xs transition-colors ${hasPermission('maintenance:create') ? 'hover:bg-bambu-green/20 text-bambu-gray hover:text-bambu-green' : 'opacity-50 cursor-not-allowed text-bambu-gray'}`}
  995. >
  996. + {p.name}
  997. </button>
  998. ))}
  999. </div>
  1000. )}
  1001. </div>
  1002. )}
  1003. </div>
  1004. );
  1005. })}
  1006. </div>
  1007. </div>
  1008. {/* Per-printer interval overrides */}
  1009. {printerItems.length > 0 && (
  1010. <div>
  1011. <div className="mb-4">
  1012. <h2 className="text-lg font-semibold text-white">{t('maintenance.intervalOverrides')}</h2>
  1013. <p className="text-sm text-bambu-gray mt-1">{t('maintenance.intervalOverridesDescription')}</p>
  1014. </div>
  1015. <div className="space-y-4">
  1016. {printerItems.map((printer) => (
  1017. <Card key={printer.printerId}>
  1018. <CardContent className="py-4">
  1019. <h3 className="text-sm font-medium text-white mb-3">{printer.printerName}</h3>
  1020. <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2">
  1021. {printer.items.map((item) => {
  1022. const Icon = getIcon(item.maintenance_type_icon);
  1023. const typeInfo = types.find(t => t.id === item.maintenance_type_id);
  1024. const defaultInterval = typeInfo?.default_interval_hours || item.interval_hours;
  1025. const defaultIntervalType = typeInfo?.interval_type || 'hours';
  1026. const intervalType = item.interval_type || 'hours';
  1027. const isEditing = editingInterval === item.id;
  1028. return (
  1029. <div key={item.id} className="flex items-center gap-2 p-2.5 bg-bambu-dark rounded-lg">
  1030. <Icon className="w-4 h-4 text-bambu-gray shrink-0" />
  1031. <span className="text-xs text-bambu-gray flex-1 truncate">{item.maintenance_type_name}</span>
  1032. {isEditing ? (
  1033. <div className="flex items-center gap-1">
  1034. {intervalTypeInput === 'days' ? (
  1035. <Calendar className="w-3.5 h-3.5 text-bambu-gray shrink-0" />
  1036. ) : (
  1037. <Timer className="w-3.5 h-3.5 text-bambu-gray shrink-0" />
  1038. )}
  1039. <select
  1040. value={intervalTypeInput}
  1041. onChange={(e) => setIntervalTypeInput(e.target.value as 'hours' | 'days')}
  1042. className="px-1.5 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs"
  1043. >
  1044. <option value="hours">{t('maintenance.printHours')}</option>
  1045. <option value="days">{t('maintenance.calendarDays')}</option>
  1046. </select>
  1047. <input
  1048. type="number"
  1049. value={intervalInput}
  1050. onChange={(e) => setIntervalInput(e.target.value)}
  1051. onKeyDown={(e) => {
  1052. if (e.key === 'Enter') handleSaveInterval(item.id, defaultInterval, defaultIntervalType);
  1053. if (e.key === 'Escape') setEditingInterval(null);
  1054. }}
  1055. className="w-16 px-2 py-1 bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded text-white text-xs"
  1056. min="1"
  1057. />
  1058. <Button size="sm" onClick={() => handleSaveInterval(item.id, defaultInterval, defaultIntervalType)}>OK</Button>
  1059. </div>
  1060. ) : (
  1061. <button
  1062. onClick={() => {
  1063. if (!hasPermission('maintenance:update')) return;
  1064. setEditingInterval(item.id);
  1065. setIntervalInput(item.interval_hours.toString());
  1066. setIntervalTypeInput(intervalType);
  1067. }}
  1068. disabled={!hasPermission('maintenance:update')}
  1069. title={!hasPermission('maintenance:update') ? t('maintenance.noPermissionEditIntervals') : undefined}
  1070. className={`px-2 py-1 bg-bambu-dark-tertiary border border-bambu-dark-tertiary rounded text-xs font-medium text-white transition-colors flex items-center gap-1 ${hasPermission('maintenance:update') ? 'hover:bg-bambu-dark-secondary hover:border-bambu-green' : 'opacity-50 cursor-not-allowed'}`}
  1071. >
  1072. {intervalType === 'days' ? <Calendar className="w-3 h-3" /> : <Timer className="w-3 h-3" />}
  1073. {formatIntervalLabel(item.interval_hours, intervalType, t)}
  1074. <Edit3 className="w-3 h-3 text-bambu-gray" />
  1075. </button>
  1076. )}
  1077. </div>
  1078. );
  1079. })}
  1080. </div>
  1081. </CardContent>
  1082. </Card>
  1083. ))}
  1084. </div>
  1085. </div>
  1086. )}
  1087. {printerItems.length === 0 && (
  1088. <Card>
  1089. <CardContent className="text-center py-12">
  1090. <Clock className="w-12 h-12 mx-auto mb-4 text-bambu-gray/30" />
  1091. <p className="text-bambu-gray">{t('common.noPrinters')}</p>
  1092. <p className="text-sm text-bambu-gray/70 mt-1">
  1093. {t('maintenance.intervalOverridesDescription')}
  1094. </p>
  1095. </CardContent>
  1096. </Card>
  1097. )}
  1098. {pendingSystemDelete && (
  1099. <ConfirmModal
  1100. title={t('maintenance.deleteSystemTypeTitle')}
  1101. message={t('maintenance.deleteSystemTypeMessage', { name: pendingSystemDelete.name })}
  1102. confirmText={t('common.delete')}
  1103. cancelText={t('common.cancel')}
  1104. variant="danger"
  1105. cancelVariant="primary"
  1106. cardClassName="bg-red-950/70 border border-red-800/70"
  1107. onConfirm={() => {
  1108. onDeleteType(pendingSystemDelete.id);
  1109. setPendingSystemDelete(null);
  1110. }}
  1111. onCancel={() => setPendingSystemDelete(null)}
  1112. />
  1113. )}
  1114. </div>
  1115. );
  1116. }
  1117. type TabType = 'status' | 'settings';
  1118. export function MaintenancePage() {
  1119. const { t } = useTranslation();
  1120. const queryClient = useQueryClient();
  1121. const { showToast } = useToast();
  1122. const { hasPermission } = useAuth();
  1123. const [activeTab, setActiveTab] = useState<TabType>('status');
  1124. const { data: overview, isLoading } = useQuery({
  1125. queryKey: ['maintenanceOverview'],
  1126. queryFn: api.getMaintenanceOverview,
  1127. });
  1128. const { data: types } = useQuery({
  1129. queryKey: ['maintenanceTypes'],
  1130. queryFn: api.getMaintenanceTypes,
  1131. });
  1132. const performMutation = useMutation({
  1133. mutationFn: ({ id, notes }: { id: number; notes?: string }) =>
  1134. api.performMaintenance(id, notes),
  1135. onSuccess: () => {
  1136. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1137. queryClient.invalidateQueries({ queryKey: ['maintenanceSummary'] });
  1138. showToast(t('maintenance.maintenanceComplete'));
  1139. },
  1140. onError: (error: Error) => {
  1141. showToast(error.message, 'error');
  1142. },
  1143. });
  1144. const updateMutation = useMutation({
  1145. mutationFn: ({ id, data }: { id: number; data: { custom_interval_hours?: number | null; custom_interval_type?: 'hours' | 'days' | null; enabled?: boolean } }) =>
  1146. api.updateMaintenanceItem(id, data),
  1147. onSuccess: () => {
  1148. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1149. },
  1150. onError: (error: Error) => {
  1151. showToast(error.message, 'error');
  1152. },
  1153. });
  1154. // addTypeMutation removed - we now handle type creation with printer assignment
  1155. // directly in onAddType callback
  1156. const updateTypeMutation = useMutation({
  1157. mutationFn: ({ id, data }: { id: number; data: Partial<{ name: string; default_interval_hours: number; interval_type: 'hours' | 'days'; icon: string }> }) =>
  1158. api.updateMaintenanceType(id, data),
  1159. onSuccess: () => {
  1160. queryClient.invalidateQueries({ queryKey: ['maintenanceTypes'] });
  1161. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1162. showToast(t('maintenance.typeUpdated'));
  1163. },
  1164. onError: (error: Error) => {
  1165. showToast(error.message, 'error');
  1166. },
  1167. });
  1168. const deleteTypeMutation = useMutation({
  1169. mutationFn: api.deleteMaintenanceType,
  1170. onSuccess: () => {
  1171. queryClient.invalidateQueries({ queryKey: ['maintenanceTypes'] });
  1172. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1173. showToast(t('maintenance.typeDeleted'));
  1174. },
  1175. onError: (error: Error) => {
  1176. showToast(error.message, 'error');
  1177. },
  1178. });
  1179. const restoreDefaultsMutation = useMutation({
  1180. mutationFn: api.restoreDefaultMaintenanceTypes,
  1181. onSuccess: (data: { restored: number }) => {
  1182. queryClient.invalidateQueries({ queryKey: ['maintenanceTypes'] });
  1183. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1184. showToast(t('maintenance.defaultsRestored', { count: data.restored }));
  1185. },
  1186. onError: (error: Error) => {
  1187. showToast(error.message, 'error');
  1188. },
  1189. });
  1190. const setHoursMutation = useMutation({
  1191. mutationFn: ({ printerId, hours }: { printerId: number; hours: number }) =>
  1192. api.setPrinterHours(printerId, hours),
  1193. onSuccess: () => {
  1194. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1195. queryClient.invalidateQueries({ queryKey: ['maintenanceSummary'] });
  1196. showToast(t('maintenance.printHoursUpdated'));
  1197. },
  1198. onError: (error: Error) => {
  1199. showToast(error.message, 'error');
  1200. },
  1201. });
  1202. const assignTypeMutation = useMutation({
  1203. mutationFn: ({ printerId, typeId }: { printerId: number; typeId: number }) =>
  1204. api.assignMaintenanceType(printerId, typeId),
  1205. onSuccess: () => {
  1206. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1207. showToast(t('maintenance.printerAssigned'));
  1208. },
  1209. onError: (error: Error) => {
  1210. showToast(error.message, 'error');
  1211. },
  1212. });
  1213. const removeItemMutation = useMutation({
  1214. mutationFn: api.removeMaintenanceItem,
  1215. onSuccess: () => {
  1216. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1217. showToast(t('maintenance.printerRemoved'));
  1218. },
  1219. onError: (error: Error) => {
  1220. showToast(error.message, 'error');
  1221. },
  1222. });
  1223. const handlePerform = (id: number) => {
  1224. performMutation.mutate({ id });
  1225. };
  1226. const handleToggle = (id: number, enabled: boolean) => {
  1227. updateMutation.mutate({ id, data: { enabled } });
  1228. };
  1229. const handleSetHours = (printerId: number, hours: number) => {
  1230. setHoursMutation.mutate({ printerId, hours });
  1231. };
  1232. if (isLoading) {
  1233. return (
  1234. <div className="p-4 md:p-8 flex justify-center">
  1235. <Loader2 className="w-8 h-8 text-bambu-green animate-spin" />
  1236. </div>
  1237. );
  1238. }
  1239. const totalDue = overview?.reduce((sum, p) => sum + p.due_count, 0) || 0;
  1240. const totalWarning = overview?.reduce((sum, p) => sum + p.warning_count, 0) || 0;
  1241. return (
  1242. <div className="p-4 md:p-8">
  1243. {/* Header */}
  1244. <div className="mb-6">
  1245. <h1 className="text-2xl font-bold text-white">{t('maintenance.title')}</h1>
  1246. <p className="text-bambu-gray text-sm mt-1">
  1247. {activeTab === 'status' ? (
  1248. <>
  1249. {totalDue > 0 && <span className="text-red-400">{t('maintenance.dueCount', { count: totalDue })}</span>}
  1250. {totalDue > 0 && totalWarning > 0 && ' · '}
  1251. {totalWarning > 0 && <span className="text-amber-400">{t('maintenance.warningCount', { count: totalWarning })}</span>}
  1252. {totalDue === 0 && totalWarning === 0 && <span className="text-bambu-green">{t('maintenance.allOk')}</span>}
  1253. </>
  1254. ) : (
  1255. t('maintenance.configureSettings')
  1256. )}
  1257. </p>
  1258. </div>
  1259. {/* Tabs */}
  1260. <div className="flex gap-1 mb-6 border-b border-bambu-dark-tertiary">
  1261. <button
  1262. onClick={() => setActiveTab('status')}
  1263. className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
  1264. activeTab === 'status'
  1265. ? 'text-bambu-green border-bambu-green'
  1266. : 'text-bambu-gray border-transparent hover:text-white'
  1267. }`}
  1268. >
  1269. {t('maintenance.statusTab')}
  1270. </button>
  1271. <button
  1272. onClick={() => setActiveTab('settings')}
  1273. className={`px-4 py-2 text-sm font-medium border-b-2 -mb-px transition-colors ${
  1274. activeTab === 'settings'
  1275. ? 'text-bambu-green border-bambu-green'
  1276. : 'text-bambu-gray border-transparent hover:text-white'
  1277. }`}
  1278. >
  1279. {t('maintenance.settingsTab')}
  1280. </button>
  1281. </div>
  1282. {/* Tab content */}
  1283. {activeTab === 'status' ? (
  1284. <div className="space-y-6">
  1285. {overview && overview.length > 0 ? (
  1286. [...overview].sort((a, b) => {
  1287. // Sort printers with issues first
  1288. const aScore = a.due_count * 10 + a.warning_count;
  1289. const bScore = b.due_count * 10 + b.warning_count;
  1290. if (aScore !== bScore) return bScore - aScore;
  1291. return a.printer_name.localeCompare(b.printer_name);
  1292. }).map((printerOverview) => (
  1293. <PrinterSection
  1294. key={printerOverview.printer_id}
  1295. overview={printerOverview}
  1296. onPerform={handlePerform}
  1297. onToggle={handleToggle}
  1298. onSetHours={handleSetHours}
  1299. hasPermission={hasPermission}
  1300. t={t}
  1301. />
  1302. ))
  1303. ) : (
  1304. <Card>
  1305. <CardContent className="text-center py-16">
  1306. <Wrench className="w-16 h-16 mx-auto mb-4 text-bambu-gray/30" />
  1307. <p className="text-lg font-medium text-white mb-2">{t('common.noPrinters')}</p>
  1308. <p className="text-bambu-gray">{t('maintenance.configureSettings')}</p>
  1309. </CardContent>
  1310. </Card>
  1311. )}
  1312. </div>
  1313. ) : (
  1314. <SettingsSection
  1315. overview={overview}
  1316. types={types || []}
  1317. onUpdateInterval={(id, data) =>
  1318. updateMutation.mutate({ id, data })
  1319. }
  1320. onAddType={async (data, printerIds) => {
  1321. // Create the type first, then assign to selected printers
  1322. const newType = await api.createMaintenanceType(data);
  1323. // Assign to each selected printer
  1324. for (const printerId of printerIds) {
  1325. await api.assignMaintenanceType(printerId, newType.id);
  1326. }
  1327. queryClient.invalidateQueries({ queryKey: ['maintenanceTypes'] });
  1328. queryClient.invalidateQueries({ queryKey: ['maintenanceOverview'] });
  1329. showToast(t('maintenance.typeUpdated'));
  1330. }}
  1331. onUpdateType={(id, data) => updateTypeMutation.mutate({ id, data })}
  1332. onDeleteType={(id) => deleteTypeMutation.mutate(id)}
  1333. onRestoreDefaults={() => restoreDefaultsMutation.mutate()}
  1334. isRestoringDefaults={restoreDefaultsMutation.isPending}
  1335. onAssignType={(printerId, typeId) => assignTypeMutation.mutate({ printerId, typeId })}
  1336. onRemoveItem={(itemId) => removeItemMutation.mutate(itemId)}
  1337. hasPermission={hasPermission}
  1338. t={t}
  1339. />
  1340. )}
  1341. </div>
  1342. );
  1343. }