From 5753e14721bd54f6dc56d22961523544088f895c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Jaenisch?= Date: Sun, 23 Jun 2024 12:27:41 +0200 Subject: [PATCH] refactor: group commits by date and project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Yes, this means nested looping. But it also allows me to separate the commits into a shape I need. I feel like this should not happen in Vervis as it is something I as a consument of the data need. I could have done this inside of the template but it feels ugly. The main drawback I have with the current approach is that Svelte is not capable of looping over objects so I have to turn that into a list of nested Arrays. I might have done that in the function already but it is easier to look up keys on an Object that to sift through Arrays. The current implementation works. The next step is to extract it into a component, test it and then I can refactor further. Signed-off-by: André Jaenisch --- src/lib/components/templates/Profile.svelte | 192 ++++++++++++-------- 1 file changed, 112 insertions(+), 80 deletions(-) diff --git a/src/lib/components/templates/Profile.svelte b/src/lib/components/templates/Profile.svelte index b241604..7ec864a 100644 --- a/src/lib/components/templates/Profile.svelte +++ b/src/lib/components/templates/Profile.svelte @@ -74,6 +74,31 @@ You should have received a copy of the GNU Affero General Public License along w }) .flat(1); }; + + /* Since I group commits by date and project I need to create a structure. */ + const groupCommitsByDateAndProject = function (commitsMap) { + const groupByDateAndProject = {}; + commitsMap + // Some commits couldn't be fetched from Vervis + .filter((commit) => Boolean(commit.id)) + .forEach((commit) => { + const { committed, context } = commit; + + // Ensure key exists + if (!groupByDateAndProject[committed]) { + groupByDateAndProject[committed] = {}; + } + + // Ensure Array exists + if (!groupByDateAndProject[committed][context]) { + groupByDateAndProject[committed][context] = []; + } + + groupByDateAndProject[committed][context].push(commit); + }); + + return groupByDateAndProject; + };
@@ -199,86 +224,93 @@ You should have received a copy of the GNU Affero General Public License along w
    {#if data?.user?.commitsMap} - -
  • - -
    - - -
    - - - - - - - - - {$date(new Date(data.user.commitsMap[0].created))} - {$_( - 'page.profile.history.activities.commits.number', - { values: { number: data.user.commitsMap.length - 1 } } - )} - - - - {data.user.followingsMap[3].name} - - -
    - -
      - {#each data.user.commitsMap as commit} - {#if commit.name} -
    • - -
      - -
      - {commit.name} - {$_('page.profile.history.activities.commits.relative_time', { - values: { relativeTime: $date(new Date(commit.committed)) } - })} -
      - -
      - - - -
      - -
      - -
      -
      -
    • - {/if} - {/each} -
    -
    -
  • + {#each Object.entries(groupCommitsByDateAndProject(data.user.commitsMap)) as commitsByDateAndProject} + {#each Object.entries(commitsByDateAndProject[1]) as commitsByProject} + +
  • + +
    + + +
    + + + + + + + {$date(new Date(commitsByDateAndProject[0]))} - {$_( + 'page.profile.history.activities.commits.number', + { values: { number: commitsByProject[1].length } } + )} + + + {data.user.followingsMap.find( + (following) => following.id === commitsByProject[0] + ).name} + + +
    + +
      + {#each commitsByProject[1] as commit} + {#if commit.name} +
    • + +
      + +
      + {commit.name} + {$_('page.profile.history.activities.commits.relative_time', { + values: { relativeTime: $date(new Date(commit.committed)) } + })} +
      + +
      + + + +
      + +
      + +
      +
      +
    • + {/if} + {/each} +
    +
    +
  • + {/each} + {/each} {:else}