SpoolInfoCard.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { Check, AlertTriangle, RefreshCw, Unlink } from 'lucide-react';
  4. import type { MatchedSpool } from '../../hooks/useSpoolBuddyState';
  5. import { spoolbuddyApi } from '../../api/client';
  6. import { SpoolIcon } from './SpoolIcon';
  7. // Storage key for default core weight
  8. const DEFAULT_CORE_WEIGHT_KEY = 'spoolbuddy-default-core-weight';
  9. function getDefaultCoreWeight(): number {
  10. try {
  11. const stored = localStorage.getItem(DEFAULT_CORE_WEIGHT_KEY);
  12. if (stored) {
  13. const weight = parseInt(stored, 10);
  14. if (weight >= 0 && weight <= 500) return weight;
  15. }
  16. } catch {
  17. // Ignore errors
  18. }
  19. return 250; // Default 250g (typical Bambu spool core)
  20. }
  21. interface SpoolInfoCardProps {
  22. spool: MatchedSpool;
  23. scaleWeight: number | null;
  24. onClose?: () => void;
  25. onSyncWeight?: () => void;
  26. onAssignToAms?: () => void;
  27. isAssigned?: boolean;
  28. onUnassignFromAms?: () => void;
  29. }
  30. export function SpoolInfoCard({ spool, scaleWeight, onClose, onSyncWeight, onAssignToAms, isAssigned, onUnassignFromAms }: SpoolInfoCardProps) {
  31. const { t } = useTranslation();
  32. const [syncing, setSyncing] = useState(false);
  33. const [synced, setSynced] = useState(false);
  34. const colorHex = spool.rgba ? `#${spool.rgba.slice(0, 6)}` : '#808080';
  35. // Use spool's core_weight if set, otherwise fall back to default
  36. const coreWeight = (spool.core_weight && spool.core_weight > 0)
  37. ? spool.core_weight
  38. : getDefaultCoreWeight();
  39. // Gross weight from scale (live) or fallback
  40. const grossWeight = scaleWeight !== null
  41. ? Math.round(Math.max(0, scaleWeight))
  42. : null;
  43. // Remaining filament = gross - core
  44. const remaining = grossWeight !== null
  45. ? Math.round(Math.max(0, grossWeight - coreWeight))
  46. : null;
  47. const labelWeight = Math.round(spool.label_weight || 1000);
  48. const fillPercent = remaining !== null ? Math.min(100, Math.round((remaining / labelWeight) * 100)) : null;
  49. const fillColor = fillPercent !== null
  50. ? fillPercent > 50 ? '#22c55e' : fillPercent > 20 ? '#eab308' : '#ef4444'
  51. : '#808080';
  52. // Weight comparison (scale vs calculated expected)
  53. const netWeight = Math.max(0,
  54. (spool.label_weight || 0) - (spool.weight_used || 0)
  55. );
  56. const calculatedWeight = netWeight + coreWeight;
  57. const difference = grossWeight !== null ? grossWeight - calculatedWeight : null;
  58. const isMatch = difference !== null ? Math.abs(difference) <= 50 : null;
  59. const handleSyncWeight = async () => {
  60. if (scaleWeight === null) return;
  61. setSyncing(true);
  62. try {
  63. await spoolbuddyApi.updateSpoolWeight(spool.id, Math.round(scaleWeight));
  64. setSynced(true);
  65. onSyncWeight?.();
  66. setTimeout(() => setSynced(false), 3000);
  67. } catch (e) {
  68. console.error('Failed to sync weight:', e);
  69. } finally {
  70. setSyncing(false);
  71. }
  72. };
  73. return (
  74. <div className="flex flex-col items-center space-y-4 max-w-md">
  75. {/* Top section: Spool icon + main info */}
  76. <div className="flex items-start gap-5">
  77. {/* Spool visualization */}
  78. <div className="relative shrink-0">
  79. <SpoolIcon color={colorHex} isEmpty={false} size={100} />
  80. {fillPercent !== null && (
  81. <div
  82. className="absolute -bottom-2 -right-2 px-2 py-0.5 rounded-full text-xs font-bold text-white shadow-lg"
  83. style={{ backgroundColor: fillColor }}
  84. >
  85. {fillPercent}%
  86. </div>
  87. )}
  88. </div>
  89. {/* Main info */}
  90. <div className="flex-1 min-w-0 pt-1">
  91. <h3 className="text-lg font-semibold text-zinc-100">
  92. {spool.color_name || 'Unknown color'}
  93. </h3>
  94. <p className="text-sm text-zinc-400">
  95. {spool.brand} &bull; {spool.material}
  96. {spool.subtype && ` ${spool.subtype}`}
  97. </p>
  98. {/* Filament remaining - big number */}
  99. {remaining !== null && (
  100. <div className="mt-3">
  101. <div className="flex items-baseline gap-2">
  102. <span className="text-3xl font-bold font-mono text-zinc-100">{remaining}g</span>
  103. <span className="text-sm text-zinc-500">/ {labelWeight}g</span>
  104. </div>
  105. <p className="text-xs text-zinc-500 mt-0.5">{t('spoolbuddy.spool.remaining', 'Remaining')}</p>
  106. {/* Fill bar */}
  107. <div className="mt-2 max-w-xs">
  108. <div className="h-2 bg-zinc-700 rounded-full overflow-hidden">
  109. <div
  110. className="h-full rounded-full transition-all duration-500"
  111. style={{ width: `${fillPercent}%`, backgroundColor: fillColor }}
  112. />
  113. </div>
  114. </div>
  115. </div>
  116. )}
  117. </div>
  118. </div>
  119. {/* Details grid */}
  120. <div className="grid grid-cols-2 gap-x-6 gap-y-2 text-sm bg-zinc-800 rounded-lg p-4 w-full">
  121. <div className="flex justify-between">
  122. <span className="text-zinc-500">{t('spoolbuddy.dashboard.grossWeight', 'Gross weight')}</span>
  123. <span className="font-mono text-zinc-300">{grossWeight !== null ? `${grossWeight}g` : '\u2014'}</span>
  124. </div>
  125. <div className="flex justify-between">
  126. <span className="text-zinc-500">{t('spoolbuddy.spool.coreWeight', 'Core')}</span>
  127. <span className="font-mono text-zinc-300">{coreWeight}g</span>
  128. </div>
  129. <div className="flex justify-between">
  130. <span className="text-zinc-500">{t('spoolbuddy.dashboard.spoolSize', 'Spool size')}</span>
  131. <span className="font-mono text-zinc-300">{labelWeight}g</span>
  132. </div>
  133. <div className="flex justify-between items-center">
  134. <span className="text-zinc-500">{t('spoolbuddy.spool.scaleWeight', 'Scale')}</span>
  135. {grossWeight !== null ? (
  136. <span className={`flex items-center gap-1 font-mono ${isMatch ? 'text-green-500' : 'text-yellow-500'}`}>
  137. {grossWeight}g
  138. {isMatch ? (
  139. <Check className="w-3.5 h-3.5" />
  140. ) : (
  141. <>
  142. <AlertTriangle className="w-3.5 h-3.5" />
  143. <button
  144. onClick={handleSyncWeight}
  145. className="p-1 hover:bg-green-500/20 rounded transition-colors text-green-500"
  146. title={t('spoolbuddy.dashboard.syncWeight', 'Sync Weight')}
  147. >
  148. <RefreshCw className="w-4 h-4" />
  149. </button>
  150. </>
  151. )}
  152. </span>
  153. ) : (
  154. <span className="text-zinc-500">{'\u2014'}</span>
  155. )}
  156. </div>
  157. <div className="flex justify-between items-center">
  158. <span className="text-zinc-500">{t('spoolbuddy.dashboard.tagId', 'Tag')}</span>
  159. <span className="font-mono text-xs text-zinc-400 truncate max-w-[120px]" title={spool.tag_uid || ''}>
  160. {spool.tag_uid ? spool.tag_uid.slice(-8) : '\u2014'}
  161. </span>
  162. </div>
  163. </div>
  164. {/* Action buttons */}
  165. <div className="flex gap-2 justify-center">
  166. {onAssignToAms && (
  167. <button
  168. onClick={isAssigned ? undefined : onAssignToAms}
  169. disabled={!!isAssigned}
  170. 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"
  171. >
  172. {t('spoolbuddy.modal.assignToAms', 'Assign to AMS')}
  173. </button>
  174. )}
  175. {onUnassignFromAms && (
  176. <button
  177. onClick={onUnassignFromAms}
  178. 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]"
  179. >
  180. <Unlink className="w-4 h-4 inline mr-1" />
  181. {t('inventory.unassignSpool')}
  182. </button>
  183. )}
  184. <button
  185. onClick={handleSyncWeight}
  186. disabled={scaleWeight === null || syncing}
  187. className={`px-5 py-2.5 rounded-lg text-sm font-medium transition-colors min-h-[44px] ${
  188. synced
  189. ? 'bg-green-600/20 text-green-400'
  190. : onAssignToAms
  191. ? 'bg-zinc-700 text-zinc-300 hover:bg-zinc-600 disabled:opacity-40 disabled:cursor-not-allowed'
  192. : 'bg-green-600 text-white hover:bg-green-700 disabled:opacity-40 disabled:cursor-not-allowed'
  193. }`}
  194. >
  195. {syncing ? '...' : synced ? t('spoolbuddy.dashboard.weightSynced', 'Synced!') : t('spoolbuddy.dashboard.syncWeight', 'Sync Weight')}
  196. </button>
  197. {onClose && (
  198. <button
  199. onClick={onClose}
  200. 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]"
  201. >
  202. {t('spoolbuddy.dashboard.close', 'Close')}
  203. </button>
  204. )}
  205. </div>
  206. </div>
  207. );
  208. }
  209. interface UnknownTagCardProps {
  210. tagUid: string;
  211. scaleWeight: number | null;
  212. coreWeight?: number;
  213. onLinkSpool?: () => void;
  214. onAddToInventory?: () => void;
  215. onClose?: () => void;
  216. }
  217. export function UnknownTagCard({ tagUid, scaleWeight, coreWeight, onLinkSpool, onAddToInventory, onClose }: UnknownTagCardProps) {
  218. const { t } = useTranslation();
  219. const defaultCoreWeight = coreWeight ?? getDefaultCoreWeight();
  220. const grossWeight = scaleWeight !== null
  221. ? Math.round(Math.max(0, scaleWeight))
  222. : null;
  223. const estimatedRemaining = grossWeight !== null
  224. ? Math.round(Math.max(0, grossWeight - defaultCoreWeight))
  225. : null;
  226. return (
  227. <div className="flex flex-col items-center text-center space-y-5">
  228. <div className="w-20 h-20 rounded-2xl bg-green-500/15 flex items-center justify-center">
  229. <svg className="w-10 h-10 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
  230. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A2 2 0 013 12V7a4 4 0 014-4z" />
  231. </svg>
  232. </div>
  233. <div>
  234. <h3 className="text-lg font-semibold text-zinc-100">{t('spoolbuddy.dashboard.newTag', 'New Tag Detected')}</h3>
  235. <p className="text-sm text-zinc-500 font-mono mt-1">{tagUid}</p>
  236. </div>
  237. {grossWeight !== null && (
  238. <div className="text-sm text-zinc-400">
  239. <span className="font-mono font-semibold">{grossWeight}g</span> {t('spoolbuddy.dashboard.onScale', 'on scale')}
  240. {estimatedRemaining !== null && estimatedRemaining > 0 && (
  241. <span className="text-zinc-500"> &bull; ~{estimatedRemaining}g filament</span>
  242. )}
  243. </div>
  244. )}
  245. <div className="flex flex-wrap gap-2 justify-center">
  246. {onAddToInventory && (
  247. <button
  248. onClick={onAddToInventory}
  249. 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]"
  250. >
  251. {t('spoolbuddy.modal.addToInventory', 'Add to Inventory')}
  252. </button>
  253. )}
  254. {onLinkSpool && (
  255. <button
  256. onClick={onLinkSpool}
  257. 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]"
  258. >
  259. <svg className="w-4 h-4 inline-block mr-1.5 -mt-0.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
  260. <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13.828 10.172a4 4 0 00-5.656 0l-4 4a4 4 0 105.656 5.656l1.102-1.101m-.758-4.899a4 4 0 005.656 0l4-4a4 4 0 00-5.656-5.656l-1.1 1.1" />
  261. </svg>
  262. {t('inventory.assignSpool', 'Assign Spool')}
  263. </button>
  264. )}
  265. {onClose && (
  266. <button
  267. onClick={onClose}
  268. 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]"
  269. >
  270. {t('spoolbuddy.dashboard.close', 'Close')}
  271. </button>
  272. )}
  273. </div>
  274. </div>
  275. );
  276. }