clients.ts 844 B

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