From 11db41b900dccccc9f8432c7a38f8412c8197a65 Mon Sep 17 00:00:00 2001 From: James Collins Date: Fri, 29 Sep 2023 16:02:57 +1000 Subject: [PATCH] accept media or string --- resources/js/helpers/media.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/resources/js/helpers/media.ts b/resources/js/helpers/media.ts index ee16120..bcca2c9 100644 --- a/resources/js/helpers/media.ts +++ b/resources/js/helpers/media.ts @@ -26,15 +26,28 @@ export const mediaGetVariantUrl = ( : media.url; }; -export const mediaGetWebURL = (media: Media): string => { +/** + * Convert a Media URL to a user friendly URL + * @param {Media|string} mediaOrString Media object or URL string + * @returns {string} User friendly URL + */ +export const mediaGetWebURL = (mediaOrString: Media | string): string => { const webUrl = (import.meta as ImportMetaExtras).env.APP_URL; const apiUrl = (import.meta as ImportMetaExtras).env.APP_URL_API; - let url = media.url; + let url = + typeof mediaOrString === "string" + ? mediaOrString + : (mediaOrString as Media).url; - // Is the URL a API request? - if (media.url.startsWith(apiUrl)) { - const fileUrlPath = media.url.substring(apiUrl.length); + // If the input is a string, use it as the URL directly + if (typeof mediaOrString === "string") { + return url; + } + + // Is the URL an API request? + if (url.startsWith(apiUrl)) { + const fileUrlPath = url.substring(apiUrl.length); const fileUrlParts = fileUrlPath.split("/"); if (