Skip to content

Commit f2e0c83

Browse files
committed
Add recoil-nexus package and related states and selectors
1 parent 1170006 commit f2e0c83

File tree

7 files changed

+130
-95
lines changed

7 files changed

+130
-95
lines changed

package-lock.json

Lines changed: 25 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react": "^18.2.0",
2424
"react-dom": "^18.2.0",
2525
"recoil": "^0.7.7",
26+
"recoil-nexus": "^0.5.0",
2627
"tauri-plugin-context-menu": "^0.5.0",
2728
"tauri-plugin-log-api": "github:tauri-apps/tauri-plugin-log#v1",
2829
"tauri-plugin-store-api": "github:tauri-apps/tauri-plugin-store#v1"

src/App.tsx

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { useState , useEffect} from "react";
22
import { Box, Button, Spinner, useToast } from '@chakra-ui/react'
3-
import { fetchUserProfile,getUserProfileFromStorage, getAccessToken, openAuthWindow, saveAccessToken, saveAuthCode, saveUserProfile, getAccessTokenFromStorage, deleteAccessToken } from "./helpers/auth";
3+
import { fetchUserProfile,getUserProfileFromStorage, getAccessToken, openAuthWindow, saveAccessToken, saveAuthCode, saveUserProfile, getAccessTokenFromStorage, deleteAccessToken, handleInitialLogin, handleLoadFrom, handleLogin, handleLogout } from "./helpers/auth";
44
import { UserProfile } from "./types/googleapis";
55
import { loadContextmenu , pushNotification } from "./helpers/windowhelper";
66
import TaskPage from "./components/TaskPage";
77
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
8-
import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, loggedInSelector, messageState, userProfileState } from "./config/states";
8+
import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, attemptLoginState, attemptLogoutState, authLoadingState, loggedInSelector, messageState, userProfileState } from "./config/states";
99
import Header from "./components/ui/Header";
1010
import { task } from "./types/taskapi";
1111
import { listen_for_auth_code } from "./helpers/eventlistner";
12-
import { test_command } from "./helpers/invoker";
1312
import { AccessToken } from "./helpers/commands";
1413

1514
// disable default context menu on build
1615
loadContextmenu();
1716

