Skip to content

Commit 6e3717d

Browse files
committed
fix: skip print dialog when only one template exists
1 parent 9dd1e21 commit 6e3717d

File tree

1 file changed

+50
-86
lines changed

1 file changed

+50
-86
lines changed

src/renderer/src/components/print/PrintDialog.tsx

Lines changed: 50 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
DialogDescription,
1111
DialogFooter,
1212
DialogHeader,
13-
DialogTitle,
14-
DialogTrigger
13+
DialogTitle
1514
} from '@renderer/components/ui/dialog'
1615
import {
1716
DropdownMenu,
@@ -39,14 +38,12 @@ export const PrintDialog = ({
3938
const [selectedTemplate, setSelectedTemplate] = useState<PrintTemplate | null>(null)
4039
const [isPrinting, setIsPrinting] = useState(false)
4140

42-
// Fetch all templates of this type
43-
const { data: templatesData } = useQuery({
44-
...queries.printTemplates.list({ type: templateType }),
45-
enabled: open
41+
// Always fetch templates so we know the count before user clicks
42+
const { data: templatesData, isLoading } = useQuery({
43+
...queries.printTemplates.list({ type: templateType })
4644
})
4745

4846
const templates = useMemo(() => templatesData?.data || [], [templatesData?.data])
49-
const hasMultipleTemplates = templates.length > 1
5047

5148
// Set default template when templates load
5249
useEffect(() => {
@@ -56,14 +53,12 @@ export const PrintDialog = ({
5653
}
5754
}, [templates, selectedTemplate])
5855

59-
const handlePrint = async () => {
60-
if (!selectedTemplate) return
61-
56+
const printWithTemplate = async (template: PrintTemplate) => {
6257
setIsPrinting(true)
6358
try {
6459
await window.electronApi.openPrintDialog({
6560
title,
66-
templateStructure: selectedTemplate.structure,
61+
templateStructure: template.structure,
6762
templateContext: context
6863
})
6964
onPrint?.()
@@ -73,33 +68,29 @@ export const PrintDialog = ({
7368
}
7469
}
7570

76-
// If there's only one template (or none), print directly without dialog
71+
const handlePrint = async () => {
72+
if (!selectedTemplate) return
73+
await printWithTemplate(selectedTemplate)
74+
}
75+
7776
const handleTriggerClick = async () => {
78-
// Query templates first if not loaded
79-
if (!templatesData) {
77+
// If still loading, open dialog to show loading state
78+
if (isLoading) {
8079
setOpen(true)
8180
return
8281
}
8382

83+
// If only one template (or none), print directly
8484
if (templates.length <= 1) {
85-
// Direct print without dialog
8685
const template = templates[0]
8786
if (template) {
88-
setIsPrinting(true)
89-
try {
90-
await window.electronApi.openPrintDialog({
91-
title,
92-
templateStructure: template.structure,
93-
templateContext: context
94-
})
95-
onPrint?.()
96-
} finally {
97-
setIsPrinting(false)
98-
}
87+
await printWithTemplate(template)
9988
}
100-
} else {
101-
setOpen(true)
89+
return
10290
}
91+
92+
// Multiple templates - show dialog
93+
setOpen(true)
10394
}
10495

10596
const defaultTrigger = (
@@ -109,31 +100,24 @@ export const PrintDialog = ({
109100
</Button>
110101
)
111102

112-
// If we haven't loaded templates yet, show dialog to load them
113-
// Otherwise, only show dialog if multiple templates exist
114103
return (
115-
<Dialog open={open} onOpenChange={setOpen}>
116-
<DialogTrigger asChild onClick={(e) => {
117-
e.preventDefault()
118-
handleTriggerClick()
119-
}}>
104+
<>
105+
<div onClick={handleTriggerClick}>
120106
{trigger || defaultTrigger}
121-
</DialogTrigger>
122-
<DialogContent className="sm:max-w-md">
123-
<DialogHeader>
124-
<DialogTitle className="flex items-center gap-2">
125-
<Printer className="h-5 w-5" />
126-
Print {templateType === 'surgery' ? 'Surgery Note' : 'Follow-up Note'}
127-
</DialogTitle>
128-
<DialogDescription>
129-
{hasMultipleTemplates
130-
? 'Select a template for printing this record.'
131-
: 'Click Print to generate the document.'}
132-
</DialogDescription>
133-
</DialogHeader>
107+
</div>
108+
<Dialog open={open} onOpenChange={setOpen}>
109+
<DialogContent className="sm:max-w-md">
110+
<DialogHeader>
111+
<DialogTitle className="flex items-center gap-2">
112+
<Printer className="h-5 w-5" />
113+
Print {templateType === 'surgery' ? 'Surgery Note' : 'Follow-up Note'}
114+
</DialogTitle>
115+
<DialogDescription>
116+
Select a template for printing this record.
117+
</DialogDescription>
118+
</DialogHeader>
134119

135-
<div className="py-4">
136-
{hasMultipleTemplates && (
120+
<div className="py-4">
137121
<div className="space-y-2">
138122
<label className="text-sm font-medium text-muted-foreground">
139123
Template
@@ -186,42 +170,22 @@ export const PrintDialog = ({
186170
</DropdownMenuContent>
187171
</DropdownMenu>
188172
</div>
189-
)}
190-
191-
{!hasMultipleTemplates && templates.length === 1 && (
192-
<div className="flex items-center gap-3 p-3 rounded-lg bg-muted/50">
193-
<FileText className="h-5 w-5 text-muted-foreground" />
194-
<div>
195-
<p className="text-sm font-medium">{templates[0].name}</p>
196-
{templates[0].description && (
197-
<p className="text-xs text-muted-foreground">{templates[0].description}</p>
198-
)}
199-
</div>
200-
</div>
201-
)}
202-
203-
{templates.length === 0 && (
204-
<div className="text-center py-6 text-muted-foreground">
205-
<FileText className="h-8 w-8 mx-auto mb-2 opacity-50" />
206-
<p className="text-sm">No templates available for this type.</p>
207-
<p className="text-xs">Create a template in Settings → Print Templates.</p>
208-
</div>
209-
)}
210-
</div>
173+
</div>
211174

212-
<DialogFooter>
213-
<Button variant="outline" onClick={() => setOpen(false)}>
214-
Cancel
215-
</Button>
216-
<Button
217-
onClick={handlePrint}
218-
disabled={!selectedTemplate || isPrinting}
219-
>
220-
<Printer className="h-4 w-4 mr-2" />
221-
{isPrinting ? 'Printing...' : 'Print'}
222-
</Button>
223-
</DialogFooter>
224-
</DialogContent>
225-
</Dialog>
175+
<DialogFooter>
176+
<Button variant="outline" onClick={() => setOpen(false)}>
177+
Cancel
178+
</Button>
179+
<Button
180+
onClick={handlePrint}
181+
disabled={!selectedTemplate || isPrinting}
182+
>
183+
<Printer className="h-4 w-4 mr-2" />
184+
{isPrinting ? 'Printing...' : 'Print'}
185+
</Button>
186+
</DialogFooter>
187+
</DialogContent>
188+
</Dialog>
189+
</>
226190
)
227191
}

0 commit comments

Comments
 (0)