mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 13:36:20 +00:00
Migrated to TS main services except ShlinkApiClient
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import { rangeOf } from '../utils';
|
||||
import LocalStorage from './LocalStorage';
|
||||
|
||||
const HEX_COLOR_LENGTH = 6;
|
||||
const { floor, random } = Math;
|
||||
const letters = '0123456789ABCDEF';
|
||||
const buildRandomColor = () =>
|
||||
`#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
||||
const normalizeKey = (key) => key.toLowerCase().trim();
|
||||
const buildRandomColor = () => `#${rangeOf(HEX_COLOR_LENGTH, () => letters[floor(random() * letters.length)]).join('')}`;
|
||||
const normalizeKey = (key: string) => key.toLowerCase().trim();
|
||||
|
||||
export default class ColorGenerator {
|
||||
constructor(storage) {
|
||||
this.storage = storage;
|
||||
this.colors = this.storage.get('colors') || {};
|
||||
private readonly colors: Record<string, string>;
|
||||
|
||||
public constructor(private readonly storage: LocalStorage) {
|
||||
this.colors = this.storage.get<Record<string, string>>('colors') || {};
|
||||
}
|
||||
|
||||
getColorForKey = (key) => {
|
||||
public readonly getColorForKey = (key: string) => {
|
||||
const normalizedKey = normalizeKey(key);
|
||||
const color = this.colors[normalizedKey];
|
||||
|
||||
@@ -26,7 +27,7 @@ export default class ColorGenerator {
|
||||
return color;
|
||||
};
|
||||
|
||||
setColorForKey = (key, color) => {
|
||||
public readonly setColorForKey = (key: string, color: string) => {
|
||||
const normalizedKey = normalizeKey(key);
|
||||
|
||||
this.colors[normalizedKey] = color;
|
||||
@@ -36,6 +37,7 @@ export default class ColorGenerator {
|
||||
};
|
||||
}
|
||||
|
||||
/** @deprecated Use ColorGenerator class instead */
|
||||
export const colorGeneratorType = PropTypes.shape({
|
||||
getColorForKey: PropTypes.func,
|
||||
setColorForKey: PropTypes.func,
|
||||
Reference in New Issue
Block a user