Skip to content

Commit

Permalink
[PUI] Url fixes (#8615)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SchrodingersGat authored Dec 2, 2024
1 parent 147ca53 commit 21cd285
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/frontend/src/components/buttons/AdminButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export default function AdminButton(props: Readonly<AdminButtonProps>) {
}

// 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) {
// Open the link in a new tab
Expand Down
8 changes: 4 additions & 4 deletions src/frontend/src/components/buttons/PrintingActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export function PrintingActions({

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

if (response.output) {
// An output file was generated
const url = `${host}${response.output}`;
window.open(url, '_blank');
const url = new URL(response.output, host);
window.open(url.toString(), '_blank');
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/images/ApiImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function ApiImage(props: Readonly<ApiImageProps>) {
const { host } = useLocalState.getState();

const imageUrl = useMemo(() => {
return `${host}${props.src}`;
return new URL(props.src, host).toString();
}, [host, props.src]);

return (
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/components/items/AttachmentLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export function AttachmentLink({
return attachment;
}

return `${host}${attachment}`;
const u = new URL(attachment, host);
return u.toString();
}, [host, attachment, external]);

return (
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/components/modals/AboutInvenTreeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export function AboutInvenTreeModal({
{
ref: 'api',
title: <Trans>API Version</Trans>,
link: `${host}api-doc/`,
link: new URL('/api-doc/', host).toString(),
copy: true
},
{
Expand Down
9 changes: 3 additions & 6 deletions src/frontend/src/components/plugins/PluginSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ export async function loadExternalPluginSource(source: string) {
return null;
}

// If the source is a relative URL, prefix it with the host URL
if (source.startsWith('/')) {
source = `${host}${source}`;
}
const url = new URL(source, host).toString();

const module = await import(/* @vite-ignore */ source)
const module = await import(/* @vite-ignore */ url)
.catch((error) => {
console.error(`ERR: Failed to load plugin from ${source}:`, error);
console.error(`ERR: Failed to load plugin from ${url}:`, error);
return null;
})
.then((module) => {
Expand Down
4 changes: 1 addition & 3 deletions src/frontend/src/states/IconState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ export const useIconState = create<IconState>()((set, get) => ({
const src = Object.entries(pack.fonts as Record<string, string>)
.map(
([format, url]) =>
`url(${
url.startsWith('/') ? host + url : url
}) format("${format}")`
`url(${new URL(url, host).toString()}) format("${format}")`
)
.join(',\n');
const font = new FontFace(fontName, `${src};`);
Expand Down

0 comments on commit 21cd285

Please sign in to comment.