-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add redirect util function for project custom actions #1865
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||||||||||
import { logger } from '../../utils/logger'; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Extracts the redirect URL from request headers for AdminJS actions | ||||||||||||||
* @param request - The AdminJS action request object | ||||||||||||||
* @param resourceName - The name of the resource (e.g., 'Project', 'User') | ||||||||||||||
* @returns The URL to redirect to after action completion | ||||||||||||||
*/ | ||||||||||||||
export const getRedirectUrl = (request: any, resourceName: string): string => { | ||||||||||||||
const refererIndex = | ||||||||||||||
request?.rawHeaders?.findIndex(h => h.toLowerCase() === 'referer') || -1; | ||||||||||||||
const referrerUrl = | ||||||||||||||
refererIndex !== -1 ? request.rawHeaders[refererIndex + 1] : false; | ||||||||||||||
|
||||||||||||||
// Default fallback URL if no referer is found | ||||||||||||||
const defaultUrl = `/admin/resources/${resourceName}`; | ||||||||||||||
|
||||||||||||||
try { | ||||||||||||||
if (referrerUrl) { | ||||||||||||||
const url = new URL(referrerUrl); | ||||||||||||||
// If it's the main list view (no search params), add a timestamp to force refresh | ||||||||||||||
if (url.pathname === `/admin/resources/${resourceName}` && !url.search) { | ||||||||||||||
return `${url.pathname}?timestamp=${Date.now()}`; | ||||||||||||||
} | ||||||||||||||
Comment on lines
+22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider using higher precision timestamp. Using - return `${url.pathname}?timestamp=${Date.now()}`;
+ return `${url.pathname}?timestamp=${Date.now()}.${performance.now().toString().replace('.', '')}`; 📝 Committable suggestion
Suggested change
|
||||||||||||||
return url.pathname + url.search; | ||||||||||||||
} | ||||||||||||||
} catch (error) { | ||||||||||||||
logger.error('Error parsing referrer URL:', error); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Add timestamp to default URL as well | ||||||||||||||
return `${defaultUrl}?timestamp=${Date.now()}`; | ||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improve referrer URL extraction logic.
The current implementation has potential issues with falsy values and type coercion.
📝 Committable suggestion