Refactor Labelary preview to support custom hosts#54
Conversation
Update the Labelary preview functionality to allow for custom API hosts and keys, supporting private Labelary instances. This includes changes to the store, locale strings, and the addition of a `isDefaultLabelaryHost` utility function. The privacy notice link has also been updated to point to Labelary's service and pricing information, rather than just privacy.
There was a problem hiding this comment.
Code Review
This pull request introduces support for custom Labelary API endpoints and API keys via environment variables, allowing the application to interface with private or metered Labelary instances. The UI has been updated to conditionally display privacy notices and attribution links only when the default public service is used. Feedback suggests optimizing performance by computing the API host and key at the module level rather than on every function call, which is particularly beneficial for frequently executed state selectors.
| function host(): string { | ||
| const configured = trimmed(import.meta.env.VITE_LABELARY_API_URL); | ||
| if (!configured) return DEFAULT_HOST; | ||
| return configured.replace(/\/+$/, ''); | ||
| } | ||
|
|
||
| function apiKey(): string | undefined { | ||
| return trimmed(import.meta.env.VITE_LABELARY_API_KEY); | ||
| } |
There was a problem hiding this comment.
The host() and apiKey() functions perform string operations and environment variable lookups on every call. Since these values are derived from build-time environment variables and are constant during the application's lifecycle, it is more efficient to compute them once at the module level. This is particularly relevant as host() is called via the selectLabelaryNoticeRequired Zustand selector, which may execute frequently during UI interactions (e.g., when dragging objects on the canvas).
| function host(): string { | |
| const configured = trimmed(import.meta.env.VITE_LABELARY_API_URL); | |
| if (!configured) return DEFAULT_HOST; | |
| return configured.replace(/\/+$/, ''); | |
| } | |
| function apiKey(): string | undefined { | |
| return trimmed(import.meta.env.VITE_LABELARY_API_KEY); | |
| } | |
| const API_HOST = trimmed(import.meta.env.VITE_LABELARY_API_URL)?.replace(/\/+$/, '') ?? DEFAULT_HOST; | |
| const API_KEY = trimmed(import.meta.env.VITE_LABELARY_API_KEY); | |
| function host(): string { return API_HOST; } | |
| function apiKey(): string | undefined { return API_KEY; } |
No description provided.