notification-index.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. type NotificationIndexItem = {
  2. directory?: string
  3. session?: string
  4. viewed: boolean
  5. type: string
  6. }
  7. export function buildNotificationIndex<T extends NotificationIndexItem>(list: T[]) {
  8. const sessionAll = new Map<string, T[]>()
  9. const sessionUnseen = new Map<string, T[]>()
  10. const sessionUnseenCount = new Map<string, number>()
  11. const sessionUnseenHasError = new Map<string, boolean>()
  12. const projectAll = new Map<string, T[]>()
  13. const projectUnseen = new Map<string, T[]>()
  14. const projectUnseenCount = new Map<string, number>()
  15. const projectUnseenHasError = new Map<string, boolean>()
  16. for (const notification of list) {
  17. const session = notification.session
  18. if (session) {
  19. const all = sessionAll.get(session)
  20. if (all) all.push(notification)
  21. else sessionAll.set(session, [notification])
  22. if (!notification.viewed) {
  23. const unseen = sessionUnseen.get(session)
  24. if (unseen) unseen.push(notification)
  25. else sessionUnseen.set(session, [notification])
  26. sessionUnseenCount.set(session, (sessionUnseenCount.get(session) ?? 0) + 1)
  27. if (notification.type === "error") sessionUnseenHasError.set(session, true)
  28. }
  29. }
  30. const directory = notification.directory
  31. if (directory) {
  32. const all = projectAll.get(directory)
  33. if (all) all.push(notification)
  34. else projectAll.set(directory, [notification])
  35. if (!notification.viewed) {
  36. const unseen = projectUnseen.get(directory)
  37. if (unseen) unseen.push(notification)
  38. else projectUnseen.set(directory, [notification])
  39. projectUnseenCount.set(directory, (projectUnseenCount.get(directory) ?? 0) + 1)
  40. if (notification.type === "error") projectUnseenHasError.set(directory, true)
  41. }
  42. }
  43. }
  44. return {
  45. session: {
  46. all: sessionAll,
  47. unseen: sessionUnseen,
  48. unseenCount: sessionUnseenCount,
  49. unseenHasError: sessionUnseenHasError,
  50. },
  51. project: {
  52. all: projectAll,
  53. unseen: projectUnseen,
  54. unseenCount: projectUnseenCount,
  55. unseenHasError: projectUnseenHasError,
  56. },
  57. }
  58. }