Skip to content

Commit

Permalink
Extract GH-Label component
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Oct 21, 2024
1 parent a4dcf5e commit 3faeda1
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public Optional<UserDTO> addTeamToUser(String login, Long teamId) {
Team team = optionalTeam.get();
User user = optionalUser.get();
team.addMember(user);
teamService.saveTeam(team);
return Optional.of(new UserDTO(user.getId(), user.getLogin(), user.getEmail(), user.getName(), user.getUrl()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ public class Team {

public void addMember(User user) {
members.add(user);
// user.addTeam(this);
user.addTeam(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public List<TeamDTO> getAllTeams() {
return teams;
}

public Team saveTeam(Team team) {
logger.info("Saving team: " + team);
return teamRepository.save(team);
}

public Boolean createDefaultTeams() {
logger.info("Creating default teams");
Team iris = teamRepository.save(new Team());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void addReview(PullRequestReview review) {
reviews.add(review);
}

// public void addTeam(Team team) {
// teams.add(team);
// }
public void addTeam(Team team) {
teams.add(team);
}
}
9 changes: 4 additions & 5 deletions webapp/src/app/admin/users/table/users-table.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@
</brn-column-def>
<brn-column-def name="teams" class="w-20 lg:flex-1">
<hlm-th *brnHeaderDef>Teams</hlm-th>
<hlm-td class="font-medium tabular-nums" *brnCellDef="let element">
@if (this.isLoading()) {
<hlm-skeleton class="h-6 w-36" />
} @else {
{{ element.teams }}
<hlm-td class="font-medium tabular-nums flex gap-4" *brnCellDef="let element">
@for (team of element.teams; track team) {
@let label = { name: team.name, color: '69feff' };
<app-github-label [label]="label" [isLoading]="this.isLoading()" />
}
</hlm-td>
</brn-column-def>
Expand Down
7 changes: 5 additions & 2 deletions webapp/src/app/admin/users/table/users-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { HlmSkeletonModule } from '@spartan-ng/ui-skeleton-helm';
import { debounceTime, map } from 'rxjs';
import { AdminService, TeamDTO, UserTeamsDTO } from '@app/core/modules/openapi';
import { RouterLink } from '@angular/router';
import { GithubLabelComponent } from '@app/ui/github-label/github-label.component';

const LOADING_DATA: UserTeamsDTO[] = [
{
Expand Down Expand Up @@ -82,7 +83,9 @@ const LOADING_TEAMS: TeamDTO[] = [
BrnSelectModule,
HlmSelectModule,

HlmSkeletonModule
HlmSkeletonModule,

GithubLabelComponent
],
providers: [provideIcons({ lucideChevronDown, lucideMoreHorizontal, lucideArrowUpDown })],
templateUrl: './users-table.component.html'
Expand Down Expand Up @@ -190,7 +193,7 @@ export class AdminUsersTableComponent {
this.adminService.addTeamToUser(user.login, this._selectedTeam()!.id).subscribe({
next: () => {
console.log('Team added to user', user);
this.userData()
this._users()
?.find((u) => u.login === user.login)
?.teams.add(this._selectedTeam()!);
},
Expand Down
84 changes: 84 additions & 0 deletions webapp/src/app/ui/github-label/github-label.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Component, computed, input } from '@angular/core';
import type { PullRequestLabel } from '@app/core/modules/openapi';
import { HlmSkeletonModule } from '@spartan-ng/ui-skeleton-helm';

@Component({
selector: 'app-github-label',
standalone: true,
imports: [HlmSkeletonModule],
styleUrls: ['./github-label.component.scss'],
host: {
'[style.--label-r]': 'colors().r',
'[style.--label-g]': 'colors().g',
'[style.--label-b]': 'colors().b',
'[style.--label-h]': 'colors().h',
'[style.--label-s]': 'colors().s',
'[style.--label-l]': 'colors().l'
},
template: `
@if (isLoading()) {
<hlm-skeleton class="w-14 h-6" />
} @else {
<span class="px-2 py-0.5 rounded-[2rem] text-xs font-medium dark:border gh-label">
{{ label().name }}
</span>
}
`
})
export class GithubLabelComponent {
isLoading = input(false);
label = input.required<PullRequestLabel>();

protected colors = computed(() => this.hexToRgb(this.label().color ?? 'FFFFFF'));

hexToRgb(hex: string) {
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;

const hsl = this.rgbToHsl(r, g, b);

return {
r: r,
g: g,
b: b,
...hsl
};
}

rgbToHsl(r: number, g: number, b: number) {
r /= 255;
g /= 255;
b /= 255;

const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h = 0,
s = 0,
l = (max + min) / 2;

if (max !== min) {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}

h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);

return { h, s, l };
}
}
66 changes: 66 additions & 0 deletions webapp/src/app/ui/github-label/github-label.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { argsToTemplate, Meta, StoryObj } from '@storybook/angular';
import { GithubLabelComponent } from './github-label.component';

type FlatArgs = {
isLoading: boolean;
name: string;
color: string;
};

function flatArgsToProps(args: FlatArgs) {
return {
isLoading: args.isLoading,
label: {
name: args.name,
color: args.color
}
};
}

const meta: Meta<FlatArgs> = {
component: GithubLabelComponent,
tags: ['autodocs'],
args: {
isLoading: false,
name: 'bug',
color: 'f00000'
},
argTypes: {
isLoading: {
control: {
type: 'boolean'
}
},
name: {
control: {
type: 'text'
}
},
color: {
control: {
type: 'text'
}
}
}
};

export default meta;

type Story = StoryObj<GithubLabelComponent>;

export const Default: Story = {
render: (args) => ({
props: flatArgsToProps(args as unknown as FlatArgs),
template: `<app-github-label ${argsToTemplate(flatArgsToProps(args as unknown as FlatArgs))} />`
})
};

export const isLoading: Story = {
args: {
isLoading: true
},
render: (args) => ({
props: flatArgsToProps(args as unknown as FlatArgs),
template: `<app-github-label ${argsToTemplate(flatArgsToProps(args as unknown as FlatArgs))} />`
})
};
13 changes: 1 addition & 12 deletions webapp/src/app/user/issue-card/issue-card.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,7 @@
@if (!isLoading()) {
<div hlmCardFooter class="flex flex-wrap gap-2 p-0 space-x-0">
@for (label of pullRequestLabels(); track label.name) {
@let labelColors = hexToRgb(label.color ?? 'FFFFFF');
<span
class="px-2 py-0.5 rounded-[2rem] text-xs font-medium dark:border gh-label"
[style.--label-r]="labelColors.r"
[style.--label-g]="labelColors.g"
[style.--label-b]="labelColors.b"
[style.--label-h]="labelColors.h"
[style.--label-s]="labelColors.s"
[style.--label-l]="labelColors.l"
>
{{ label.name }}
</span>
<app-github-label [label]="label"></app-github-label>
}
</div>
}
Expand Down
55 changes: 2 additions & 53 deletions webapp/src/app/user/issue-card/issue-card.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { NgIcon } from '@ng-icons/core';
import { octCheck, octComment, octFileDiff, octGitPullRequest, octGitPullRequestClosed, octX } from '@ng-icons/octicons';
import { HlmCardModule } from '@spartan-ng/ui-card-helm';
import { HlmSkeletonComponent } from '@spartan-ng/ui-skeleton-helm';
import { GithubLabelComponent } from '@app/ui/github-label/github-label.component';
import dayjs from 'dayjs';
import { cn } from '@app/utils';

@Component({
selector: 'app-issue-card',
templateUrl: './issue-card.component.html',
imports: [NgIcon, HlmCardModule, HlmSkeletonComponent],
styleUrls: ['./issue-card.component.scss'],
imports: [NgIcon, HlmCardModule, HlmSkeletonComponent, GithubLabelComponent],
standalone: true
})
export class IssueCardComponent {
Expand All @@ -37,55 +37,4 @@ export class IssueCardComponent {
displayCreated = computed(() => dayjs(this.createdAt()));
displayTitle = computed(() => (this.title() ?? '').replace(/`([^`]+)`/g, '<code class="textCode">$1</code>'));
computedClass = computed(() => cn('w-72', !this.isLoading() ? 'hover:bg-accent/50 cursor-pointer' : '', this.class()));

hexToRgb(hex: string) {
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;

const hsl = this.rgbToHsl(r, g, b);

return {
r: r,
g: g,
b: b,
...hsl
};
}

rgbToHsl(r: number, g: number, b: number) {
r /= 255;
g /= 255;
b /= 255;

const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
let h = 0,
s = 0,
l = (max + min) / 2;

if (max !== min) {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
}
h /= 6;
}

h = Math.round(h * 360);
s = Math.round(s * 100);
l = Math.round(l * 100);

return { h, s, l };
}
}

0 comments on commit 3faeda1

Please sign in to comment.