Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
fix(log): remove type assertions
Browse files Browse the repository at this point in the history
extract getTeams, which is type-safe
  • Loading branch information
mxdvl committed Aug 4, 2022
1 parent 278681b commit 4e2dd59
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
6 changes: 3 additions & 3 deletions scripts/generateSvg.logger.teams.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { teamStyles } from '../src/logger/teamStyles';
import { commonStyle, teamStyles } from '../src/logger/teamStyles';

fs.writeFileSync(__dirname + '/../static/logger.svg', generateSvg());

Expand Down Expand Up @@ -72,8 +72,8 @@ function generateSvg(): string {
}
.common {
background-color: ${teamStyles.common.background};
color: ${teamStyles.common.font};
background-color: ${commonStyle.common.background};
color: ${commonStyle.common.font};
}
</style>
<foreignObject x="0" y="0" width="${width}" height="${height}">
Expand Down
6 changes: 3 additions & 3 deletions src/logger/@types/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { teamStyles } from '../teamStyles';
import type { commonStyle, teamStyles } from '../teamStyles';

export type Teams<K extends string> = Record<K, Record<string, string>>;
export type LogCall = (team: TeamName, ...args: unknown[]) => void;
export type TeamSubscription = (arg: TeamName) => void;
export type TeamStyle = keyof typeof teamStyles;
export type TeamName = Exclude<TeamStyle, 'common'>;
export type TeamStyle = TeamName | keyof typeof commonStyle;
export type TeamName = keyof typeof teamStyles;
34 changes: 20 additions & 14 deletions src/logger/log.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { isString } from '../isString/isString';
import { storage } from '../storage/storage';
import type { LogCall, TeamStyle, TeamSubscription } from './@types/logger';
import type {
LogCall,
TeamName,
TeamStyle,
TeamSubscription,
} from './@types/logger';
import { STORAGE_KEY } from './storage-key';
import { teamStyles } from './teamStyles';
import { commonStyle, isTeam, teamStyles } from './teamStyles';

const allStyles = { ...teamStyles, ...commonStyle };

const messageStyle = (teamStyle: TeamStyle): string => {
const { background, font } = teamStyles[teamStyle];
const { background, font } = allStyles[teamStyle];
return `background: ${background}; color: ${font}; padding: 2px 3px; border-radius:3px`;
};

const getTeams = (): TeamName[] => {
const teams = storage.local.get(STORAGE_KEY);
if (!isString(teams)) return [];
return teams.split(',').filter(isTeam);
};

/**
* Subscribe to a team’s log
* @param team the team’s unique ID
Expand All @@ -27,11 +41,7 @@ const subscribeTo: TeamSubscription = (team) => {
*/
const unsubscribeFrom: TeamSubscription = (team) => {
log(team, '🔕 Unsubscribed, good-bye!');
const teamSubscriptions: string[] = (
storage.local.get(STORAGE_KEY) as string
)
.split(',')
.filter((t) => t !== team);
const teamSubscriptions: string[] = getTeams().filter((t) => t !== team);
storage.local.set(STORAGE_KEY, teamSubscriptions.join(','));
};

Expand All @@ -41,19 +51,15 @@ if (typeof window !== 'undefined') {
window.guardian.logger ||= {
subscribeTo,
unsubscribeFrom,
teams: () => Object.keys(teamStyles),
teams: getTeams,
};
}

/**
* Runs in all environments, if local storage values are set.
*/

export const log: LogCall = (team, ...args) => {
// TODO add check for localStorage
if (!((storage.local.get(STORAGE_KEY) || '') as string).includes(team)) {
return;
}
if (!getTeams().includes(team)) return;

const styles = [messageStyle('common'), '', messageStyle(team), ''];

Expand Down
20 changes: 15 additions & 5 deletions src/logger/teamStyles.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import type { TeamName } from './@types/logger';

/** Common Guardian blue label. Do not edit */
const commonStyle = {
common: {
background: '#052962',
font: '#ffffff',
},
} as const;

/**
* You can only subscribe to teams from this list.
* Add your team name below to start logging.
*
* Make sure your label has a contrast ratio of 4.5 or more.
* */
export const teamStyles = {
common: {
background: '#052962',
font: '#ffffff',
},
const teamStyles = {
commercial: {
background: '#77EEAA',
font: '#004400',
Expand All @@ -34,3 +40,7 @@ export const teamStyles = {
font: '#ffffff',
},
} as const;

const isTeam = (team: string): team is TeamName => team in teamStyles;

export { commonStyle, teamStyles, isTeam };

0 comments on commit 4e2dd59

Please sign in to comment.