Skip to content

Commit

Permalink
Upgrade Import Modal (#1008)
Browse files Browse the repository at this point in the history
  • Loading branch information
HunterBarclay authored Jul 15, 2024
2 parents 7e6c913 + 73a3356 commit b2a5a06
Show file tree
Hide file tree
Showing 18 changed files with 810 additions and 242 deletions.
2 changes: 0 additions & 2 deletions fission/.env

This file was deleted.

Binary file removed fission/bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions fission/src/Synthesis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ import DriverStationPanel from "@/panels/simulation/DriverStationPanel"
import ManageAssembliesModal from "@/modals/spawning/ManageAssembliesModal.tsx"
import World from "@/systems/World.ts"
import { AddRobotsModal, AddFieldsModal, SpawningModal } from "@/modals/spawning/SpawningModals.tsx"
import ImportMirabufModal from "@/modals/mirabuf/ImportMirabufModal.tsx"
import ImportLocalMirabufModal from "@/modals/mirabuf/ImportLocalMirabufModal.tsx"
import APS from "./aps/APS.ts"
import ImportMirabufPanel from "@/ui/panels/mirabuf/ImportMirabufPanel.tsx"
import Skybox from "./ui/components/Skybox.tsx"
import PokerPanel from "@/panels/PokerPanel.tsx"

Expand Down Expand Up @@ -212,7 +212,6 @@ const initialModals = [
<MatchModeModal key="match-mode" modalId="match-mode" />,
<ConfigMotorModal key="config-motor" modalId="config-motor" />,
<ManageAssembliesModal key="manage-assemblies" modalId="manage-assemblies" />,
<ImportMirabufModal key="import-mirabuf" modalId="import-mirabuf" />,
<ImportLocalMirabufModal key="import-local-mirabuf" modalId="import-local-mirabuf" />,
]

Expand All @@ -225,6 +224,7 @@ const initialPanels: ReactElement[] = [
<ConfigureShotTrajectoryPanel key="config-shot-trajectory" panelId="config-shot-trajectory" />,
<ScoringZonesPanel key="scoring-zones" panelId="scoring-zones" />,
<ZoneConfigPanel key="zone-config" panelId="zone-config" />,
<ImportMirabufPanel key="import-mirabuf" panelId="import-mirabuf" />,
<PokerPanel key="poker" panelId="poker" />,
]

Expand Down
49 changes: 34 additions & 15 deletions fission/src/aps/APS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ export const APS_USER_INFO_UPDATE_EVENT = "aps_user_info_update"

const CLIENT_ID = "GCxaewcLjsYlK8ud7Ka9AKf9dPwMR3e4GlybyfhAK2zvl3tU"

const ENDPOINT_SYNTHESIS_CODE = `${import.meta.env.VITE_SYNTHESIS_SERVER_PATH}api/aps/code`
export const ENDPOINT_SYNTHESIS_CHALLENGE = `${import.meta.env.VITE_SYNTHESIS_SERVER_PATH}api/aps/challenge`
const ENDPOINT_SYNTHESIS_CODE = `/api/aps/code`
export const ENDPOINT_SYNTHESIS_CHALLENGE = `/api/aps/challenge`

const ENDPOINT_AUTODESK_AUTHENTICATION_AUTHORIZE = "https://developer.api.autodesk.com/authentication/v2/authorize"
const ENDPOINT_AUTODESK_AUTHENTICATION_TOKEN = "https://developer.api.autodesk.com/authentication/v2/token"
const ENDPOINT_AUTODESK_USERINFO = "https://api.userprofile.autodesk.com/userinfo"

interface APSAuth {
export interface APSAuth {
access_token: string
refresh_token: string
expires_in: number
expires_at: number
token_type: number
}

interface APSUserInfo {
export interface APSUserInfo {
name: string
picture: string
givenName: string
Expand Down Expand Up @@ -73,7 +73,14 @@ class APS {
* Returns the auth data of the current user. See {@link APSAuth}
* @returns {(APSAuth | undefined)} Auth data of the current user
*/
static getAuth(): APSAuth | undefined {
static async getAuth(): Promise<APSAuth | undefined> {
const auth = this.auth
if (!auth) return undefined

if (Date.now() > auth.expires_at) {
console.debug("Expired. Refreshing...")
await this.refreshAuthToken(auth.refresh_token, false)
}
return this.auth
}

Expand All @@ -83,10 +90,13 @@ class APS {
*/
static async getAuthOrLogin(): Promise<APSAuth | undefined> {
const auth = this.auth
if (!auth) return undefined
if (!auth) {
this.requestAuthCode()
return undefined
}

if (Date.now() > auth.expires_at) {
await this.refreshAuthToken(auth.refresh_token)
await this.refreshAuthToken(auth.refresh_token, true)
}
return this.auth
}
Expand Down Expand Up @@ -158,9 +168,12 @@ class APS {
/**
* Refreshes the access token using our refresh token.
* @param {string} refresh_token - The refresh token from our auth data
*
* @returns If the promise returns true, that means the auth token is currently available. If not, it means it
* is not readily available, although one may be in the works
*/
static async refreshAuthToken(refresh_token: string) {
await this.requestMutex.runExclusive(async () => {
static async refreshAuthToken(refresh_token: string, shouldRelog: boolean): Promise<boolean> {
return this.requestMutex.runExclusive(async () => {
try {
const res = await fetch(ENDPOINT_AUTODESK_AUTHENTICATION_TOKEN, {
method: "POST",
Expand All @@ -176,23 +189,29 @@ class APS {
})
const json = await res.json()
if (!res.ok) {
MainHUD_AddToast("error", "Error signing in.", json.userMessage)
this.auth = undefined
await this.requestAuthCode()
return
if (shouldRelog) {
MainHUD_AddToast("warning", "Must Re-signin.", json.userMessage)
this.auth = undefined
await this.requestAuthCode()
return false
} else {
return false
}
}
json.expires_at = json.expires_in + Date.now()
json.expires_at = json.expires_in * 1000 + Date.now()
this.auth = json as APSAuth
if (this.auth) {
await this.loadUserInfo(this.auth)
if (APS.userInfo) {
MainHUD_AddToast("info", "ADSK Login", `Hello, ${APS.userInfo.givenName}`)
}
}
return true
} catch (e) {
MainHUD_AddToast("error", "Error signing in.", "Please try again.")
this.auth = undefined
await this.requestAuthCode()
return false
}
})
}
Expand All @@ -212,7 +231,7 @@ class APS {
return
}
const auth_res = json.response as APSAuth
auth_res.expires_at = auth_res.expires_in + Date.now()
auth_res.expires_at = auth_res.expires_in * 1000 + Date.now()
this.auth = auth_res
console.log("Preloading user info")
const auth = await this.getAuth()
Expand Down
Loading

0 comments on commit b2a5a06

Please sign in to comment.