Repository: temporalio/ui
Pull request #407 (2022-04-11 15:28:08)

Simplify event attributes with one key/value to just their value
If an event attribute only has one property, just show that one property. This is useful for Workflow Type Names.

cloudflare-pages[bot]: ## Deploying with &nbsp;<a href="https://pages.dev"><img alt="Cloudflare Pages" src="https://user-images.githubusercontent.com/23264/106598434-9e719e00-654f-11eb-9e59-6167043cfa01.png" width="16"></a> &nbsp;Cloudflare Pages

<table><tr><td><strong>Latest commit:</strong> </td><td>
<code>eb2b3c9</code>
</td></tr>
<tr><td><strong>Status:</strong></td><td>&nbsp;✅&nbsp; Deploy successful!</td></tr>
<tr><td><strong>Preview URL:</strong></td><td>
<a href='https://b9bf36c3.ui-c3x.pages.dev'>https://b9bf36c3.ui-c3x.pages.dev</a>
</td></tr>
</table>

[View logs](https://dash.cloudflare.com/?to=/:account/pages/view/temporal-pizza/b9bf36c3-3017-401f-9ebf-8683325d3c16)

cypress[bot]: 
<p align="center">
  <br />
  <img src="https://assets.cypress.io/github-pr-comment-icons/cypress-logo.png" height="32" />
</p>

---

### Test summary
<a href="https://dashboard.cypress.io/projects/g7xfxw/runs/88/overview?utm_source=github"><img src="https://assets.cypress.io/github-pr-comment-icons/green-check-mark.png" width="14" height="14" title="Passed" /></a> <b title="Passed">28</b>  &bull; <a href="https://dashboard.cypress.io/projects/g7xfxw/runs/88/test-results?utm_source=github&statuses=%5B%7B%22value%22%3A%22FAILED%22%2C%22label%22%3A%22FAILED%22%7D%5D"><img src="https://assets.cypress.io/github-pr-comment-icons/red-x.png" width="14" height="14" title="Failed" /></a> <b title="Failed">0</b>  &bull; <img src="https://assets.cypress.io/github-pr-comment-icons/gray-circle.png" width="14" height="14" title="Tests that did not run due to a developer annotating a test with .skip" /> <b title="Pending">0</b> &bull; <img src="https://assets.cypress.io/github-pr-comment-icons/yellow-triangle.png" width="14" height="14" title="Tests that did not run due to a failure in a mocha hook" /> <b title="Skipped">0</b> 

---

### Run details
| | |
| --- | --- |
| **Project** | Temporal UI |
| **Status** | Passed |
| **Commit** | 7205e25996 <span style="cursor:help;" title="Your CI provider generated this commit by merging this branch into the target branch.">ℹ️</span> |
| **Started** | Apr 11, 2022 3:29 PM |
| **Ended** | Apr 11, 2022 3:31 PM |
| **Duration** | 01:28 <a href="https://on.cypress.io/parallelization?utm_source=github" title="Enable parallelization to reduce run duration">💡</a> |
| **OS** | Linux Ubuntu - 20.04 |
| **Browser** | Chrome 100 <img src="https://cdnjs.cloudflare.com/ajax/libs/browser-logos/48.0.4/chrome/chrome_32x32.png" width="16" height="16" /> |

[View run in Cypress Dashboard ➡️](https://dashboard.cypress.io/projects/g7xfxw/runs/88/overview?utm_source=github)





---

This comment has been generated by **cypress-bot** as a result of this project's GitHub integration settings. You can manage this integration in [this project's settings in the Cypress Dashboard](https://dashboard.cypress.io/projects/g7xfxw/settings?utm_source=github)



Patch:
diff --git a/src/lib/models/event-history/index.ts b/src/lib/models/event-history/index.ts
index 35985e90..03185346 100644
--- a/src/lib/models/event-history/index.ts
+++ b/src/lib/models/event-history/index.ts
@@ -6,6 +6,7 @@ import { findAttributesAndKey } from '$lib/utilities/is-event-type';
 import { groupEvents } from '../group-events';
 import { getEventCategory } from './get-event-categorization';
 import { getEventClassification } from './get-event-classification';
+import { simplifyAttributes } from './simplify-attributes';
 
 export async function getEventAttributes(
   historyEvent: HistoryEvent,
@@ -27,7 +28,9 @@ const toEvent = async (
   const timestamp = formatDate(String(historyEvent.eventTime));
   const classification = getEventClassification(eventType);
   const category = getEventCategory(eventType);
-  const attributes = await getEventAttributes(historyEvent);
+  const attributes = await getEventAttributes(historyEvent).then(
+    simplifyAttributes,
+  );
 
   return {
     ...historyEvent,
diff --git a/src/lib/models/event-history/simplify-attributes.ts b/src/lib/models/event-history/simplify-attributes.ts
new file mode 100644
index 00000000..ce267900
--- /dev/null
+++ b/src/lib/models/event-history/simplify-attributes.ts
@@ -0,0 +1,31 @@
+const canBeSimplified = (value: unknown): value is Record<string, string> => {
+  if (value === null) return false;
+  if (value === undefined) return false;
+  if (typeof value !== 'object') return false;
+
+  const keys = Object.keys(value);
+  const [key] = keys;
+
+  if (keys.length !== 1) return false;
+  if (typeof value[key] !== 'string') return false;
+
+  return true;
+};
+
+const getValueForFirstKey = (value: Record<string, string>): string => {
+  for (const v of Object.values(value)) {
+    return v;
+  }
+};
+
+export const simplifyAttributes = (
+  attributes: EventAttributesWithType,
+): EventAttributesWithType => {
+  for (const [key, value] of Object.entries(attributes)) {
+    if (canBeSimplified(value)) {
+      attributes[key] = getValueForFirstKey(value);
+    }
+  }
+
+  return attributes;
+};


Test patch:
diff --git a/src/lib/models/event-history/simplify-attributes.test.ts b/src/lib/models/event-history/simplify-attributes.test.ts
new file mode 100644
index 00000000..b0a64869
--- /dev/null
+++ b/src/lib/models/event-history/simplify-attributes.test.ts
@@ -0,0 +1,32 @@
+import { simplifyAttributes } from './simplify-attributes';
+
+const createAttributes = () => {
+  return {
+    shouldBeSimple: {
+      name: 'SimpleResult',
+    },
+    notSoSimple: {
+      name: 'NotSimple',
+      type: 'NotSimple',
+    },
+  };
+};
+
+describe(simplifyAttributes, () => {
+  it('should take single key attributes and reduce them down to their values', () => {
+    const attributes = createAttributes();
+    const { shouldBeSimple } = simplifyAttributes(attributes);
+
+    expect(shouldBeSimple).toBe('SimpleResult');
+  });
+
+  it('should take single key attributes and reduce them down to their values', () => {
+    const attributes = createAttributes();
+    const { notSoSimple } = simplifyAttributes(attributes);
+
+    expect(notSoSimple).toEqual({
+      name: 'NotSimple',
+      type: 'NotSimple',
+    });
+  });
+});