forked from snario/allhands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateSlidesFromLinearInitiatives.ts
263 lines (223 loc) · 7.45 KB
/
createSlidesFromLinearInitiatives.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* Script to create a Google Slides presentation from Linear initiatives.
*
* Author: @snario <[email protected]>
* Date: August 25 2024
*
* What it does:
* - Fetches initiatives and projects from Linear API.
* - Creates slides for each initiative and project.
* - One slide for the initiative TOC (list of initiatives).
* - One slide for each initiative summary.
* - One slide for each project in an initiative.
*/
import {
getDocumentProperty,
getOrSetSecretInteractive,
saveDocumentProperty,
} from "../../lib/googleAppsScript";
import InitiativeSlide from "./initiativeSlide";
import {
fetchAllInitiatives,
fetchAllProjects,
fetchInitiative,
fetchProject,
InitiativeWithProjects,
mapProjectsToInitiatives,
} from "../../lib/linear";
import ProjectSlide from "./projectSlide";
import AgendaSlide from "./agendaSlide";
import { insertTextBox } from "../../lib/googleSlides";
import { TEXT_COLOR_SECONDARY } from "../../constants";
export function createSlidesFromLinear(): void {
const presentation = SlidesApp.getActivePresentation();
if (!presentation)
throw new Error("Active document is not a Google Slides presentation.");
const apiKey = getOrSetSecretInteractive(SlidesApp, "LINEAR_API_KEY");
const initiatives = fetchAndPrepareData(apiKey);
Logger.log(`Presentation URL: ${presentation.getUrl()}`);
let cache = fetchCacheFromDocumentProperties(presentation);
const config = JSON.parse(getDocumentProperty("configSettings")) || {};
cache = generateSlidesAndUpdateCache(
presentation,
initiatives,
cache,
config,
);
Logger.log("Slides created or updated successfully");
saveCacheToDocumentProperties(presentation, cache);
}
export function updateExistingProjectSlide() {
const presentation = SlidesApp.getActivePresentation();
if (!presentation)
throw new Error("Active document is not a Google Slides presentation.");
const apiKey = getOrSetSecretInteractive(SlidesApp, "LINEAR_API_KEY");
const projectSlideMap: Record<string, string> = JSON.parse(
getDocumentProperty(`${ProjectSlide.cacheKey}_${presentation.getId()}`),
);
if (!projectSlideMap)
throw new Error("No cache found for this presentation.");
const slideId = presentation.getSelection().getCurrentPage().getObjectId();
const projectId = Object.keys(projectSlideMap).find(
(projectId) => slideId === projectSlideMap[projectId],
);
if (!projectId) {
throw new Error("No project slide found on the current slide.");
}
const project = fetchProject(apiKey, projectId);
const initiative = fetchInitiative(apiKey, project.initiatives.nodes[0].id);
const projectSlide = getOrCreateSlideWithCache(
presentation,
projectSlideMap,
project.id,
);
const config = JSON.parse(getDocumentProperty("configSettings"));
ProjectSlide.populate(projectSlide, project, initiative, config);
}
function fetchAndPrepareData(apiKey: string): InitiativeWithProjects[] {
return mapProjectsToInitiatives(
fetchAllInitiatives(apiKey),
fetchAllProjects(apiKey),
);
}
function generateSlidesAndUpdateCache(
presentation: GoogleAppsScript.Slides.Presentation,
initiatives: InitiativeWithProjects[],
cache: {
projectSlideMap: Record<string, string>;
agendaSlideMap: Record<string, string>;
initiativeSlideMap: Record<string, string>;
},
config: {
includeProjectSlides: boolean;
includeAgendaSlide: boolean;
withAssigneeAvatars: boolean;
},
) {
const { projectSlideMap, agendaSlideMap, initiativeSlideMap } = cache;
initiatives.forEach((initiative) => {
if (config.includeAgendaSlide) {
const agendaSlide = getOrCreateSlideWithCache(
presentation,
agendaSlideMap,
initiative.id,
);
AgendaSlide.populate(
agendaSlide,
initiatives,
initiative.id,
config,
);
}
const initiativeSlide = getOrCreateSlideWithCache(
presentation,
initiativeSlideMap,
initiative.id,
);
InitiativeSlide.populate(initiativeSlide, initiative, config);
if (config.includeProjectSlides) {
initiative.projects.forEach((project) => {
const projectSlide = getOrCreateSlideWithCache(
presentation,
projectSlideMap,
project.id,
);
ProjectSlide.populate(
projectSlide,
project,
initiative,
config,
);
});
}
});
return cache;
}
function fetchCacheFromDocumentProperties(
presentation: GoogleAppsScript.Slides.Presentation,
) {
return {
projectSlideMap:
JSON.parse(
getDocumentProperty(
`${ProjectSlide.cacheKey}_${presentation.getId()}`,
),
) || {},
agendaSlideMap:
JSON.parse(
getDocumentProperty(
`${AgendaSlide.cacheKey}_${presentation.getId()}`,
),
) || {},
initiativeSlideMap:
JSON.parse(
getDocumentProperty(
`${InitiativeSlide.cacheKey}_${presentation.getId()}`,
),
) || {},
};
}
function saveCacheToDocumentProperties(
presentation: GoogleAppsScript.Slides.Presentation,
cache: {
projectSlideMap: Record<string, string>;
agendaSlideMap: Record<string, string>;
initiativeSlideMap: Record<string, string>;
},
) {
saveDocumentProperty(
`${ProjectSlide.cacheKey}_${presentation.getId()}`,
JSON.stringify(cache.projectSlideMap),
);
saveDocumentProperty(
`${AgendaSlide.cacheKey}_${presentation.getId()}`,
JSON.stringify(cache.agendaSlideMap),
);
saveDocumentProperty(
`${InitiativeSlide.cacheKey}_${presentation.getId()}`,
JSON.stringify(cache.initiativeSlideMap),
);
}
interface SlideCache {
[id: string]: ReturnType<GoogleAppsScript.Slides.Slide["getObjectId"]>;
}
export function getOrCreateSlideWithCache(
presentation: GoogleAppsScript.Slides.Presentation,
cache: SlideCache,
id: string,
): GoogleAppsScript.Slides.Slide {
let slide;
if (cache[id]) {
slide = presentation.getSlideById(cache[id]);
}
if (!slide) {
slide = presentation.appendSlide();
cache[id] = slide.getObjectId();
}
const timestampOptions: Intl.DateTimeFormatOptions = {
month: "short",
day: "2-digit",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
timeZoneName: "short",
};
const timestamp = new Date().toLocaleDateString("en-US", timestampOptions);
// Insert a timestamp on the slide for easy reference
insertTextBox(
slide,
{
paragraphAlignment: SlidesApp.ParagraphAlignment.END,
fontSize: 7,
fontColor: TEXT_COLOR_SECONDARY,
},
{
top: presentation.getPageHeight() - 20,
left: presentation.getPageWidth() - 200,
width: 200,
height: 20,
},
`Slide generated on ${timestamp}`,
);
return slide;
}