0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00

Improved null handling on Admin X actions (#18346)

refs https://ghost.slack.com/archives/C0568LN2CGJ/p1695642553045059

- The previous commit didn't quite handle things as expected. This adds
more null handling.
---

<!-- Leave the line below if you'd like GitHub Copilot to generate a
summary from your commit -->
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 1a69211</samp>

This pull request improves the robustness and accuracy of the admin
settings API for actions. It adds checks and logic to handle missing or
invalid data and custom post types in `actions.ts`.
This commit is contained in:
Ronald Langeveld 2023-09-26 13:58:35 +07:00 committed by GitHub
parent 677c829c4b
commit 1f2f0a7322
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -110,8 +110,19 @@ export const getActorLinkTarget = (action: Action): InternalLink | ExternalLink
};
export const getLinkTarget = (action: Action): InternalLink | ExternalLink | undefined => {
if (!action.resource_type || !action.event || !action.resource) {
return;
}
let resourceType = action.resource_type;
const contextExists = action.context !== null;
if (resourceType === 'post' && contextExists) {
if (action.context?.type) {
resourceType = action.context?.type as string;
}
}
if (action.event !== 'deleted') {
switch (action.resource_type) {
case 'page':
@ -172,6 +183,9 @@ export const getLinkTarget = (action: Action): InternalLink | ExternalLink | und
};
export const getActionTitle = (action: Action) => {
if (!action.resource_type || !action.event) {
return '';
}
let resourceType = action.resource_type;
if (resourceType === 'api_key') {
@ -185,7 +199,7 @@ export const getActionTitle = (action: Action) => {
const contextExists = action.context !== null;
if (resourceType === 'post' && contextExists) {
if (action.context.type) {
if (action.context?.type) {
resourceType = action.context.type as string;
}
}
@ -193,12 +207,12 @@ export const getActionTitle = (action: Action) => {
let actionName = action.event;
if (action.event === 'edited' && contextExists) {
if (action.context.action_name) {
if (action.context?.action_name) {
actionName = action.context.action_name as string;
}
}
if (contextExists && action.context.count && (action.context.count as number) > 1) {
if (contextExists && action.context?.count && (action.context.count as number) > 1) {
return `${action.context.count} ${resourceType}s ${actionName}`;
}
@ -206,14 +220,13 @@ export const getActionTitle = (action: Action) => {
};
export const getContextResource = (action: Action) => {
if (action.resource_type === 'setting') {
if (action.context?.group && action.context?.key) {
return {
group: action.context.group as string,
key: action.context.key as string
};
}
if (action.resource_type === 'setting' && action.context && action.context?.group && action.context?.key) {
return {
group: action.context.group as string,
key: action.context.key as string
};
}
};
export const isBulkAction = (action: Action) => typeof action.context.count === 'number' && action.context.count > 1;
export const isBulkAction = (action: Action) => {
return action.context !== null && typeof action.context?.count === 'number' && action.context.count > 1;
};