-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(providers): Use classes instead of fns, new PlayerProviders
- Loading branch information
Showing
9 changed files
with
137 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createContext, useContext, useCallback, useMemo } from "react"; | ||
import type { ReactNode } from "@tanstack/react-router"; | ||
|
||
import providers from "@/providers"; | ||
import { IProviderClient } from "@/types/providers/client"; | ||
|
||
const PlayerProvidersContext = createContext<{ | ||
[key: string]: IProviderClient; | ||
}>({}); | ||
|
||
export function PlayerProvidersProvider({ | ||
children, | ||
}: Readonly<{ | ||
children: ReactNode; | ||
}>) { | ||
const handleAuth = useCallback((provider: string) => { | ||
console.log("auth", provider); | ||
}, []); | ||
|
||
const value = useMemo(() => { | ||
return Object.fromEntries( | ||
providers.map(([id, provider]) => { | ||
return [ | ||
id, | ||
new provider({ | ||
onAuth: () => handleAuth(id), | ||
}), | ||
]; | ||
}) | ||
); | ||
}, []); | ||
|
||
return ( | ||
<PlayerProvidersContext.Provider value={value}> | ||
{children} | ||
</PlayerProvidersContext.Provider> | ||
); | ||
} | ||
|
||
export function usePlayerProviders() { | ||
return useContext(PlayerProvidersContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { ProviderMeta } from "@/types/providers/meta"; | ||
import type { | ||
IProviderClient, | ||
IProviderClientConstructor, | ||
} from "@/types/providers/client"; | ||
|
||
const providersGlob = import.meta.glob<ProviderMeta>("./*/index.ts", { | ||
eager: true, | ||
import: "default", | ||
}); | ||
type NewProviderClient = new ( | ||
args: IProviderClientConstructor | ||
) => IProviderClient; | ||
|
||
const providers = Object.fromEntries( | ||
Object.values(providersGlob).map((value) => [value.id, value]) | ||
const providersClientGlob = import.meta.glob<NewProviderClient>( | ||
"./*/client.ts", | ||
{ | ||
eager: true, | ||
import: "default", | ||
} | ||
); | ||
|
||
const providers: [string, NewProviderClient][] = Object.entries( | ||
providersClientGlob | ||
).map(([importPath, client]) => { | ||
const [, id] = importPath.split("/"); | ||
|
||
return [id, client]; | ||
}); | ||
|
||
export default providers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,50 @@ | ||
import { SPOTIFY_OAUTH_SCOPES } from "./constants"; | ||
import { | ||
AccessToken, | ||
SpotifyApi, | ||
AuthorizationCodeWithPKCEStrategy, | ||
} from "@spotify/web-api-ts-sdk"; | ||
|
||
import { providerConfig } from "./config"; | ||
import { | ||
IProviderClient, | ||
IProviderClientConstructor, | ||
} from "@/types/providers/client"; | ||
import spotifyProviderMeta from "@/providers/spotify"; | ||
|
||
const { VITE_SPOTIFY_CLIENT_ID, VITE_SPOTIFY_REDIRECT_URI } = providerConfig; | ||
|
||
export async function authenticate(callback: (token: AccessToken) => void) { | ||
const auth = new AuthorizationCodeWithPKCEStrategy( | ||
VITE_SPOTIFY_CLIENT_ID, | ||
VITE_SPOTIFY_REDIRECT_URI, | ||
[...SPOTIFY_OAUTH_SCOPES] | ||
); | ||
const client = new SpotifyApi(auth); | ||
export default class SpotifyProvider implements IProviderClient { | ||
readonly meta = spotifyProviderMeta; | ||
isAuthenticated = false; | ||
|
||
// API event handlers | ||
private onAuth: () => void; | ||
|
||
constructor({ onAuth }: IProviderClientConstructor) { | ||
this.onAuth = onAuth; | ||
} | ||
|
||
try { | ||
const { authenticated } = await client.authenticate(); | ||
async authenticate() { | ||
const auth = new AuthorizationCodeWithPKCEStrategy( | ||
VITE_SPOTIFY_CLIENT_ID, | ||
VITE_SPOTIFY_REDIRECT_URI, | ||
[...SPOTIFY_OAUTH_SCOPES] | ||
); | ||
const client = new SpotifyApi(auth); | ||
|
||
if (authenticated) { | ||
console.log("Authenticated"); | ||
try { | ||
const { authenticated } = await client.authenticate(); | ||
|
||
if (authenticated) { | ||
this.isAuthenticated = true; | ||
this.onAuth(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
|
||
async callback() { | ||
await this.authenticate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import type { ProviderMeta } from "@/types/providers/meta"; | ||
|
||
export default { | ||
const spotifyProviderMeta: ProviderMeta = { | ||
name: "Spotify", | ||
id: "spotify", | ||
auth: (await import("./client")).authenticate, | ||
callback: (await import("./client")).authenticate, | ||
} as ProviderMeta; | ||
}; | ||
|
||
export default spotifyProviderMeta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ProviderMeta } from "@/types/providers/meta"; | ||
|
||
export interface IProviderClientConstructor { | ||
onAuth: () => void; | ||
} | ||
|
||
export interface IProviderClient { | ||
readonly meta: ProviderMeta; | ||
|
||
isAuthenticated: boolean; | ||
|
||
authenticate(): Promise<void>; | ||
callback(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
export interface ProviderMeta { | ||
name: string; | ||
id: string; | ||
auth: (callback?: (token: any) => void) => any; | ||
callback: (callback?: (token: any) => void) => any; | ||
} |