Files
smartguide/webapp/src/app/shared/ui/topic-chip/topic-chip.component.ts
T

215 lines
6.3 KiB
TypeScript

import { Component, computed, input, output } from '@angular/core';
import { MatIconModule } from '@angular/material/icon';
import { MatTooltipModule } from '@angular/material/tooltip';
import { Params } from '@angular/router';
import { Topic } from '../../../api';
import { ComponentTooltipDirective } from '../../../directives/component-tooltip.directive';
import { AppTopicTooltipComponent } from '../app-topic-tooltip/app-topic-tooltip.component';
export const TOPIC_CHIP_PALETTE = [
'purple',
'blue',
'green',
'orange',
'pink',
] as const;
export type TopicChipPaletteColor = (typeof TOPIC_CHIP_PALETTE)[number];
export type TopicChipColor = TopicChipPaletteColor | string;
export type TopicChipSize = 'normal' | 'mini';
export type TopicChipTooltipTarget = 'label' | 'button';
export interface TopicChipColorTokens {
border: string;
background: string;
activeBackground: string;
color: string;
}
export const TOPIC_CHIP_COLORS: Record<
TopicChipPaletteColor,
TopicChipColorTokens
> = {
purple: {
border: '#dcd5f7',
background: '#f2effc',
activeBackground: '#e2daf9',
color: '#7255d9',
},
blue: {
border: '#cde1fd',
background: '#edf4fe',
activeBackground: '#d7e7fe',
color: '#2678e8',
},
green: {
border: '#c7edd9',
background: '#eef9f3',
activeBackground: '#d3f2e2',
color: '#39a96b',
},
orange: {
border: '#fde5c7',
background: '#fff6eb',
activeBackground: '#fce9cf',
color: '#e08326',
},
pink: {
border: '#f9d2df',
background: '#fcf0f4',
activeBackground: '#f8dbe4',
color: '#e0527c',
},
};
export const topicChipColorTokens = (
color: TopicChipPaletteColor,
): Readonly<TopicChipColorTokens> => TOPIC_CHIP_COLORS[color];
@Component({
selector: 'app-topic-chip',
imports: [MatIconModule, MatTooltipModule, ComponentTooltipDirective],
templateUrl: './topic-chip.component.html',
styleUrl: './topic-chip.component.scss',
standalone: true,
host: {
'[class.topic-chip--normal]': 'normalSize()',
'[class.topic-chip--mini]': 'miniSize()',
'[class.topic-chip--active]': 'activeState()',
'[style.--topic-border]': 'topicBorder()',
'[style.--topic-bg]': 'topicBackground()',
'[style.--topic-active-bg]': 'topicActiveBackground()',
'[style.--topic-color]': 'topicColor()',
'[style.--topic-text]': 'topicText()',
'[style.--topic-count-bg]': 'topicCountBackground()',
'[style.--topic-count-text]': 'topicCountText()',
},
})
export class TopicChipComponent {
readonly AppTopicTooltipComponent = AppTopicTooltipComponent;
label = input<string>('');
count = input<number | string | null | undefined>(undefined);
description = input<string | null | undefined>('');
subfieldName = input<string | null | undefined>('');
fieldName = input<string | null | undefined>('');
domainName = input<string | null | undefined>('');
countTooltip = input<string | null | undefined>('');
countIcon = input<string | null | undefined>(undefined);
color = input<TopicChipColor>('purple');
size = input<TopicChipSize>('normal');
disabled = input<boolean>(false);
active = input<boolean>(false);
descriptionTooltipTarget = input<TopicChipTooltipTarget>('label');
topicId = input<string | null | undefined>(undefined);
topic = input<Topic | null | undefined>(undefined);
secondaryActionLabel = input<string | null>(null);
secondaryActionIcon = input<string>('person_search');
secondaryActionLink = input<unknown[] | string | null>(null);
secondaryActionQueryParams = input<Params | null>(null);
chipClick = output<void>();
normalSize = computed(() => this.size() === 'normal');
miniSize = computed(() => this.size() === 'mini');
activeState = computed(() => this.active());
topicBorder = computed(() => {
return (
this.colorTokens()?.border ??
`color-mix(in srgb, ${this.topicColor()} 72%, var(--sg-color-mix-base))`
);
});
topicBackground = computed(() => {
return (
this.colorTokens()?.background ??
`color-mix(in srgb, ${this.topicColor()} 17%, var(--sg-color-mix-base))`
);
});
topicActiveBackground = computed(() => {
return (
this.colorTokens()?.activeBackground ??
`color-mix(in srgb, ${this.topicColor()} 27%, var(--sg-color-mix-base))`
);
});
topicColor = computed(() => {
return this.colorTokens()?.color ?? this.color();
});
topicText = computed(() => {
return this.colorTokens()?.color
? this.colorTokens()!.color
: `color-mix(in srgb, ${this.topicColor()} 45%, var(--sg-text))`;
});
topicCountBackground = computed(() => {
return this.colorTokens()?.background ?? 'var(--sg-surface)';
});
topicCountText = computed(() => {
return this.colorTokens()?.color
? this.colorTokens()!.color
: `color-mix(in srgb, ${this.topicColor()} 48%, var(--sg-text))`;
});
hasCount = computed(() => {
const c = this.count();
return c !== undefined && c !== null && `${c}` !== '';
});
normalizedCountTooltip = computed(() => {
return (this.countTooltip() ?? '').trim();
});
countIconName = computed(() => {
return (this.countIcon() ?? '').trim();
});
readonly topicObject = computed<Topic>(() => {
const t = this.topic();
if (t) {
return t;
}
return {
id: this.topicId() ?? undefined,
display_name: this.label(),
description: this.description() ?? undefined,
subfield: this.subfieldName()
? { display_name: this.subfieldName() ?? undefined }
: undefined,
field: this.fieldName()
? { display_name: this.fieldName() ?? undefined }
: undefined,
domain: this.domainName()
? { display_name: this.domainName() ?? undefined }
: undefined,
};
});
tooltipInputs = computed(() => {
return {
topic: this.topicObject(),
topicId: this.topicId() ?? this.topic()?.id ?? undefined,
secondaryActionLabel: this.secondaryActionLabel(),
secondaryActionIcon: this.secondaryActionIcon(),
secondaryActionLink: this.secondaryActionLink(),
secondaryActionQueryParams: this.secondaryActionQueryParams(),
};
});
handleClick(): void {
if (this.disabled()) {
return;
}
this.chipClick.emit();
}
private colorTokens = computed<TopicChipColorTokens | undefined>(() => {
return TOPIC_CHIP_COLORS[this.color() as TopicChipPaletteColor];
});
}