-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
216 lines (188 loc) · 5.52 KB
/
index.jsx
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
import { styled, run } from "uebersicht";
const _version = "1.0.0";
// Config
export const refreshFrequency = 5000;
const options = {
/* Widget position! */
verticalPosition: "bottom", // -> top (default) | center | bottom | "<number>" | "-<number>"
horizontalPosition: "center", // -> left (default) | center | right | "<number>" | "-<number>"
alwaysShow: false, // -> true | false (default)
services: {
Things3: {
enabled: true,
tag: "Current",
},
Asana: {
enabled: false,
accessToken: "<asana API key here/>",
workspace: "<asana workspace name here/>",
workspaceID: null,
tag: "Current",
tagID: null,
},
},
};
// Asana Integration
let asanaTagId = null;
const ASANA_REST_PARAMS = {
headers: new Headers({
Authorization: `Bearer ${options.services["Asana"].accessToken}`,
"Content-Type": "application/x-www-form-urlencoded",
}),
};
const initAsana = async () => {
let res = await fetch("https://app.asana.com/api/1.0/users/me", ASANA_REST_PARAMS);
const userData = await res.json();
let personalWorkspaceID = null;
userData.data.workspaces.forEach((workspace) => {
if (workspace.name == options.services["Asana"].workspace) personalWorkspaceID = workspace.gid;
});
res = await fetch(
`https://app.asana.com/api/1.0/users/me/favorites?resource_type=tag&workspace=${personalWorkspaceID}`,
ASANA_REST_PARAMS
);
if (res.status === 200) {
const tags = await res.json();
tags.data.forEach((tag) => {
if (tag.name == options.services["Asana"].tag) asanaTagId = tag.gid;
});
}
};
const getAsanaTask = async () => {
if (!asanaTagId) return null;
let res = await fetch(
`https://app.asana.com/api/1.0/tags/${asanaTagId}/tasks?opt_fields=name,completed&limit=1`,
ASANA_REST_PARAMS
);
let data = await res.json();
if (data.data.length === 0) return null;
const { name, completed } = data.data[0];
if (!completed) {
return {
name: name,
};
} else {
return null;
}
};
export const init = async (dispatch) => {
if (options.services["Asana"].enabled) {
await initAsana();
}
};
// Things Integration
const getThingsTask = async () => {
let data = await run(`osascript current-task/lib/things/main.scpt ${options.services["Things3"].tag}`);
if (data === "") return null;
return {
name: data,
};
};
// State loop
export const initialState = {
task: null,
};
export const command = async (dispatch) => {
let task = null;
if (options.services["Asana"].enabled) {
task = await getAsanaTask(dispatch);
}
if (options.services["Things3"].enabled) {
task = await getThingsTask(dispatch);
}
dispatch({ type: "FETCH_SUCCEEDED", data: task });
};
// Update state
export const updateState = ({ type, data, error }, previousState) => {
switch (type) {
case "UB/COMMAND_RAN":
return previousState;
case "FETCH_SUCCEEDED":
console.log(data);
return {
task: data,
};
case "FETCH_FAILED":
return previousState;
default:
console.error(`Invalid dispatch type: ${type}`);
return previousState;
}
};
// React element
export const className = `
pointer-events: none;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
color: white;
* {
box-sizing: border-box;
padding: 0;
border: 0;
margin: 0;
}
`;
const wrapperPos = ({ horizontal, vertical }) => {
if (horizontal === "center" && vertical === "center") {
return `
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
`;
}
let hPos, vPos;
switch (horizontal) {
case "left":
hPos = `left: 20px;`;
break;
case "center":
hPos = `left: 50%; transform: translateX(-50%);`;
break;
case "right":
hPos = `right: 20px;`;
break;
default:
hPos = horizontal.startsWith("-") ? `right: ${parseInt(horizontal) * -1}px;` : `left: ${horizontal}px;`;
break;
}
switch (vertical) {
case "top":
vPos = `top: 20px;`;
break;
case "center":
vPos = `top: 50%; transform: translateY(-50%);`;
break;
case "bottom":
vPos = `bottom: 20px;`;
break;
default:
vPos = vertical.startsWith("-") ? `bottom: ${parseInt(vertical) * -1}px;` : `top: ${vertical}px;`;
break;
}
return `${hPos} ${vPos}`;
};
const Wrapper = styled("div")`
position: absolute;
opacity: ${(props) => (props.show ? 1 : 0)};
${wrapperPos}
`;
const Prefix = styled("span")`
font-size: 1.8em;
font-weight: 400;
`;
const Task = styled("span")`
font-size: 1.8em;
font-weight: 200;
`;
export const render = (state, dispatch) => {
const { verticalPosition, horizontalPosition, alwaysShow } = options;
return (
<Wrapper horizontal={horizontalPosition} vertical={verticalPosition} show={state.task || alwaysShow}>
<Prefix>Current Task - </Prefix> <Task>{state.task && state.task.name}</Task>
</Wrapper>
);
};