InventorySpoolInfoCard.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { useState } from 'react';
  2. import { useQuery } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { Check, AlertTriangle, RefreshCw, Unlink } from 'lucide-react';
  5. import type { InventorySpool } from '../../api/client';
  6. import { spoolbuddyApi, api } from '../../api/client';
  7. import { SpoolIcon } from './SpoolIcon';
  8. import { spoolColorString } from '../../utils/colors';
  9. const DEFAULT_CORE_WEIGHT_KEY = 'spoolbuddy-default-core-weight';
  10. function getDefaultCoreWeight(): number {
  11. try {
  12. const stored = localStorage.getItem(DEFAULT_CORE_WEIGHT_KEY);
  13. if (stored) {
  14. const weight = parseInt(stored, 10);
  15. if (weight >= 0 && weight <= 500) return weight;
  16. }
  17. } catch {
  18. // Ignore errors
  19. }
  20. return 250;
  21. }
  22. interface InventorySpoolInfoCardProps {
  23. spool: InventorySpool;
  24. liveScaleWeight: number | null;
  25. persistedGrossWeight?: number | null;
  26. onClose?: () => void;
  27. onSyncWeight?: () => void;
  28. onAssignToAms?: () => void;
  29. isAssigned?: boolean;
  30. onUnassignFromAms?: () => void;
  31. className?: string;
  32. }
  33. export function InventorySpoolInfoCard({
  34. spool,
  35. liveScaleWeight,
  36. persistedGrossWeight,
  37. onClose,
  38. onSyncWeight,
  39. onAssignToAms,
  40. isAssigned,
  41. onUnassignFromAms,
  42. className,
  43. }: InventorySpoolInfoCardProps) {
  44. const { t } = useTranslation();
  45. const [syncing, setSyncing] = useState(false);
  46. const [synced, setSynced] = useState(false);
  47. const [syncedGrossWeight, setSyncedGrossWeight] = useState<number | null>(null);
  48. // Fetch k_profiles if not already present in the spool object
  49. const { data: fetchedKProfiles } = useQuery({
  50. queryKey: ['spool-k-profiles', spool.id],
  51. queryFn: () => api.getSpoolKProfiles(spool.id),
  52. // Inventory list payloads may omit k_profiles, so lazily fetch when missing.
  53. enabled: !spool.k_profiles || spool.k_profiles.length === 0,
  54. staleTime: 5 * 60 * 1000,
  55. });
  56. // Use fetched k_profiles if available, otherwise use the ones from the spool object
  57. const kProfiles = (spool.k_profiles && spool.k_profiles.length > 0) ? spool.k_profiles : fetchedKProfiles;
  58. const colorHex = spoolColorString(spool.rgba);
  59. const coreWeight = (spool.core_weight && spool.core_weight > 0)
  60. ? spool.core_weight
  61. : getDefaultCoreWeight();
  62. const grossWeightFromScale = liveScaleWeight !== null
  63. ? Math.round(Math.max(0, liveScaleWeight))
  64. : null;
  65. // Inventory scenario: prefer the most recently synced value in this modal session.
  66. const displayedGrossWeight = syncedGrossWeight ?? (
  67. persistedGrossWeight !== undefined
  68. ? (persistedGrossWeight !== null ? Math.round(Math.max(0, persistedGrossWeight)) : null)
  69. : grossWeightFromScale
  70. );
  71. const inventoryRemaining = Math.round(Math.max(0,
  72. (spool.label_weight || 0) - (spool.weight_used || 0)
  73. ));
  74. // Use live scale for remaining/fill only when scale has a meaningful reading.
  75. const minDynamicScaleReading = 10;
  76. const useDynamicRemaining = grossWeightFromScale !== null
  77. && grossWeightFromScale >= minDynamicScaleReading;
  78. const remaining = useDynamicRemaining
  79. ? Math.round(Math.max(0, grossWeightFromScale - coreWeight))
  80. : inventoryRemaining;
  81. const labelWeight = Math.round(spool.label_weight || 1000);
  82. const fillPercent = labelWeight > 0 ? Math.min(100, Math.round((remaining / labelWeight) * 100)) : null;
  83. const fillColor = fillPercent !== null
  84. ? (fillPercent > 50 ? '#22c55e' : fillPercent > 20 ? '#eab308' : '#ef4444')
  85. : '#808080';
  86. const netWeight = Math.max(0,
  87. (spool.label_weight || 0) - (spool.weight_used || 0)
  88. );
  89. const calculatedWeight = netWeight + coreWeight;
  90. const difference = grossWeightFromScale !== null ? grossWeightFromScale - calculatedWeight : null;
  91. const isMatch = difference !== null ? Math.abs(difference) <= 50 : null;
  92. // Inventory fallback so gross is always populated across spools.
  93. const inventoryDerivedGrossWeight = Math.round(calculatedWeight);
  94. const resolvedGrossWeight = displayedGrossWeight ?? inventoryDerivedGrossWeight;
  95. const nozzleTempRange = (spool.nozzle_temp_min != null && spool.nozzle_temp_max != null)
  96. ? `${spool.nozzle_temp_min}-${spool.nozzle_temp_max}\u00B0C`
  97. : null;
  98. const slicerPreset = spool.slicer_filament_name || spool.slicer_filament || null;
  99. const note = spool.note?.trim() || null;
  100. const kFactorSummary = (kProfiles && kProfiles.length > 0)
  101. ? Array.from(new Set(kProfiles.map(kp => kp.k_value.toFixed(3)))).join(', ')
  102. : null;
  103. const handleSyncWeight = async () => {
  104. if (liveScaleWeight === null) return;
  105. const roundedLiveWeight = Math.round(Math.max(0, liveScaleWeight));
  106. setSyncing(true);
  107. try {
  108. await spoolbuddyApi.updateSpoolWeight(spool.id, roundedLiveWeight);
  109. setSyncedGrossWeight(roundedLiveWeight);
  110. setSynced(true);
  111. onSyncWeight?.();
  112. setTimeout(() => setSynced(false), 3000);
  113. } catch (e) {
  114. console.error('Failed to sync weight:', e);
  115. } finally {
  116. setSyncing(false);
  117. }
  118. };
  119. return (
  120. <div className={`flex flex-col items-center space-y-4 max-w-md ${className ?? ''}`}>
  121. <div className="flex items-start gap-5">
  122. <div className="relative shrink-0">
  123. <SpoolIcon color={colorHex} isEmpty={false} size={100} />
  124. {fillPercent !== null && (
  125. <div
  126. className="absolute -bottom-2 -right-2 px-2 py-0.5 rounded-full text-xs font-bold text-white shadow-lg"
  127. style={{ backgroundColor: fillColor }}
  128. >
  129. {fillPercent}%
  130. </div>
  131. )}
  132. </div>
  133. <div className="flex-1 min-w-0 pt-1">
  134. <div className="flex items-center gap-2">
  135. <h3 className="text-lg font-semibold text-zinc-100">
  136. {spool.color_name || 'Unknown color'}
  137. </h3>
  138. <span className="text-xs font-mono text-zinc-500 shrink-0">#{spool.id}</span>
  139. </div>
  140. <p className="text-sm text-zinc-400">
  141. {spool.brand} &bull; {spool.material}
  142. {spool.subtype && ` ${spool.subtype}`}
  143. </p>
  144. <div className="mt-3">
  145. <div className="flex items-baseline gap-2">
  146. <span className="text-3xl font-bold font-mono text-zinc-100">{remaining}g</span>
  147. <span className="text-sm text-zinc-500">/ {labelWeight}g</span>
  148. </div>
  149. <p className="text-xs text-zinc-500 mt-0.5">{t('spoolbuddy.spool.remaining', 'Remaining')}</p>
  150. <div className="mt-2 max-w-xs">
  151. <div className="h-2 bg-zinc-700 rounded-full overflow-hidden">
  152. <div
  153. className="h-full rounded-full transition-all duration-500"
  154. style={{ width: `${fillPercent ?? 0}%`, backgroundColor: fillColor }}
  155. />
  156. </div>
  157. </div>
  158. </div>
  159. </div>
  160. </div>
  161. <div className="grid grid-cols-2 gap-x-6 gap-y-2 text-sm bg-zinc-800 rounded-lg p-4 w-full">
  162. <div className="flex justify-between">
  163. <span className="text-zinc-500">{t('spoolbuddy.dashboard.grossWeight', 'Gross weight')}</span>
  164. <span className="font-mono text-zinc-300">{resolvedGrossWeight}g</span>
  165. </div>
  166. <div className="flex justify-between">
  167. <span className="text-zinc-500">{t('spoolbuddy.spool.coreWeight', 'Core')}</span>
  168. <span className="font-mono text-zinc-300">{coreWeight}g</span>
  169. </div>
  170. <div className="flex justify-between">
  171. <span className="text-zinc-500">{t('spoolbuddy.dashboard.spoolSize', 'Spool size')}</span>
  172. <span className="font-mono text-zinc-300">{labelWeight}g</span>
  173. </div>
  174. <div className="flex justify-between items-center">
  175. <span className="text-zinc-500">{t('spoolbuddy.spool.scaleWeight', 'Scale')}</span>
  176. {grossWeightFromScale !== null ? (
  177. <span className={`flex items-center gap-1 font-mono ${isMatch ? 'text-green-500' : 'text-yellow-500'}`}>
  178. {grossWeightFromScale}g
  179. {isMatch ? (
  180. <Check className="w-3.5 h-3.5" />
  181. ) : (
  182. <>
  183. <AlertTriangle className="w-3.5 h-3.5" />
  184. <button
  185. onClick={handleSyncWeight}
  186. className="p-1 hover:bg-green-500/20 rounded transition-colors text-green-500"
  187. title={t('spoolbuddy.dashboard.syncWeight', 'Sync Weight')}
  188. >
  189. <RefreshCw className="w-4 h-4" />
  190. </button>
  191. </>
  192. )}
  193. </span>
  194. ) : (
  195. <span className="text-zinc-500">{'\u2014'}</span>
  196. )}
  197. </div>
  198. <div className="flex justify-between items-center">
  199. <span className="text-zinc-500">{t('spoolbuddy.dashboard.tagId', 'Tag')}</span>
  200. <span className="font-mono text-xs text-zinc-400 truncate max-w-[120px]" title={spool.tag_uid || ''}>
  201. {spool.tag_uid ? spool.tag_uid.slice(-8) : '\u2014'}
  202. </span>
  203. </div>
  204. {nozzleTempRange && (
  205. <div className="flex justify-between items-center">
  206. <span className="text-zinc-500">{t('spoolbuddy.inventory.nozzleTemp', 'Nozzle')}</span>
  207. <span className="font-mono text-zinc-300">{nozzleTempRange}</span>
  208. </div>
  209. )}
  210. {spool.cost_per_kg != null && spool.cost_per_kg > 0 && (
  211. <div className="flex justify-between items-center">
  212. <span className="text-zinc-500">{t('spoolbuddy.inventory.costPerKg', 'Cost/kg')}</span>
  213. <span className="font-mono text-zinc-300">{spool.cost_per_kg.toFixed(2)}/kg</span>
  214. </div>
  215. )}
  216. {kFactorSummary && (
  217. <div className="flex justify-between items-center">
  218. <span className="text-zinc-500">{t('spoolbuddy.inventory.kProfiles', 'K-Profile')}</span>
  219. <span className="font-mono text-zinc-300 truncate max-w-[220px] text-right" title={kFactorSummary}>{kFactorSummary}</span>
  220. </div>
  221. )}
  222. {slicerPreset && (
  223. <div className="min-w-0">
  224. <p className="text-xs text-zinc-500 mb-1">{t('spoolbuddy.inventory.slicerFilament', 'Slicer Filament')}</p>
  225. <p className="text-sm text-zinc-300 whitespace-pre-wrap break-words">{slicerPreset}</p>
  226. </div>
  227. )}
  228. {note && (
  229. <div className="col-span-2">
  230. <p className="text-xs text-zinc-500 mb-1">{t('spoolbuddy.inventory.note', 'Note')}</p>
  231. <p className="text-sm leading-5 text-zinc-300 whitespace-pre-wrap break-words max-h-[3.75rem] overflow-y-auto pr-1">{note}</p>
  232. </div>
  233. )}
  234. </div>
  235. <div className="flex gap-2 justify-center">
  236. {onAssignToAms && (
  237. <button
  238. onClick={isAssigned ? undefined : onAssignToAms}
  239. disabled={!!isAssigned}
  240. className="px-5 py-2.5 rounded-lg text-sm font-medium bg-green-600 text-white hover:bg-green-700 transition-colors min-h-[44px] disabled:opacity-50 disabled:cursor-not-allowed"
  241. >
  242. {t('spoolbuddy.modal.assignToAms', 'Assign to AMS')}
  243. </button>
  244. )}
  245. {onUnassignFromAms && (
  246. <button
  247. onClick={onUnassignFromAms}
  248. className="px-5 py-2.5 rounded-lg text-sm font-medium bg-red-600/20 text-red-400 hover:bg-red-600/30 transition-colors min-h-[44px]"
  249. >
  250. <Unlink className="w-4 h-4 inline mr-1" />
  251. {t('inventory.unassignSpool')}
  252. </button>
  253. )}
  254. <button
  255. onClick={handleSyncWeight}
  256. disabled={liveScaleWeight === null || syncing}
  257. className={`px-5 py-2.5 rounded-lg text-sm font-medium transition-colors min-h-[44px] ${
  258. synced
  259. ? 'bg-green-600/20 text-green-400'
  260. : onAssignToAms
  261. ? 'bg-zinc-700 text-zinc-300 hover:bg-zinc-600 disabled:opacity-40 disabled:cursor-not-allowed'
  262. : 'bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 disabled:cursor-not-allowed'
  263. }`}
  264. >
  265. {syncing ? '...' : synced ? t('spoolbuddy.dashboard.weightSynced', 'Synced!') : t('spoolbuddy.dashboard.syncWeight', 'Sync Weight')}
  266. </button>
  267. {onClose && (
  268. <button
  269. onClick={onClose}
  270. className="px-5 py-2.5 rounded-lg text-sm font-medium bg-zinc-700 text-zinc-300 hover:bg-zinc-600 transition-colors min-h-[44px]"
  271. >
  272. {t('spoolbuddy.dashboard.close', 'Close')}
  273. </button>
  274. )}
  275. </div>
  276. </div>
  277. );
  278. }