2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

[PUI] Url fixes (#8615)

* Refactor URL generation

- Use built-in URL function
- Refactor "admin" button
- Refactor API image
- Refactor printing actions
- Refactor attachment link

* Refactor URL generation for icon packs
This commit is contained in:
Oliver 2024-12-02 18:49:15 +11:00 committed by GitHub
parent 147ca53629
commit 21cd285599
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 16 additions and 17 deletions

View File

@ -63,7 +63,10 @@ export default function AdminButton(props: Readonly<AdminButtonProps>) {
} }
// Generate the URL for the admin interface // Generate the URL for the admin interface
const url = `${host}/${server.server.django_admin}${modelDef.admin_url}${props.id}/`; const url = new URL(
`${server.server.django_admin}${modelDef.admin_url}${props.id}/`,
host
);
if (event?.ctrlKey || event?.shiftKey) { if (event?.ctrlKey || event?.shiftKey) {
// Open the link in a new tab // Open the link in a new tab

View File

@ -116,8 +116,8 @@ export function PrintingActions({
if (response.output) { if (response.output) {
// An output file was generated // An output file was generated
const url = `${host}${response.output}`; const url = new URL(response.output, host);
window.open(url, '_blank'); window.open(url.toString(), '_blank');
} }
} }
}); });
@ -154,8 +154,8 @@ export function PrintingActions({
if (response.output) { if (response.output) {
// An output file was generated // An output file was generated
const url = `${host}${response.output}`; const url = new URL(response.output, host);
window.open(url, '_blank'); window.open(url.toString(), '_blank');
} }
} }
}); });

View File

@ -19,7 +19,7 @@ export function ApiImage(props: Readonly<ApiImageProps>) {
const { host } = useLocalState.getState(); const { host } = useLocalState.getState();
const imageUrl = useMemo(() => { const imageUrl = useMemo(() => {
return `${host}${props.src}`; return new URL(props.src, host).toString();
}, [host, props.src]); }, [host, props.src]);
return ( return (

View File

@ -68,7 +68,8 @@ export function AttachmentLink({
return attachment; return attachment;
} }
return `${host}${attachment}`; const u = new URL(attachment, host);
return u.toString();
}, [host, attachment, external]); }, [host, attachment, external]);
return ( return (

View File

@ -137,7 +137,7 @@ export function AboutInvenTreeModal({
{ {
ref: 'api', ref: 'api',
title: <Trans>API Version</Trans>, title: <Trans>API Version</Trans>,
link: `${host}api-doc/`, link: new URL('/api-doc/', host).toString(),
copy: true copy: true
}, },
{ {

View File

@ -13,14 +13,11 @@ export async function loadExternalPluginSource(source: string) {
return null; return null;
} }
// If the source is a relative URL, prefix it with the host URL const url = new URL(source, host).toString();
if (source.startsWith('/')) {
source = `${host}${source}`;
}
const module = await import(/* @vite-ignore */ source) const module = await import(/* @vite-ignore */ url)
.catch((error) => { .catch((error) => {
console.error(`ERR: Failed to load plugin from ${source}:`, error); console.error(`ERR: Failed to load plugin from ${url}:`, error);
return null; return null;
}) })
.then((module) => { .then((module) => {

View File

@ -44,9 +44,7 @@ export const useIconState = create<IconState>()((set, get) => ({
const src = Object.entries(pack.fonts as Record<string, string>) const src = Object.entries(pack.fonts as Record<string, string>)
.map( .map(
([format, url]) => ([format, url]) =>
`url(${ `url(${new URL(url, host).toString()}) format("${format}")`
url.startsWith('/') ? host + url : url
}) format("${format}")`
) )
.join(',\n'); .join(',\n');
const font = new FontFace(fontName, `${src};`); const font = new FontFace(fontName, `${src};`);