1817
function App() {
19-
const [loading, setLoading] = useState<boolean>(false);
18+
const [loading, setLoading] = useRecoilState<boolean>(authLoadingState);
2019
const loggedIn = useRecoilValue(loggedInSelector);
2120
const setProfile = useSetRecoilState<UserProfile | null>(userProfileState);
2221
const setAccessToken = useSetRecoilState<string | null>(accessTokenState);
@@ -92,7 +91,7 @@ function App() {
9291
// check the offline data for access token
9392
useEffect(() => {
9493
setLoading(true)
95-
handleInitialLogin ().catch((err) => {
94+
handleInitialLogin().catch((err) => {
9695
console.log(err);
9796
setLoading(false)
9897
setToastMessage({
@@ -106,77 +105,8 @@ function App() {
106105
}, [])
107106

108107

109-
async function handleInitialLogin() {
110-
// get access token from storage
111-
const accessToken = await getAccessTokenFromStorage();
112-
if (!accessToken) throw new Error("Signin required");
113-
pushNotification("Login Successful")
114-
const profile = navigator.onLine ? await fetchUserProfile(accessToken.access_token) : await getUserProfileFromStorage();
115-
if(!profile || !profile?.email) throw new Error("Something went wrong, please try again");
116-
setProfile(profile);
117-
setAccessToken(accessToken.access_token);
118-
pushNotification(`welcome back ${profile.name}`)
119-
}
120-
121-
122-
async function handleLoadFrom(accessTokenBody: AccessToken) {
123-
try {
124-
await saveAccessToken(JSON.stringify(accessTokenBody, null, 2))
125-
setAccessToken(accessTokenBody.access_token);
126-
const userProfile = await fetchUserProfile(accessTokenBody.access_token);
127-
await saveUserProfile(userProfile)
128-
setProfile(userProfile);
129-
}
130-
catch (err) {
131-
console.log(err);
132-
setLoading(false)
133-
await handleLogout();
134-
setToastMessage({
135-
title: "Error",
136-
body: "Error signing in",
137-
type: "error"
138-
})
139-
}
140-
}
141108

142-
143-
144-
145-
async function handleLogout() {
146-
setLoading(true)
147-
setAccessToken(null);
148-
setProfile(null);
149-
setActiveTaskCategory(-1)
150-
setActiveCategoryTasksState([])
151-
await deleteAccessToken();
152-
setLoading(false)
153-
}
154-
155-
156109

157-
158-
159-
async function handleLogin() {
160-
setLoading(true)
161-
try {
162-
const storedAccessToken = await getAccessTokenFromStorage();
163-
if (storedAccessToken) {
164-
handleLoadFrom(storedAccessToken);
165-
pushNotification('Login Successful')
166-
return;
167-
}
168-
pushNotification('login required')
169-
await openAuthWindow();
170-
} catch (error) {
171-
console.log(error);
172-
setLoading(false)
173-
setToastMessage({
174-
title: "Error",
175-
body: "Error signing in",
176-
type: "error"
177-
})
178-
}
179-
}
180110

181111

182112
return (

src/config/states.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,19 @@ const messageState = atom<{ title: string, body?: string , type: "info" | "warni
5353
default: null,
5454
});
5555

56+
const authLoadingState = atom<boolean>({
57+
key: 'authLoading',
58+
default: false,
59+
});
60+
5661

62+
const authLoadingSelector = selector({
63+
key: 'authLoadingSelector',
64+
get: ({ get }) => {
65+
const loading = get(authLoadingState);
66+
return loading;
67+
},
68+
});
5769

5870

5971
const loggedInSelector = selector({
@@ -163,4 +175,6 @@ export {
163175
attemptLogoutSelector,
164176
messageState,
165177
messageSelector,
178+
authLoadingState,
179+
authLoadingSelector
166180
};

src/helpers/auth.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { CLIENT_ID, CLIENT_SECRET } from "../config/credentials";
66
import settings from "../config/settings";
77
import { generate_oauth_port, get_access_token, get_auth_code, save_access_token, save_auth_code } from "./invoker";
88
import { AccessToken } from "./commands";
9+
import { getRecoil, setRecoil } from "recoil-nexus";
10+
import { accessTokenState, activeCategoryTasksState, activeTaskCategoryState, authLoadingState, messageState, userProfileState } from "../config/states";
11+
import { pushNotification } from "./windowhelper";
912

1013

1114
const DEFAULT_DIRECTORY = settings.fs.DEFAULT_DIRECTORY;
@@ -206,6 +209,79 @@ export async function getUserProfileFromStorage() {
206209
}
207210

208211

212+
export async function handleLogin() {
213+
setRecoil(authLoadingState, true)
214+
try {
215+
const storedAccessToken = await getAccessTokenFromStorage();
216+
if (storedAccessToken) {
217+
handleLoadFrom(storedAccessToken);
218+
pushNotification('Login Successful')
219+
return;
220+
}
221+
pushNotification('login required')
222+
await openAuthWindow();
223+
} catch (error) {
224+
console.log(error);
225+
setRecoil(authLoadingState, false)
226+
setRecoil(messageState, {
227+
title: "Error",
228+
body: "Error signing in",
229+
type: "error"
230+
})
231+
}
232+
}
233+
234+
235+
export async function handleLoadFrom(accessTokenBody: AccessToken) {
236+
try {
237+
await saveAccessToken(JSON.stringify(accessTokenBody, null, 2))
238+
setRecoil(accessTokenState, accessTokenBody.access_token);
239+
// setAccessToken(accessTokenBody.access_token);
240+
const userProfile = await fetchUserProfile(accessTokenBody.access_token);
241+
await saveUserProfile(userProfile)
242+
setRecoil(userProfileState, userProfile);
243+
}
244+
catch (err) {
245+
console.log(err);
246+
setRecoil(authLoadingState, false)
247+
await handleLogout();
248+
setRecoil(messageState, {
249+
title: "Error",
250+
body: "Error signing in",
251+
type: "error"
252+
})
253+
}
254+
}
255+
256+
export async function handleLogout() {
257+
setRecoil(authLoadingState, true)
258+
setRecoil(accessTokenState, null);
259+
// setAccessToken(null);
260+
setRecoil(userProfileState, null);
261+
// setProfile(null);
262+
// setActiveTaskCategory(-1)
263+
setRecoil(activeTaskCategoryState, -1)
264+
// setActiveCategoryTasksState([])
265+
setRecoil(activeCategoryTasksState, [])
266+
await deleteAccessToken();
267+
setRecoil(authLoadingState, false)
268+
}
269+
270+
271+
export async function handleInitialLogin() {
272+
// get access token from storage
273+
const accessToken = await getAccessTokenFromStorage();
274+
if (!accessToken) throw new Error("Signin required");
275+
pushNotification("Login Successful")
276+
const profile = navigator.onLine ? await fetchUserProfile(accessToken.access_token) : await getUserProfileFromStorage();
277+
if(!profile || !profile?.email) throw new Error("Something went wrong, please try again");
278+
// setProfile(profile);
279+
setRecoil(userProfileState, profile);
280+
// setAccessToken(accessToken.access_token);
281+
setRecoil(accessTokenState, accessToken.access_token);
282+
pushNotification(`welcome back ${profile.name}`)
283+
}
284+
209285

210286

211287

src/helpers/windowhelper.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export function loadContextmenu() {
2222
}, { capture: true })
2323
}
2424

25+
26+
// TODO: Remove the counter
27+
let notificationCount = 0;
2528
export function pushNotification(options: string | Options) {
2629
isPermissionGranted().then((granted) => {
2730
if (!granted) {
@@ -32,8 +35,11 @@ export function pushNotification(options: string | Options) {
3235
}
3336
});
3437
}
35-
console.log("permission granted");
36-
sendNotification(options);
38+
console.log("permission granted");
39+
if (typeof options === 'object') options.body = `${options.body} ${notificationCount}`;
40+
if (typeof options === 'string') options = `${options} ${notificationCount}`;
41+
sendNotification(options);
42+
notificationCount++;
3743
});
3844
}
3945

0 commit comments

Comments
 (0)