-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseCalendar.ts
155 lines (135 loc) · 4.21 KB
/
useCalendar.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { add, format, sub } from 'date-fns'
import {
API_BASE_URLS,
CalendarEventFilterType,
generateNodeId,
getSlug,
MEETING_PREFIX,
MeetingSnippetContent,
SEPARATOR,
useCalendarStore,
useDataStore
} from '@mexit/core'
const MAX_EVENTS = 15
export const useCalendar = () => {
const setEvents = useCalendarStore((state) => state.setEvents)
const addToken = useCalendarStore((state) => state.addToken)
const getNodeForMeeting = async (e: any, onCreate): Promise<string | undefined> => {
const title = `${getSlug(e.summary)} ${format(e.times.start, 'dd-MM-yyyy')}`
const meetNotePath = `${MEETING_PREFIX}${SEPARATOR}${title}`
const links = useDataStore.getState().ilinks
const link = links?.find((l) => l.path === meetNotePath)
if (link) return link.nodeid
const node = await onCreate({
node: {
nodeid: generateNodeId(),
title,
path: meetNotePath
},
content: MeetingSnippetContent({
title: e.summary,
date: e.times.start,
link: e.links.meet ?? e.links.event
// attendees: getAttendeeUserIDsFromCalendarEvent(e)
})
})
if (node) return node.nodeId
}
const getEvents = async (url: string) => {
const result = await chrome.runtime.sendMessage({
type: 'CALENDAR',
subType: 'GET_EVENTS',
data: {
url
}
})
if (result.message) {
const events = result.message.items
setEvents(events.map(converGoogleEventToCalendarEvent))
}
}
const converGoogleEventToCalendarEvent = (event: any) => {
const createdTime = Date.parse(event.created)
const updatedTime = Date.parse(event.updated)
const startTime = Date.parse(event.start.dateTime ?? event.start.date)
const endTime = Date.parse(event.end.dateTime ?? event.end.date)
const people =
event.attendees !== undefined
? event.attendees.map((a) => {
const person: any = {
email: a.email,
displayName: a.displayName,
optional: a.optional,
organizer: a.email === event.organizer?.email,
responseStatus: a.responseStatus as any,
creator: a.email === event.creator?.email,
resource: a.resource
}
return person
})
: []
return {
id: event.id,
status: event.status as any,
summary: event.summary,
description: event.description,
links: {
meet: event.hangoutLink,
event: event.htmlLink
},
creator: event.creator,
times: {
created: createdTime,
updated: updatedTime,
start: startTime,
end: endTime
},
people
}
}
const getCalenderEvents = async () => {
const currentDate = new Date()
const yesterday = sub(currentDate, { days: 1 }).toISOString()
const twoDaysFromNow = add(currentDate, { days: 2 }).toISOString()
const request = encodeURI(
`${API_BASE_URLS.googleCalendar}?maxResults=${MAX_EVENTS}&timeMin=${yesterday}&timeMax=${twoDaysFromNow}`
)
await getEvents(request)
}
const getUpcomingEvents = (calendarEventFilter: CalendarEventFilterType) => {
const now = new Date()
const twoHoursFromNow = add(now, { hours: 2 })
const events = useCalendarStore.getState().events
switch (calendarEventFilter) {
case 'All':
return events.sort((a, b) => b.times.start - a.times.start)
case 'Past':
return events
.filter((event) => {
const start = new Date(event.times.start)
return start < now
})
.sort((a, b) => b.times.start - a.times.start)
case 'Upcoming':
default:
return events
.filter((event) => {
const start = new Date(event.times.start)
return start <= twoHoursFromNow && start >= now
})
.sort((a, b) => b.times.start - a.times.start)
}
}
const getCalendarAuth = async () => {
const res = await chrome.runtime.sendMessage({ type: 'CALENDAR', subType: 'GET_AUTH' })
if (!res?.error) {
addToken('GOOGLE_CAL', res?.message)
}
}
return {
getCalendarAuth,
getNodeForMeeting,
getUpcomingEvents,
getCalenderEvents
}
}