import { environment } from '../environments/environment'; import { isHostedMount } from './hostedMount'; /** * Where a bundled static file lives, on whichever target is running. * * WordPress serves these out of the plugin directory and tells the client where that is; a hosted page * serves them beside itself. The same file, two addresses, and no call site should have to know which. * * TAKES THE FILE, NOT A PREFIX, and that is what makes it correct rather than tidy. The six call sites * this replaces each built their own path, and they had already drifted: the practitioner avatar wrote * `${pluginUrl}/assets/...` and the other three wrote `${pluginUrl}assets/...`. With an empty plugin * URL those are `/assets/...` and `assets/...` - root-absolute and relative, the same only when the app * is at the root of its origin. * * Nothing here spells a host. The hosted build copies `assets/` to the root of its output, so a bare * filename is the whole address; the plugin keeps its directory. */ export const assetUrl = (file: string): string => { const name = file.replace(/^\/+/, ''); // Root-absolute, not bare: a bare name resolves against the current path, so it is correct only // at the root of the origin and breaks on every deeper one, the staff portal included. if (isHostedMount()) return `/${name}`; const pluginUrl = environment.wpData?.pluginUrl ?? ''; // The plugin URL is stored with a trailing slash today. Normalised rather than assumed, because a // missing one silently glues the directory to the filename and the image simply does not appear. return `${pluginUrl.replace(/\/+$/, '')}/assets/${name}`; };