-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.ts
74 lines (72 loc) · 2.1 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { useMemo } from "react"
import { BROWSE_VIEW } from "~constants"
import type { Creator } from "~types/annotations"
import type {
BrowseViewEvent,
CaptureEventsList
} from "~types/captureEventsTypes"
import type { ChannelsMapItem, ContentsMapItem } from "~types/common"
interface structuredContents {
channelsMap: Map<string, ChannelsMapItem>
contentsMap: Map<string, ContentsMapItem>
rowsCount: number
}
interface Creators {
[key: string]: Creator
}
export const useBuildStructuredContentsList = (
events: CaptureEventsList,
creators: Creators
): structuredContents => {
return useMemo(() => {
const validEvents = events.filter(
(event) =>
event.type === BROWSE_VIEW &&
event.url &&
event.metadata.title &&
["live", "video", "short"].includes(event.viewType)
)
const contents = new Map()
const channels = new Map()
let index = 0
let rCount = 0
validEvents.forEach((event: BrowseViewEvent) => {
let channel = event.metadata.channelName || event.metadata.channelId
const channelSlug = `${event.metadata.channelId}-${event.platform}`
const creator = Object.values(creators).find((c) =>
c.channels.includes(channelSlug)
)
channel = creator ? creator.name : channel
// console.log('creators', creators, channelSlug);
if (!channels.has(channel)) {
channels.set(channel, new Map())
rCount++
}
const uniqueContents = channels.get(channel) // || new Map();
if (!uniqueContents.has(event.url)) {
index++
rCount++
uniqueContents.set(event.url, {
url: event.url,
title: event.metadata.title,
channel,
platform: event.platform,
index
})
contents.set(event.url, {
url: event.url,
title: event.metadata.title,
channel,
platform: event.platform,
index
})
}
channels.set(channel, uniqueContents)
})
return {
contentsMap: contents,
channelsMap: channels,
rowsCount: rCount
}
}, [events, creators])
}