Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.33.2 - mACF default, stay signed into Google Sheets #319

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "modaq",
"version": "1.33.1",
"version": "1.33.2",
"description": "Quiz Bowl Reader using TypeScript, React, and MobX",
"repository": {
"type": "git",
Expand All @@ -21,9 +21,9 @@
"devDependencies": {
"@babel/core": "^7.25.2",
"@types/chai": "^4.2.15",
"@types/google.accounts": "^0.0.2",
"@types/gapi.client.sheets": "^4.0.20201030",
"@types/gapi.client": "^1.0.5",
"@types/google.accounts": "^0.0.15",
"@types/gapi.client.sheets": "^4.0.20201031",
"@types/gapi.client": "^1.0.8",
"@types/he": "^1.1.2",
"@types/mocha": "^8.2.1",
"@types/react": "^17.0.3",
Expand Down
43 changes: 32 additions & 11 deletions src/sheets/SheetsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,37 @@ const timedOutMessage = "Timed out signing into Google Sheets";

export const SheetsApi: ISheetsApi = {
initializeIfNeeded: async (uiState: UIState): Promise<void> => {
if (
uiState.sheetsState.apiInitialized === LoadingState.Loading ||
uiState.sheetsState.apiInitialized === LoadingState.Loaded
// If it's loaded and the token has expired, prompt again
if (uiState.sheetsState.apiInitialized === LoadingState.Loading) {
return;
} else if (
uiState.sheetsState.apiInitialized === LoadingState.Loaded &&
uiState.sheetsState.expiresAt != undefined &&
uiState.sheetsState.expiresAt > Date.now()
) {
return;
}

const initialState: LoadingState = uiState.sheetsState.apiInitialized;

// Bit of a hacky wait to wait until the callback is done
// Need to follow this approach: https://developers.google.com/identity/oauth2/web/guides/migration-to-gis#gapi-asyncawait
uiState.sheetsState.setSheetsApiInitialized(LoadingState.Loading);

const promise: Promise<void> = new Promise<void>(async (resolve, reject) => {
try {
// Load gapi first, then load the GIS client
await new Promise<void>((resolve) => {
gapi.load("client", resolve);
});
// Load gapi first, then load the GIS client. Only load gapi stuff the first time we're through, however.
if (initialState === LoadingState.Unloaded) {
await new Promise<void>((resolve) => {
gapi.load("client", resolve);
});

// Typings issue, init isn't listed as a method
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (gapi.client as any).init({});
// Typings issue, init isn't listed as a method
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await (gapi.client as any).init({});

await gapi.client.load("https://sheets.googleapis.com/$discovery/rest?version=v4");
await gapi.client.load("https://sheets.googleapis.com/$discovery/rest?version=v4");
}

const clientId: string | undefined = uiState.sheetsState.clientId;
if (clientId == undefined) {
Expand All @@ -53,8 +61,13 @@ export const SheetsApi: ISheetsApi = {
}
}, 2 * 60 * 1000);

// If the user already signed in, don't make them pick the account again
const prompt: "none" | "select_account" =
initialState === LoadingState.Loaded ? "none" : "select_account";

const tokenClient = google.accounts.oauth2.initTokenClient({
client_id: clientId,
prompt,
scope: "https://www.googleapis.com/auth/spreadsheets",
callback: (tokenResponse) => {
clearTimeout(cancelSignIn);
Expand All @@ -65,9 +78,17 @@ export const SheetsApi: ISheetsApi = {
return;
}

const expiresAt: number = Date.now() + parseInt(tokenResponse.expires_in) * 1000;
uiState.sheetsState.setExpiresAt(expiresAt);
uiState.sheetsState.setSheetsApiInitialized(LoadingState.Loaded);
resolve();
},
error_callback: (error) => {
clearTimeout(cancelSignIn);
uiState.sheetsState.setSheetsApiInitialized(LoadingState.Error);
reject(error.message);
return;
},
});

tokenClient.requestAccessToken();
Expand Down
14 changes: 13 additions & 1 deletion src/state/SheetState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import { IStatus } from "../IStatus";

export class SheetState {
@ignore
apiInitialized: LoadingState;
public apiInitialized: LoadingState;

public clientId: string | undefined;

@ignore
public expiresAt: number | undefined;

@ignore
public exportStatus: IStatus | undefined;

Expand All @@ -35,6 +38,7 @@ export class SheetState {

this.apiInitialized = LoadingState.Unloaded;
this.clientId = undefined;
this.expiresAt = undefined;
this.exportStatus = undefined;
this.exportState = undefined;
this.rosterLoadStatus = undefined;
Expand All @@ -44,6 +48,10 @@ export class SheetState {
this.sheetType = undefined;
}

public clearExpiresAt(): void {
this.expiresAt = undefined;
}

public clearExportStatus(): void {
this.exportStatus = undefined;
this.exportState = undefined;
Expand All @@ -61,6 +69,10 @@ export class SheetState {
this.clientId = clientId;
}

public setExpiresAt(expiresAt: number): void {
this.expiresAt = expiresAt;
}

public setExportStatus(status: IStatus, state: ExportState | undefined = undefined): void {
this.exportStatus = status;

Expand Down
2 changes: 1 addition & 1 deletion src/state/UIState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export class UIState {
this.pendingNewGame = {
packet: new PacketState(),
type: PendingGameType.Manual,
gameFormat: GameFormats.ACFGameFormat,
gameFormat: GameFormats.StandardPowersMACFGameFormat,
manual: {
firstTeamPlayers,
secondTeamPlayers,
Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,10 @@
dependencies:
"@maxim_mazurok/gapi.client.discovery-v1" latest

"@types/gapi.client.sheets@^4.0.20201030":
version "4.0.20201030"
resolved "https://registry.yarnpkg.com/@types/gapi.client.sheets/-/gapi.client.sheets-4.0.20201030.tgz#792160242b06441962de017f33c58b3413832a0a"
integrity sha512-U6sBNFNbIV8Z18jKDpK3m5ehyHXbAJKfT3kM84hb57/tkcWvfXFu84xg4PFKHHxTSUlL0arF1ikesNdM3hRrjw==
"@types/gapi.client.sheets@^4.0.20201031":
version "4.0.20201031"
resolved "https://registry.yarnpkg.com/@types/gapi.client.sheets/-/gapi.client.sheets-4.0.20201031.tgz#2d11a6805385c4bcfc9d147ab5eb288a87547953"
integrity sha512-1Aiu11rNNoyPDHW6v8TVcSmlDN+MkxSuafwiawaK5YqZ+uYA+O63vjUvkK+3qNduSLh7D9qBJc/8GGwgN6gsTw==
dependencies:
"@maxim_mazurok/gapi.client.sheets-v4" latest

Expand All @@ -786,15 +786,15 @@
resolved "https://registry.npmjs.org/@types/gapi.client/-/gapi.client-1.0.3.tgz"
integrity sha512-XhJ45zhedkqXln75Y0XZAFmHFHNu6zCGe9EyPiu5a20Q7XnsEENR3dnUqbYvp7w9e6vjXW+uKGqb3z/ezZijDg==

"@types/gapi.client@^1.0.5":
version "1.0.5"
resolved "https://registry.yarnpkg.com/@types/gapi.client/-/gapi.client-1.0.5.tgz#a6eb97e664fe51656c5b52258bd0afef28c76308"
integrity sha512-OTpbBMuzfC4lkvaomxqskI/iWRGW3zOZbDXZLNSyiuswTiSSGgILRLkg0POuZ4EgzEdaYaTlXpnXiCp07ri/Yw==
"@types/gapi.client@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@types/gapi.client/-/gapi.client-1.0.8.tgz#8e02c57493b014521f2fa3359166c01dc2861cd7"
integrity sha512-qJQUmmumbYym3Amax0S8CVzuSngcXsC1fJdwRS2zeW5lM63zXkw4wJFP+bG0jzgi0R6EsJKoHnGNVTDbOyG1ng==

"@types/google.accounts@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@types/google.accounts/-/google.accounts-0.0.2.tgz#4e03bd249fe490f004f4f8d6423343fc51f5614a"
integrity sha512-wmnbDIiGZNr3dJ+NCGiWme78rhsf5gn1YJM6uRjf3FCb8lrRptybQeAnHQ+GDfKSo+z2hRxhS32VRDXJEAvCmg==
"@types/google.accounts@^0.0.15":
version "0.0.15"
resolved "https://registry.yarnpkg.com/@types/google.accounts/-/google.accounts-0.0.15.tgz#88c6ceba6ecd8f54ae42891393d2fff0fcdb4ee2"
integrity sha512-NiydIuJzwdpnWNqTPOeDzGXhzz46Ll6T4rN3YWSm9Zfx/8ZfbwFbzyPSw1Tkz4uE3gQiU+fRj4KvBVUuHmk+KQ==

"@types/he@^1.1.2":
version "1.1.2"
Expand Down
Loading