clients.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import colors from "./colors";
  2. import { AppState } from "./types";
  3. export const getClientColors = (clientId: string, appState: AppState) => {
  4. if (appState?.collaborators) {
  5. const currentUser = appState.collaborators.get(clientId);
  6. if (currentUser?.color) {
  7. return currentUser.color;
  8. }
  9. }
  10. // Naive way of getting an integer out of the clientId
  11. const sum = clientId.split("").reduce((a, str) => a + str.charCodeAt(0), 0);
  12. // Skip transparent background.
  13. const backgrounds = colors.elementBackground.slice(1);
  14. const strokes = colors.elementStroke.slice(1);
  15. return {
  16. background: backgrounds[sum % backgrounds.length],
  17. stroke: strokes[sum % strokes.length],
  18. };
  19. };
  20. export const getClientInitials = (username?: string | null) => {
  21. if (!username) {
  22. return "?";
  23. }
  24. const names = username.trim().split(" ");
  25. if (names.length < 2) {
  26. return names[0].substring(0, 2).toUpperCase();
  27. }
  28. const firstName = names[0];
  29. const lastName = names[names.length - 1];
  30. return (firstName[0] + lastName[0]).toUpperCase();
  31. };