2020-06-19 11:36:49 +01:00
|
|
|
import colors from "./colors";
|
2021-02-06 23:33:52 +05:30
|
|
|
import { AppState } from "./types";
|
2020-06-19 11:36:49 +01:00
|
|
|
|
2021-02-06 23:33:52 +05:30
|
|
|
export const getClientColors = (clientId: string, appState: AppState) => {
|
|
|
|
if (appState?.collaborators) {
|
|
|
|
const currentUser = appState.collaborators.get(clientId);
|
|
|
|
if (currentUser?.color) {
|
|
|
|
return currentUser.color;
|
|
|
|
}
|
|
|
|
}
|
2020-06-19 11:36:49 +01:00
|
|
|
// Naive way of getting an integer out of the clientId
|
|
|
|
const sum = clientId.split("").reduce((a, str) => a + str.charCodeAt(0), 0);
|
|
|
|
|
2022-11-01 17:29:58 +01:00
|
|
|
// Skip transparent & gray colors
|
|
|
|
const backgrounds = colors.elementBackground.slice(3);
|
|
|
|
const strokes = colors.elementStroke.slice(3);
|
2020-06-19 11:36:49 +01:00
|
|
|
return {
|
|
|
|
background: backgrounds[sum % backgrounds.length],
|
|
|
|
stroke: strokes[sum % strokes.length],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-14 02:49:09 +09:00
|
|
|
/**
|
|
|
|
* returns first char, capitalized
|
|
|
|
*/
|
|
|
|
export const getNameInitial = (name?: string | null) => {
|
|
|
|
// first char can be a surrogate pair, hence using codePointAt
|
|
|
|
const firstCodePoint = name?.trim()?.codePointAt(0);
|
|
|
|
return (
|
|
|
|
firstCodePoint ? String.fromCodePoint(firstCodePoint) : "?"
|
|
|
|
).toUpperCase();
|
2020-06-19 11:36:49 +01:00
|
|
|
};
|