From 4d2a7d1686b657346b7304af7d0141972b7c2d4f Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Tue, 20 Jul 2021 02:35:55 +0200
Subject: [PATCH] Simplify getting the source name of effects

---
 src/lang/de.json                                  |  2 +-
 src/lang/en.json                                  |  2 +-
 src/module/active-effect.ts                       |  7 +++++++
 src/module/actor/sheets/actor-sheet.ts            | 15 ++++-----------
 .../sheets/actor/components/effect-list-entry.hbs |  4 ++--
 .../actor/components/effect-list-header.hbs       |  4 ++--
 6 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/lang/de.json b/src/lang/de.json
index a268faa..5a6b293 100644
--- a/src/lang/de.json
+++ b/src/lang/de.json
@@ -125,7 +125,7 @@
     "DS4.EffectEnabled": "Aktiv",
     "DS4.EffectEnabledAbbr": "A",
     "DS4.EffectLabel": "Bezeichnung",
-    "DS4.EffectSource": "Quelle",
+    "DS4.EffectSourceName": "Quelle",
     "DS4.ActorName": "Name",
     "DS4.ActorImageAltText": "Bild des Aktors",
     "DS4.ActorTypeCharacter": "Charakter",
diff --git a/src/lang/en.json b/src/lang/en.json
index 1b94e0c..9ce4238 100644
--- a/src/lang/en.json
+++ b/src/lang/en.json
@@ -125,7 +125,7 @@
     "DS4.EffectEnabled": "Enabled",
     "DS4.EffectEnabledAbbr": "E",
     "DS4.EffectLabel": "Label",
-    "DS4.EffectSource": "Source",
+    "DS4.EffectSourceName": "Source",
     "DS4.ActorName": "Name",
     "DS4.ActorImageAltText": "Image of the Actor",
     "DS4.ActorTypeCharacter": "Character",
diff --git a/src/module/active-effect.ts b/src/module/active-effect.ts
index 1ae6ecc..6ae292d 100644
--- a/src/module/active-effect.ts
+++ b/src/module/active-effect.ts
@@ -21,4 +21,11 @@ export class DS4ActiveEffect extends ActiveEffect {
         }
         return super.apply(actor, change);
     }
+
+    /**
+     * A public wrapper for {@link ActiveEffect#_getSourceName}.
+     */
+    async getSourceName(): Promise<string> {
+        return this._getSourceName();
+    }
 }
diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts
index 859c382..7f111cd 100644
--- a/src/module/actor/sheets/actor-sheet.ts
+++ b/src/module/actor/sheets/actor-sheet.ts
@@ -11,7 +11,6 @@ import { getCanvas, getGame } from "../../helpers";
 import { DS4Item } from "../../item/item";
 import { DS4Settings, getDS4Settings } from "../../settings";
 import notifications from "../../ui/notifications";
-import { DS4Actor } from "../actor";
 import { isCheck } from "../actor-data-properties";
 
 /**
@@ -61,16 +60,10 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetD
             }),
         );
 
-        const enrichedEffectPromises = this.actor.effects.toObject().map(async (effect) => {
-            const originatingDocument = effect.origin !== undefined ? await fromUuid(effect.origin) : undefined;
-            const source =
-                (originatingDocument instanceof DS4Actor || originatingDocument instanceof DS4Item
-                    ? originatingDocument.name
-                    : null) ?? undefined;
-
+        const enrichedEffectPromises = this.actor.effects.map(async (effect) => {
             return {
-                ...effect,
-                source,
+                ...effect.toObject(),
+                sourceName: await effect.getSourceName(),
             };
         });
         const enrichedEffects = await Promise.all(enrichedEffectPromises);
@@ -335,5 +328,5 @@ interface DS4ActorSheetData extends ActorSheet.Data<ActorSheet.Options> {
 type ActiveEffectDataSource = foundry.data.ActiveEffectData["_source"];
 
 interface EnrichedActiveEffectDataSource extends ActiveEffectDataSource {
-    source?: string;
+    sourceName: string;
 }
diff --git a/src/templates/sheets/actor/components/effect-list-entry.hbs b/src/templates/sheets/actor/components/effect-list-entry.hbs
index 2fa4f33..99419a5 100644
--- a/src/templates/sheets/actor/components/effect-list-entry.hbs
+++ b/src/templates/sheets/actor/components/effect-list-entry.hbs
@@ -22,8 +22,8 @@ SPDX-License-Identifier: MIT
     {{!-- label --}}
     <div title="{{effectData.label}}">{{effectData.label}}</div>
 
-    {{!-- source --}}
-    <div>{{effectData.source}}</div>
+    {{!-- source name --}}
+    <div>{{effectData.sourceName}}</div>
 
     {{!-- control buttons --}}
     {{> systems/ds4/templates/sheets/actor/components/overview-control-buttons.hbs
diff --git a/src/templates/sheets/actor/components/effect-list-header.hbs b/src/templates/sheets/actor/components/effect-list-header.hbs
index 3256c9b..87ef232 100644
--- a/src/templates/sheets/actor/components/effect-list-header.hbs
+++ b/src/templates/sheets/actor/components/effect-list-header.hbs
@@ -18,8 +18,8 @@ SPDX-License-Identifier: MIT
     {{!-- label --}}
     <div>{{localize 'DS4.EffectLabel'}}</div>
 
-    {{!-- origin --}}
-    <div>{{localize 'DS4.EffectSource'}}</div>
+    {{!-- source name --}}
+    <div>{{localize 'DS4.EffectSourceName'}}</div>
 
     {{!-- control buttons placeholder --}}
     <div></div>