-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.ts
166 lines (155 loc) · 5.52 KB
/
background.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
import {
IS_TOKEN_REFRESHING_KEY,
backgroundDataRefresh,
refreshToken
} from "~lib/backgroundRefresh";
import {
BACKGROUND_ALARM_NAME,
CHROME_LOCAL_STORAGE,
CHROME_SESSION_STORAGE,
IS_DEV,
REFRESH_SIGNAL_KEY,
TOKEN_STORAGE
} from "~lib/constants";
import {
checkBrowserBarPermission,
createBrowserBarSuggestions,
getBrowserBarBudgets,
getBrowserBarDataForBudget,
getPossibleTransferFieldCombinations,
getPossibleTxFieldCombinations,
parseTxInput
} from "~lib/omnibox";
import type { PopupState, TxAddInitialState } from "~lib/types";
import { searchWithinString, waitForInternetConnection } from "~lib/utils";
// Listen for token refresh signal
TOKEN_STORAGE.watch({
[REFRESH_SIGNAL_KEY]: async (c) => {
if (
c.newValue !== true ||
(await CHROME_SESSION_STORAGE.get<boolean>(IS_TOKEN_REFRESHING_KEY))
)
return;
await refreshToken();
}
});
// Setup periodic background refresh
chrome.alarms.onAlarm.addListener(async (alarm: chrome.alarms.Alarm) => {
if (alarm.name === BACKGROUND_ALARM_NAME) {
try {
await waitForInternetConnection();
await backgroundDataRefresh();
} catch (err) {
console.error("Background refresh: Error", err);
}
}
});
chrome.alarms.get(BACKGROUND_ALARM_NAME).then(async (alarm) => {
if (!alarm) {
IS_DEV && console.log("Setting up new alarm!");
await chrome.alarms.clearAll();
await chrome.alarms.create(BACKGROUND_ALARM_NAME, {
periodInMinutes: 15
});
} else {
IS_DEV && console.log("Alarm already exists!");
}
});
// Setup system notification click handler
const onSystemNotificationClick = (id: string) => {
chrome.notifications.clear(id);
chrome.tabs.create({ url: `https://app.ynab.com/${id}/budget` });
};
chrome.notifications?.onClicked.removeListener(onSystemNotificationClick);
chrome.notifications?.onClicked.addListener(onSystemNotificationClick);
// Setup browser bar
const OMNIBOX_START_TEXT = "add <dim>or</dim> transfer";
chrome.omnibox.onInputStarted.addListener(async () => {
if (!(await checkBrowserBarPermission())) {
chrome.omnibox.setDefaultSuggestion({
description: "URL/address bar not enabled in settings!"
});
} else {
chrome.omnibox.setDefaultSuggestion({
description: OMNIBOX_START_TEXT
});
const budgetId = (await CHROME_LOCAL_STORAGE.get<PopupState>("popupState"))?.budgetId;
if (budgetId) getBrowserBarDataForBudget(budgetId);
}
});
chrome.omnibox.onInputCancelled.addListener(async () => {
if (!(await checkBrowserBarPermission())) {
chrome.omnibox.setDefaultSuggestion({
description: "URL/address bar not enabled in settings!"
});
} else {
chrome.omnibox.setDefaultSuggestion({
description: OMNIBOX_START_TEXT
});
}
});
chrome.omnibox.onInputChanged.addListener(async (text, suggest) => {
if (!(await checkBrowserBarPermission())) return;
chrome.omnibox.setDefaultSuggestion({
description: text.startsWith("add")
? "add (<dim>amount</dim>) (in <dim>budget</dim>) (at <dim>payee</dim>) (for <dim>category</dim>) (on <dim>account</dim>) (memo <dim>memo</dim>)"
: text.startsWith("transfer")
? "transfer (<dim>amount</dim>) (in <dim>budget</dim>) (from|to <dim>account</dim>) (from|to <dim>account</dim>) (for <dim>category</dim>) (memo <dim>memo</dim>)"
: OMNIBOX_START_TEXT
});
const parsedQuery = parseTxInput(text);
if (!parsedQuery) return;
const budgets = await getBrowserBarBudgets();
const budgetId = parsedQuery.budgetQuery
? budgets.find((b) => searchWithinString(b.name, parsedQuery.budgetQuery!.trim()))?.id
: (await CHROME_LOCAL_STORAGE.get<PopupState>("popupState"))?.budgetId;
if (!budgetId) {
chrome.omnibox.setDefaultSuggestion({ description: "Budget not found!" });
return;
}
const data = await getBrowserBarDataForBudget(budgetId);
const possibleTxFields =
parsedQuery.type === "tx"
? getPossibleTxFieldCombinations(parsedQuery, data)
: getPossibleTransferFieldCombinations(parsedQuery, data);
suggest(
createBrowserBarSuggestions(
parsedQuery.type,
possibleTxFields,
parsedQuery.budgetQuery ? budgets.find((b) => b.id === budgetId) : undefined,
parsedQuery.amount,
parsedQuery.memo,
parsedQuery.type === "tx" ? parsedQuery.amountType : undefined
)
);
});
chrome.omnibox.onInputEntered.addListener(async (text) => {
if (!(await checkBrowserBarPermission())) return;
const parsedQuery = parseTxInput(text);
if (!parsedQuery) return;
const budgets = await getBrowserBarBudgets();
const budgetId = parsedQuery.budgetQuery
? budgets.find((b) => searchWithinString(b.name, parsedQuery.budgetQuery!.trim()))?.id
: (await CHROME_LOCAL_STORAGE.get<PopupState>("popupState"))?.budgetId;
if (!budgetId) return;
const data = await getBrowserBarDataForBudget(budgetId);
const [tx] =
parsedQuery.type === "tx"
? getPossibleTxFieldCombinations(parsedQuery, data)
: getPossibleTransferFieldCombinations(parsedQuery, data);
IS_DEV && console.log("Received tx fields from omnibox:", tx);
await CHROME_LOCAL_STORAGE.set("txState", {
amount: parsedQuery.amount,
amountType: "amountType" in parsedQuery ? parsedQuery.amountType : "Outflow",
payee: tx?.payee,
accountId: tx?.account?.id,
categoryId: tx?.category?.id,
memo: parsedQuery.memo?.trim(),
isTransfer: parsedQuery.type === "transfer"
} satisfies TxAddInitialState);
await CHROME_LOCAL_STORAGE.set("popupState", {
view: "txAdd",
budgetId: budgetId
} satisfies PopupState);
chrome.action.openPopup();
});