Skip to content

Commit

Permalink
move immuatble formatter utils.tsx
Browse files Browse the repository at this point in the history
Signed-off-by: Atif Ali <[email protected]>
  • Loading branch information
aali309 committed Dec 19, 2024
1 parent e2d8e21 commit 93161a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,6 @@ const Filter = (props: {filters: string[]; setFilters: (f: string[]) => void; op

export const ApplicationOperationState: React.StatelessComponent<Props> = ({application, operationState}, ctx: AppContext) => {
const [messageFilters, setMessageFilters] = React.useState([]);
const getFormattedMessage = (message: string) => {
const prefix = 'one or more objects failed to apply, reason: ';
let cleanMessage = message;

// Remove duplicate prefix if exists
if (message.startsWith(prefix) && message.substring(prefix.length).startsWith(prefix)) {
cleanMessage = prefix + message.substring(prefix.length * 2);
}

// Format immutable fields error message
if (cleanMessage.includes('attempting to change immutable fields:')) {
const [header, ...details] = cleanMessage.split('\n');
const formattedDetails = details
.filter(line => line.trim())
.map(line => {
if (line.startsWith('-')) {
const [field, changes] = line.substring(2).split(':');
if (changes) {
const [from, to] = changes.split('to:').map(s => s.trim());
return ` - ${field}:\n from: ${from.replace('from:', '').trim()}\n to: ${to}`;
}
}
return line;
})
.join('\n');

return `${header}\n${formattedDetails}`;
}

return cleanMessage;
};

const operationAttributes = [
{title: 'OPERATION', value: utils.getOperationType(application)},
Expand All @@ -101,7 +70,7 @@ export const ApplicationOperationState: React.StatelessComponent<Props> = ({appl
margin: 0,
fontFamily: 'inherit'
}}>
{getFormattedMessage(operationState.message)}
{utils.formatOperationMessage(operationState.message)}
</pre>
)
}
Expand Down
30 changes: 30 additions & 0 deletions ui/src/app/applications/components/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,36 @@ export function formatCreationTimestamp(creationTimestamp: string) {
);
}

export function formatOperationMessage(message: string): string {
if (!message) {
return message;
}

let cleanMessage = message;

Check failure on line 1554 in ui/src/app/applications/components/utils.tsx

View workflow job for this annotation

GitHub Actions / Build, test & lint UI code

'cleanMessage' is never reassigned. Use 'const' instead

// Format immutable fields error message
if (cleanMessage.includes('attempting to change immutable fields:')) {
const [header, ...details] = cleanMessage.split('\n');
const formattedDetails = details
.filter(line => line.trim())
.map(line => {
if (line.startsWith('-')) {
const [field, changes] = line.substring(2).split(':');
if (changes) {
const [from, to] = changes.split('to:').map(s => s.trim());
return ` - ${field}:\n from: ${from.replace('from:', '').trim()}\n to: ${to}`;
}
}
return line;
})
.join('\n');

return `${header}\n${formattedDetails}`;
}

return cleanMessage;
}

export const selectPostfix = (arr: string[], singular: string, plural: string) => (arr.length > 1 ? plural : singular);

export function getUsrMsgKeyToDisplay(appName: string, msgKey: string, usrMessages: appModels.UserMessages[]) {
Expand Down

0 comments on commit 93161a5

Please sign in to comment.