GeezerVison is showing github.com — feat: the designer the server serves prints through the server and re… · u8array/ZPLab@364fcf6
File treeExpand file treeCollapse file tree Open diff view settings Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/components/Output/PrinterChip.tsx +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -45,9 +45,10 @@ export function PrinterChip() { | 45 | 45 | const settings = useReplyRead<SgdSetting[]>(); | 46 | 46 |
| 47 | 47 | const resolved = resolveQueryTarget(printTarget); | 48 | | - const name = "failure" in resolved ? t.zebraPrint.printer : resolved.target.kind === "usb" ? t.zebraPrint.tabUsb : resolved.target.host; | | 48 | + const target = "failure" in resolved ? undefined : resolved.target; | | 49 | + const name = target === undefined ? t.zebraPrint.printer : target.kind === "network" ? target.host : target.kind === "usb" ? t.zebraPrint.tabUsb : target.id; | 49 | 50 | // The stored way, because the effective one would enumerate printers on every mount. | 50 | | - const askedOverAddress = !("failure" in resolved) && resolved.target.kind === "network" && printTarget.transport !== "network"; | | 51 | + const askedOverAddress = target?.kind === "network" && printTarget.transport !== "network"; | 51 | 52 | const view = reportView(loc, state.phase === "done" ? state.report : undefined); | 52 | 53 | const reading = step !== undefined; | 53 | 54 | const tone: ReadinessTone = state.phase === "failed" ? "blocked" : (view?.line.tone ?? "unknown"); | |
Collapse file Copy file name to clipboardExpand all lines: src/components/Output/RendererSelect.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { useId } from "react"; | 2 | 2 | import { useT } from "../../hooks/useT"; | 3 | 3 | import { formatTemplate } from "../../lib/formatTemplate"; | 4 | 4 | import { previewProviderChoices, previewProviderLabel } from "../../lib/previewProviderLabel"; | | 5 | +import { printReach } from "../../lib/printReach"; | 5 | 6 | import { selectEffectivePreviewProvider, selectReadsPrinter, useLabelStore } from "../../store/labelStore"; | 6 | 7 | import type { PreviewProvider } from "../../store/slices/uiSlice"; | 7 | 8 | import { sectionHeadingCls } from "../ui/formStyles"; | @@ -16,7 +17,7 @@ export function RendererSelect() { | 16 | 17 | const labelaryAvailable = useLabelStore((s) => s.thirdParty.labelary); | 17 | 18 | const readsPrinter = useLabelStore(selectReadsPrinter); | 18 | 19 | const setProvider = useLabelStore((s) => s.setPreviewProvider); | 19 | | - const choices = previewProviderChoices(t, { labelary: labelaryAvailable, printer: readsPrinter }); | | 20 | + const choices = previewProviderChoices(t, { labelary: labelaryAvailable, printer: readsPrinter }, printReach); | 20 | 21 | return ( | 21 | 22 | <div className="flex flex-col gap-1"> | 22 | 23 | <label htmlFor={id} className={sectionHeadingCls}> | |
Collapse file Copy file name to clipboardExpand all lines: src/components/Output/SystemPrintBody.tsx +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { useT } from "../../hooks/useT"; | 2 | 2 | import { queryOffer } from "../../lib/printWayText"; | 3 | 3 | import type { DocumentQueryFacts } from "../../lib/outputChoice"; | 4 | | -import { isDesktopShell } from "../../lib/platform"; | | 4 | +import { printReach } from "../../lib/printReach"; | 5 | 5 | import { primaryButtonCls } from "../ui/formStyles"; | 6 | 6 | import { Tooltip } from "../ui/Tooltip"; | 7 | 7 | import { QueryHints } from "./QueryHints"; | @@ -24,7 +24,7 @@ export function SystemPrintBody({ onPrint, rendering, locked, query }: Props) { | 24 | 24 | <p aria-live="polite" className="font-mono text-[10px] text-muted min-h-4"> | 25 | 25 | {rendering ? loc.systemRendering : ""} | 26 | 26 | </p> | 27 | | - <QueryHints hints={queryOffer(loc, query, "system", isDesktopShell).hints} /> | | 27 | + <QueryHints hints={queryOffer(loc, query, "system", printReach).hints} /> | 28 | 28 | <div className="flex justify-end"> | 29 | 29 | <Tooltip content={locked ? t.printerSettings.printerStatus.failBusy : undefined}> | 30 | 30 | <button onClick={onPrint} disabled={rendering || locked} className={primaryButtonCls}> | |
Collapse file Copy file name to clipboard +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @vitest-environment jsdom | | 2 | +import { describe, it, expect, afterEach, beforeEach, vi } from "vitest"; | | 3 | +import { render, cleanup, act, fireEvent, waitFor } from "@testing-library/react"; | | 4 | +import type * as ServerPrint from "../../lib/serverPrint"; | | 5 | + | | 6 | +const listServerPrinters = vi.hoisted(() => vi.fn()); | | 7 | +const sendZplServer = vi.hoisted(() => vi.fn()); | | 8 | +vi.mock("../../lib/printReach", () => ({ printReach: { desktop: false, server: true }, serverApiBase: "/api/v1" })); | | 9 | +vi.mock("../../lib/serverPrint", async (importOriginal) => ({ ...(await importOriginal<typeof ServerPrint>()), listServerPrinters, sendZplServer })); | | 10 | + | | 11 | +import { WayPanel } from "./ZebraWayPanel.testkit"; | | 12 | +import { useLabelStore } from "../../store/labelStore"; | | 13 | +import { DEFAULT_PRINT_TARGET } from "../../lib/printTarget"; | | 14 | +import { fallbackTranslations as en } from "../../locales"; | | 15 | + | | 16 | +afterEach(cleanup); | | 17 | + | | 18 | +beforeEach(() => { | | 19 | + listServerPrinters.mockReset(); | | 20 | + sendZplServer.mockReset(); | | 21 | + act(() => { | | 22 | + useLabelStore.setState({ dataset: null, columnMapping: null, printTarget: { ...DEFAULT_PRINT_TARGET, transport: "server" } }); | | 23 | + }); | | 24 | +}); | | 25 | + | | 26 | +describe("ZebraWayPanel over the server", () => { | | 27 | + it("sends to the printer the server lists and words a failure from the locale", async () => { | | 28 | + listServerPrinters.mockResolvedValue({ kind: "ok", printers: [{ id: "dock", name: "Dock 3" }] }); | | 29 | + sendZplServer.mockResolvedValueOnce({ kind: "sent" }).mockResolvedValueOnce({ kind: "server_unreachable" }); | | 30 | + const r = render(<WayPanel way="server" zpl={() => "^XA^XZ"} />); | | 31 | + await waitFor(() => expect((r.getByText(en.zebraPrint.send) as HTMLButtonElement).disabled).toBe(false)); | | 32 | + await act(async () => { | | 33 | + fireEvent.click(r.getByText(en.zebraPrint.send)); | | 34 | + }); | | 35 | + expect(sendZplServer).toHaveBeenCalledWith("dock", "^XA^XZ"); | | 36 | + expect(r.getByText(en.zebraPrint.success)).toBeTruthy(); | | 37 | + await act(async () => { | | 38 | + fireEvent.click(r.getByText(en.zebraPrint.send)); | | 39 | + }); | | 40 | + expect(r.getByText(en.printerSettings.printerStatus.failServerUnreachable)).toBeTruthy(); | | 41 | + }); | | 42 | +}); |
Collapse file Copy file name to clipboardExpand all lines: src/components/Output/ZebraWayPanel.tsx +29-7Lines changed: 29 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,13 +4,15 @@ import { useT } from "../../hooks/useT"; | 4 | 4 | import { Select } from "../ui/Select"; | 5 | 5 | import { primaryButtonCls } from "../ui/formStyles"; | 6 | 6 | import { sendViaBrowserPrint, sendViaNetwork } from "../../lib/zebraPrint"; | 7 | | -import { isDesktopShell } from "../../lib/platform"; | | 7 | +import { printReach } from "../../lib/printReach"; | 8 | 8 | import { needsRawChannel, readsPrinter, resolveQueryTarget, type PrintTransport } from "../../lib/printTarget"; | | 9 | +import { failureText } from "../../lib/printerStatusText"; | 9 | 10 | import { queryButtonText, queryOffer } from "../../lib/printWayText"; | 10 | 11 | import { QueryHints } from "./QueryHints"; | 11 | 12 | import type { DocumentQueryFacts } from "../../lib/outputChoice"; | 12 | 13 | import { useLabelStore, selectEffectivePreviewProvider, selectPrinterReading } from "../../store/labelStore"; | 13 | 14 | import { sendZplLocal } from "../../lib/localPrint"; | | 15 | +import { sendZplServer } from "../../lib/serverPrint"; | 14 | 16 | import { sendZplUsb, setupUsbAccess } from "../../lib/usbPrint"; | 15 | 17 | import { pickerOptions } from "../../hooks/usePrintDevices"; | 16 | 18 | import type { PrintDeviceLists } from "../../hooks/usePrintWays"; | @@ -31,7 +33,7 @@ function StatusMessage({ status }: { status: Status }) { | 31 | 33 | ); | 32 | 34 | } | 33 | 35 |
| 34 | | -// Browser print, the OS spooler and USB share this body, because each picks from a device list. | | 36 | +// Browser print, the OS spooler, USB and the server share this body, because each picks from a device list. | 35 | 37 | interface TransportView { | 36 | 38 | key: PrintTransport; | 37 | 39 | selected: string; | @@ -76,10 +78,11 @@ export function ZebraWayPanel({ zpl, way, devices, query }: { zpl: () => string; | 76 | 78 | const locked = checking && needsRawChannel(way, renderer); | 77 | 79 | const [netStatus, setNetStatus] = useState<Status>({ type: "idle" }); | 78 | 80 |
| 79 | | - const { bp, local, usb } = devices; | | 81 | + const { bp, local, usb, server } = devices; | 80 | 82 | const [bpStatus, setBpStatus] = useState<Status>({ type: "idle" }); | 81 | 83 | const [localStatus, setLocalStatus] = useState<Status>({ type: "idle" }); | 82 | 84 | const [usbStatus, setUsbStatus] = useState<Status>({ type: "idle" }); | | 85 | + const [serverStatus, setServerStatus] = useState<Status>({ type: "idle" }); | 83 | 86 | // Tracks the last USB send being permission-denied, so the setup affordance | 84 | 87 | // survives locale changes and a cancelled polkit prompt. | 85 | 88 | const [usbNeedsSetup, setUsbNeedsSetup] = useState(false); | @@ -89,6 +92,7 @@ export function ZebraWayPanel({ zpl, way, devices, query }: { zpl: () => string; | 89 | 92 | const usbViewStatus = withListError(usbStatus, usb.error); | 90 | 93 | const localViewStatus = withListError(localStatus, local.error); | 91 | 94 | const bpViewStatus = withListError(bpStatus, bp.error && t.zebraPrint.agentNotFound); | | 95 | + const serverViewStatus = withListError(serverStatus, server.error && failureText(t.printerSettings.printerStatus, server.error)); | 92 | 96 |
| 93 | 97 | async function handleNetworkSend() { | 94 | 98 | const target = useLabelStore.getState().printTarget; | @@ -181,6 +185,13 @@ export function ZebraWayPanel({ zpl, way, devices, query }: { zpl: () => string; | 181 | 185 | } | 182 | 186 | } | 183 | 187 |
| | 188 | + async function handleServerSend() { | | 189 | + if (!server.selectedId) return; | | 190 | + setServerStatus({ type: "sending" }); | | 191 | + const result = await sendZplServer(server.selectedId, zpl()); | | 192 | + setServerStatus(result.kind === "sent" ? { type: "success", message: t.zebraPrint.success } : { type: "error", message: failureText(t.printerSettings.printerStatus, result) }); | | 193 | + } | | 194 | + | 184 | 195 | async function handleUsbSetup() { | 185 | 196 | try { | 186 | 197 | await setupUsbAccess(); | @@ -201,10 +212,9 @@ export function ZebraWayPanel({ zpl, way, devices, query }: { zpl: () => string; | 201 | 212 | }; | 202 | 213 | const checkProps = (sending: boolean) => ({ transport: way, sendBusy: sending, onFailure: onReadFailure }); | 203 | 214 |
| 204 | | - // Every way reaches the same device, so every way may ask it, and a way that cannot be questioned | 205 | | - // itself falls back to the configured address. | | 215 | + // In the desktop shell every way reaches the same device, so a way without a query falls back to the configured address. | 206 | 216 | const addressCheck = (sending: boolean) => { | 207 | | - if (!readsPrinter(printTarget, isDesktopShell, way)) return null; | | 217 | + if (!readsPrinter(printTarget, printReach, way)) return null; | 208 | 218 | const resolved = resolveQueryTarget(printTarget, way); | 209 | 219 | if ("failure" in resolved) return null; | 210 | 220 | return ( | @@ -277,9 +287,21 @@ export function ZebraWayPanel({ zpl, way, devices, query }: { zpl: () => string; | 277 | 287 | addressCheck(usbStatus.type === "sending") | 278 | 288 | ), | 279 | 289 | }, | | 290 | + { | | 291 | + key: "server", | | 292 | + selected: server.selectedId, | | 293 | + onSelect: server.select, | | 294 | + options: pickerOptions(t, server), | | 295 | + selectDisabled: server.options.length === 0, | | 296 | + onSend: handleServerSend, | | 297 | + sendLabel: serverStatus.type === "sending" ? t.zebraPrint.sending : t.zebraPrint.send, | | 298 | + sendDisabled: !server.selectedId || server.options.length === 0 || server.loading || serverStatus.type === "sending" || locked, | | 299 | + status: serverViewStatus, | | 300 | + extra: addressCheck(serverStatus.type === "sending") ?? undefined, | | 301 | + }, | 280 | 302 | ]; | 281 | 303 | const activeView = views.find((v) => v.key === way); | 282 | | - const offer = queryOffer(t.zebraPrint, query, way, isDesktopShell); | | 304 | + const offer = queryOffer(t.zebraPrint, query, way, printReach); | 283 | 305 | const netSendDisabled = !host || netStatus.type === "sending" || locked; | 284 | 306 | const action = (send: ReactNode, sendDisabled: boolean): ReactNode => | 285 | 307 | offer.queries ? <DocumentQueryAction zpl={zpl} label={queryButtonText(t.zebraPrint, query)} transport={way} disabled={sendDisabled} onFailure={onReadFailure} /> : send; | |
Collapse file Copy file name to clipboardExpand all lines: src/components/PrinterSettings/PreviewSettingsTab.tsx +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,6 +7,7 @@ import { labelCls, inputCls, buttonCls, sectionHeadingCls } from "../ui/formStyl | 7 | 7 | import { RadioOption } from "../ui/RadioOption"; | 8 | 8 | import { formatTemplate } from "../../lib/formatTemplate"; | 9 | 9 | import { previewProviderChoices, previewProviderLabel } from "../../lib/previewProviderLabel"; | | 10 | +import { printReach } from "../../lib/printReach"; | 10 | 11 |
| 11 | 12 | /** Preview configuration: which renderer draws the overlay and the Labelary privacy consent. */ | 12 | 13 | export function PreviewSettingsTab() { | @@ -19,7 +20,9 @@ export function PreviewSettingsTab() { | 19 | 20 | const setProvider = useLabelStore((s) => s.setPreviewProvider); | 20 | 21 | const labelaryAvailable = useLabelStore((s) => s.thirdParty.labelary); | 21 | 22 | const readsPrinter = useLabelStore(selectReadsPrinter); | 22 | | - const locked = Object.fromEntries(previewProviderChoices(t, { labelary: labelaryAvailable, printer: readsPrinter }).map((c) => [c.provider, c.disabled])); | | 23 | + const choices = previewProviderChoices(t, { labelary: labelaryAvailable, printer: readsPrinter }, printReach); | | 24 | + const locked = Object.fromEntries(choices.map((c) => [c.provider, c.disabled])); | | 25 | + const printerLock = choices.find((c) => c.provider === "printer")?.reason; | 23 | 26 | const labelaryConsent = useLabelStore((s) => s.labelaryNoticeAcknowledged); | 24 | 27 | const acknowledgeLabelaryNotice = useLabelStore((s) => s.acknowledgeLabelaryNotice); | 25 | 28 | const revokeLabelaryNotice = useLabelStore((s) => s.revokeLabelaryNotice); | @@ -93,7 +96,7 @@ export function PreviewSettingsTab() { | 93 | 96 | current={chosen} | 94 | 97 | onSelect={setProvider} | 95 | 98 | label={loc.providerPrinter} | 96 | | - hint={readsPrinter ? loc.providerPrinterHint : loc.providerPrinterDesktopOnly} | | 99 | + hint={printerLock ?? loc.providerPrinterHint} | 97 | 100 | disabled={locked.printer} | 98 | 101 | /> | 99 | 102 | <RadioOption | |
Collapse file Copy file name to clipboard +62Lines changed: 62 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @vitest-environment jsdom | | 2 | +import { describe, it, expect, afterEach, beforeEach, vi } from "vitest"; | | 3 | +import { render, cleanup, fireEvent, act, waitFor } from "@testing-library/react"; | | 4 | +import type * as ServerPrint from "../../lib/serverPrint"; | | 5 | + | | 6 | +const listServerPrinters = vi.hoisted(() => vi.fn()); | | 7 | +const saveServerToken = vi.hoisted(() => vi.fn()); | | 8 | +vi.mock("../../lib/printReach", () => ({ printReach: { desktop: false, server: true }, serverApiBase: "/api/v1" })); | | 9 | +vi.mock("../../lib/serverPrint", async (importOriginal) => ({ ...(await importOriginal<typeof ServerPrint>()), listServerPrinters, saveServerToken })); | | 10 | + | | 11 | +import { PrintTargetTab } from "./PrintTargetTab"; | | 12 | +import { useLabelStore } from "../../store/labelStore"; | | 13 | +import { DEFAULT_PRINT_TARGET } from "../../lib/printTarget"; | | 14 | +import { fallbackTranslations as en } from "../../locales"; | | 15 | + | | 16 | +const loc = en.printerSettings.printTarget; | | 17 | +const DOCK = { kind: "ok", printers: [{ id: "dock", name: "Dock 3" }] }; | | 18 | + | | 19 | +afterEach(cleanup); | | 20 | + | | 21 | +beforeEach(() => { | | 22 | + listServerPrinters.mockReset(); | | 23 | + saveServerToken.mockReset(); | | 24 | + saveServerToken.mockResolvedValue(undefined); | | 25 | + act(() => { | | 26 | + useLabelStore.setState({ printTarget: { ...DEFAULT_PRINT_TARGET, transport: "server" } }); | | 27 | + }); | | 28 | +}); | | 29 | + | | 30 | +describe("PrintTargetTab in the build the server serves", () => { | | 31 | + it("labels the token field alone and leaves out the address the server keeps", async () => { | | 32 | + listServerPrinters.mockResolvedValue(DOCK); | | 33 | + const r = render(<PrintTargetTab />); | | 34 | + await waitFor(() => expect(useLabelStore.getState().printTarget.serverPrinter).toBe("dock")); | | 35 | + expect((r.getByLabelText(loc.serverToken) as HTMLInputElement).type).toBe("password"); | | 36 | + expect(r.queryByPlaceholderText("192.168.1.100")).toBeNull(); | | 37 | + expect(r.queryByText(loc.hint)).toBeNull(); | | 38 | + }); | | 39 | + | | 40 | + it("stores a typed token, empties the field and lists the printers again", async () => { | | 41 | + listServerPrinters.mockResolvedValueOnce({ kind: "unauthorized" }).mockResolvedValue(DOCK); | | 42 | + const r = render(<PrintTargetTab />); | | 43 | + await waitFor(() => expect(r.getByText(en.printerSettings.printerStatus.failUnauthorized)).toBeTruthy()); | | 44 | + const field = r.getByLabelText(loc.serverToken) as HTMLInputElement; | | 45 | + fireEvent.change(field, { target: { value: "secret" } }); | | 46 | + await act(async () => { | | 47 | + fireEvent.click(r.getByText(loc.serverTokenSave)); | | 48 | + }); | | 49 | + expect(saveServerToken).toHaveBeenCalledWith("secret"); | | 50 | + expect(field.value).toBe(""); | | 51 | + await waitFor(() => expect(r.queryByText(en.printerSettings.printerStatus.failUnauthorized)).toBeNull()); | | 52 | + expect(listServerPrinters).toHaveBeenCalledTimes(2); | | 53 | + }); | | 54 | + | | 55 | + it("keeps the server way listed with its reason while the server names no printer", async () => { | | 56 | + listServerPrinters.mockResolvedValue({ kind: "ok", printers: [] }); | | 57 | + const r = render(<PrintTargetTab />); | | 58 | + await waitFor(() => expect(r.getByText(en.zebraPrint.noPrinters)).toBeTruthy()); | | 59 | + expect((r.getByLabelText(en.zebraPrint.tabServer) as HTMLInputElement).disabled).toBe(true); | | 60 | + expect((r.getByLabelText(en.zebraPrint.tabNetwork) as HTMLInputElement).checked).toBe(true); | | 61 | + }); | | 62 | +}); |
|