VirtualPrinterSettings.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. import { useState, useEffect } from 'react';
  2. import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
  3. import { Loader2, Check, AlertTriangle, Printer, Eye, EyeOff, Info, ChevronDown, ExternalLink } from 'lucide-react';
  4. import { virtualPrinterApi } from '../api/client';
  5. import { Card, CardContent, CardHeader } from './Card';
  6. import { Button } from './Button';
  7. import { useToast } from '../contexts/ToastContext';
  8. export function VirtualPrinterSettings() {
  9. const queryClient = useQueryClient();
  10. const { showToast } = useToast();
  11. const [localEnabled, setLocalEnabled] = useState(false);
  12. const [localAccessCode, setLocalAccessCode] = useState('');
  13. const [localMode, setLocalMode] = useState<'immediate' | 'queue'>('immediate');
  14. const [localModel, setLocalModel] = useState('3DPrinter-X1-Carbon');
  15. const [showAccessCode, setShowAccessCode] = useState(false);
  16. const [pendingAction, setPendingAction] = useState<'toggle' | 'accessCode' | 'mode' | 'model' | null>(null);
  17. // Fetch current settings
  18. const { data: settings, isLoading } = useQuery({
  19. queryKey: ['virtual-printer-settings'],
  20. queryFn: virtualPrinterApi.getSettings,
  21. refetchInterval: 10000, // Refresh every 10 seconds for status updates
  22. });
  23. // Fetch available models
  24. const { data: modelsData } = useQuery({
  25. queryKey: ['virtual-printer-models'],
  26. queryFn: virtualPrinterApi.getModels,
  27. });
  28. // Initialize local state from settings
  29. useEffect(() => {
  30. if (settings) {
  31. setLocalEnabled(settings.enabled);
  32. setLocalMode(settings.mode);
  33. setLocalModel(settings.model);
  34. }
  35. }, [settings]);
  36. // Update mutation
  37. const updateMutation = useMutation({
  38. mutationFn: (data: { enabled?: boolean; access_code?: string; mode?: 'immediate' | 'queue'; model?: string }) =>
  39. virtualPrinterApi.updateSettings(data),
  40. onSuccess: () => {
  41. queryClient.invalidateQueries({ queryKey: ['virtual-printer-settings'] });
  42. showToast('Virtual printer settings updated');
  43. setPendingAction(null);
  44. },
  45. onError: (error: Error) => {
  46. showToast(error.message || 'Failed to update settings', 'error');
  47. // Revert local state on error
  48. if (settings) {
  49. setLocalEnabled(settings.enabled);
  50. setLocalMode(settings.mode);
  51. setLocalModel(settings.model);
  52. }
  53. setPendingAction(null);
  54. },
  55. });
  56. const handleToggleEnabled = () => {
  57. const newEnabled = !localEnabled;
  58. // If enabling, must have access code
  59. if (newEnabled && !localAccessCode && !settings?.access_code_set) {
  60. showToast('Please set an access code first', 'error');
  61. return;
  62. }
  63. setLocalEnabled(newEnabled);
  64. setPendingAction('toggle');
  65. updateMutation.mutate({
  66. enabled: newEnabled,
  67. access_code: localAccessCode || undefined,
  68. mode: localMode,
  69. });
  70. };
  71. const handleAccessCodeChange = () => {
  72. if (!localAccessCode) {
  73. showToast('Access code cannot be empty', 'error');
  74. return;
  75. }
  76. if (localAccessCode.length !== 8) {
  77. showToast('Access code must be exactly 8 characters', 'error');
  78. return;
  79. }
  80. setPendingAction('accessCode');
  81. updateMutation.mutate({
  82. access_code: localAccessCode,
  83. });
  84. setLocalAccessCode(''); // Clear after saving
  85. };
  86. const handleModeChange = (mode: 'immediate' | 'queue') => {
  87. setLocalMode(mode);
  88. setPendingAction('mode');
  89. updateMutation.mutate({ mode });
  90. };
  91. const handleModelChange = (model: string) => {
  92. setLocalModel(model);
  93. setPendingAction('model');
  94. updateMutation.mutate({ model });
  95. };
  96. if (isLoading) {
  97. return (
  98. <Card>
  99. <CardContent className="py-8 flex justify-center">
  100. <Loader2 className="w-6 h-6 animate-spin text-bambu-green" />
  101. </CardContent>
  102. </Card>
  103. );
  104. }
  105. const status = settings?.status;
  106. const isRunning = status?.running || false;
  107. return (
  108. <div className="flex flex-col lg:flex-row gap-6 lg:gap-8">
  109. {/* Left Column - Settings */}
  110. <div className="space-y-6 lg:w-[480px] lg:flex-shrink-0">
  111. <Card>
  112. <CardHeader>
  113. <div className="flex items-center justify-between">
  114. <div className="flex items-center gap-2">
  115. <Printer className="w-5 h-5 text-bambu-green" />
  116. <h2 className="text-lg font-semibold text-white">Virtual Printer</h2>
  117. </div>
  118. {status && (
  119. <div className={`flex items-center gap-2 text-sm ${isRunning ? 'text-green-400' : 'text-bambu-gray'}`}>
  120. <span className={`w-2 h-2 rounded-full ${isRunning ? 'bg-green-400 animate-pulse' : 'bg-gray-500'}`} />
  121. {isRunning ? 'Running' : 'Stopped'}
  122. </div>
  123. )}
  124. </div>
  125. </CardHeader>
  126. <CardContent className="space-y-4">
  127. <p className="text-sm text-bambu-gray">
  128. Enable a virtual printer that appears in Bambu Studio and OrcaSlicer. Files sent to this printer
  129. will be archived directly without printing.
  130. </p>
  131. {/* Enable/Disable Toggle */}
  132. <div className="flex items-center justify-between py-3 border-t border-bambu-dark-tertiary">
  133. <div>
  134. <div className="text-white font-medium">Enable Virtual Printer</div>
  135. <div className="text-sm text-bambu-gray">
  136. {isRunning ? 'Visible as "Bambuddy" in slicer discovery' : 'Not visible to slicers'}
  137. </div>
  138. </div>
  139. <button
  140. onClick={handleToggleEnabled}
  141. disabled={pendingAction === 'toggle'}
  142. className={`relative w-12 h-6 rounded-full transition-colors ${
  143. localEnabled ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  144. } ${pendingAction === 'toggle' ? 'opacity-50' : ''}`}
  145. >
  146. <span
  147. className={`absolute top-1 left-1 w-4 h-4 bg-white rounded-full transition-transform ${
  148. localEnabled ? 'translate-x-6' : ''
  149. }`}
  150. />
  151. </button>
  152. </div>
  153. {/* Printer Model */}
  154. <div className="py-3 border-t border-bambu-dark-tertiary">
  155. <div className="text-white font-medium mb-2">Printer Model</div>
  156. <div className="text-sm text-bambu-gray mb-3">
  157. Select which printer model to emulate.
  158. </div>
  159. <div className="relative">
  160. <select
  161. value={localModel}
  162. onChange={(e) => handleModelChange(e.target.value)}
  163. disabled={pendingAction === 'model' || (localEnabled && isRunning)}
  164. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white appearance-none cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed pr-10"
  165. >
  166. {modelsData?.models && Object.entries(modelsData.models)
  167. .sort(([, a], [, b]) => (a as string).localeCompare(b as string))
  168. .map(([code, name]) => (
  169. <option key={code} value={code}>
  170. {name}
  171. </option>
  172. ))}
  173. </select>
  174. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  175. </div>
  176. {localEnabled && isRunning && (
  177. <p className="text-xs text-yellow-400 mt-2">
  178. <AlertTriangle className="w-3 h-3 inline mr-1" />
  179. Disable the virtual printer to change the model
  180. </p>
  181. )}
  182. </div>
  183. {/* Access Code */}
  184. <div className="py-3 border-t border-bambu-dark-tertiary">
  185. <div className="text-white font-medium mb-2">Access Code</div>
  186. <div className="text-sm text-bambu-gray mb-3">
  187. {settings?.access_code_set ? (
  188. <span className="flex items-center gap-1 text-green-400">
  189. <Check className="w-4 h-4" />
  190. Access code is set
  191. </span>
  192. ) : (
  193. <span className="flex items-center gap-1 text-yellow-400">
  194. <AlertTriangle className="w-4 h-4" />
  195. No access code set - required to enable
  196. </span>
  197. )}
  198. </div>
  199. <div className="flex gap-2">
  200. <div className="relative flex-1">
  201. <input
  202. type={showAccessCode ? 'text' : 'password'}
  203. value={localAccessCode}
  204. onChange={(e) => setLocalAccessCode(e.target.value)}
  205. placeholder={settings?.access_code_set ? 'Enter new code to change' : 'Enter 8-char code'}
  206. maxLength={8}
  207. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-2 text-white placeholder-bambu-gray pr-10 font-mono"
  208. />
  209. <button
  210. onClick={() => setShowAccessCode(!showAccessCode)}
  211. className="absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
  212. >
  213. {showAccessCode ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
  214. </button>
  215. </div>
  216. <Button
  217. onClick={handleAccessCodeChange}
  218. disabled={!localAccessCode || pendingAction === 'accessCode'}
  219. variant="primary"
  220. >
  221. {pendingAction === 'accessCode' ? <Loader2 className="w-4 h-4 animate-spin" /> : 'Save'}
  222. </Button>
  223. </div>
  224. <p className="text-xs text-bambu-gray mt-2">
  225. Must be exactly 8 characters. Used by slicers to authenticate.
  226. {localAccessCode && (
  227. <span className={localAccessCode.length === 8 ? 'text-green-400' : 'text-yellow-400'}>
  228. {' '}({localAccessCode.length}/8)
  229. </span>
  230. )}
  231. </p>
  232. </div>
  233. {/* Archive Mode */}
  234. <div className="py-3 border-t border-bambu-dark-tertiary">
  235. <div className="text-white font-medium mb-2">Archive Mode</div>
  236. <div className="grid grid-cols-2 gap-3">
  237. <button
  238. onClick={() => handleModeChange('immediate')}
  239. disabled={pendingAction === 'mode'}
  240. className={`p-3 rounded-lg border text-left transition-colors ${
  241. localMode === 'immediate'
  242. ? 'border-bambu-green bg-bambu-green/10'
  243. : 'border-bambu-dark-tertiary hover:border-bambu-gray'
  244. }`}
  245. >
  246. <div className="text-white font-medium">Immediate</div>
  247. <div className="text-xs text-bambu-gray">Archive files as soon as they are uploaded</div>
  248. </button>
  249. <button
  250. onClick={() => handleModeChange('queue')}
  251. disabled={pendingAction === 'mode'}
  252. className={`p-3 rounded-lg border text-left transition-colors ${
  253. localMode === 'queue'
  254. ? 'border-bambu-green bg-bambu-green/10'
  255. : 'border-bambu-dark-tertiary hover:border-bambu-gray'
  256. }`}
  257. >
  258. <div className="text-white font-medium">Queue for Review</div>
  259. <div className="text-xs text-bambu-gray">Review and tag files before archiving</div>
  260. </button>
  261. </div>
  262. </div>
  263. </CardContent>
  264. </Card>
  265. </div>
  266. {/* Right Column - Info & Status */}
  267. <div className="space-y-6 lg:w-[480px] lg:flex-shrink-0">
  268. {/* Setup Required Warning */}
  269. <Card className="border-l-4 border-l-yellow-500">
  270. <CardContent className="py-4">
  271. <div className="flex items-start gap-3">
  272. <AlertTriangle className="w-5 h-5 text-yellow-500 flex-shrink-0 mt-0.5" />
  273. <div className="text-sm">
  274. <p className="text-white font-medium mb-2">
  275. Setup Required
  276. </p>
  277. <p className="text-bambu-gray mb-3">
  278. The virtual printer feature requires additional system configuration before it will work.
  279. This includes port forwarding, firewall rules, and platform-specific settings.
  280. </p>
  281. <a
  282. href="https://wiki.bambuddy.cool/features/virtual-printer/"
  283. target="_blank"
  284. rel="noopener noreferrer"
  285. className="inline-flex items-center gap-2 px-4 py-2 bg-yellow-500/20 border border-yellow-500/50 rounded-md text-yellow-400 hover:bg-yellow-500/30 transition-colors"
  286. >
  287. <ExternalLink className="w-4 h-4" />
  288. Read the setup guide before enabling
  289. </a>
  290. </div>
  291. </div>
  292. </CardContent>
  293. </Card>
  294. {/* How it works */}
  295. <Card>
  296. <CardContent className="py-4">
  297. <div className="flex items-start gap-3">
  298. <Info className="w-5 h-5 text-blue-400 flex-shrink-0 mt-0.5" />
  299. <div className="text-sm text-bambu-gray">
  300. <p className="mb-2">
  301. <strong className="text-white">How it works:</strong>
  302. </p>
  303. <ol className="list-decimal list-inside space-y-1">
  304. <li>Complete the setup guide for your platform</li>
  305. <li>Enable the virtual printer and set an access code</li>
  306. <li>In Bambu Studio or OrcaSlicer, go to "Add Printer"</li>
  307. <li>The "Bambuddy" printer should appear in the discovery list</li>
  308. <li>Connect using the access code you set</li>
  309. <li>When you "print" to Bambuddy, the 3MF file is archived instead</li>
  310. </ol>
  311. </div>
  312. </div>
  313. </CardContent>
  314. </Card>
  315. {/* Status Details (when running) */}
  316. {status && isRunning && (
  317. <Card>
  318. <CardHeader>
  319. <h3 className="text-md font-semibold text-white">Status Details</h3>
  320. </CardHeader>
  321. <CardContent>
  322. <div className="grid grid-cols-2 gap-4 text-sm">
  323. <div>
  324. <div className="text-bambu-gray">Printer Name</div>
  325. <div className="text-white">{status.name}</div>
  326. </div>
  327. <div>
  328. <div className="text-bambu-gray">Model</div>
  329. <div className="text-white">{status.model_name || status.model}</div>
  330. </div>
  331. <div>
  332. <div className="text-bambu-gray">Serial Number</div>
  333. <div className="text-white font-mono">{status.serial}</div>
  334. </div>
  335. <div>
  336. <div className="text-bambu-gray">Mode</div>
  337. <div className="text-white capitalize">{status.mode}</div>
  338. </div>
  339. <div>
  340. <div className="text-bambu-gray">Pending Files</div>
  341. <div className="text-white">{status.pending_files}</div>
  342. </div>
  343. </div>
  344. </CardContent>
  345. </Card>
  346. )}
  347. </div>
  348. </div>
  349. );
  350. }