|
| 1 | +import * as React from "react"; |
| 2 | +import { useState, useEffect } from "react"; |
| 3 | + |
| 4 | +export interface EosSdkVersionInfo { |
| 5 | + freeEditionVersion: string; |
| 6 | + availableVersions: string[]; |
| 7 | +} |
| 8 | + |
| 9 | +export const EosSdkVersionContext = React.createContext< |
| 10 | + EosSdkVersionInfo | undefined |
| 11 | +>(undefined); |
| 12 | + |
| 13 | +async function fetchFreeEditionVersion(): Promise<string> { |
| 14 | + const response = await fetch( |
| 15 | + "https://licensing-api.redpoint.games/api/dependency-version/eos-online-subsystem-free" |
| 16 | + ); |
| 17 | + const data = await response.text(); |
| 18 | + return data; |
| 19 | +} |
| 20 | + |
| 21 | +async function fetchAvailableVersions(): Promise<string[]> { |
| 22 | + const response = await fetch( |
| 23 | + "https://licensing-api.redpoint.games/api/eos-sdk-supported-versions" |
| 24 | + ); |
| 25 | + const data = await response.text(); |
| 26 | + return data |
| 27 | + .split("\n") |
| 28 | + .map((x) => x.trim()) |
| 29 | + .filter((x) => x !== ""); |
| 30 | +} |
| 31 | + |
| 32 | +async function fetchEosSdkVersionInfo(): Promise<EosSdkVersionInfo> { |
| 33 | + const results = await Promise.all([ |
| 34 | + fetchFreeEditionVersion(), |
| 35 | + fetchAvailableVersions(), |
| 36 | + ]); |
| 37 | + return { |
| 38 | + freeEditionVersion: results[0], |
| 39 | + availableVersions: results[1], |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export default function EosSdkVersionProvider(props: { |
| 44 | + children?: React.ReactNode; |
| 45 | +}) { |
| 46 | + const [data, setData] = useState<EosSdkVersionInfo | undefined>(undefined); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + const fetchData = async () => { |
| 50 | + setData(await fetchEosSdkVersionInfo()); |
| 51 | + }; |
| 52 | + fetchData(); |
| 53 | + }, []); |
| 54 | + |
| 55 | + return ( |
| 56 | + <EosSdkVersionContext.Provider value={data}> |
| 57 | + {props.children} |
| 58 | + </EosSdkVersionContext.Provider> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +export function LoadingText(props: { children: string }) { |
| 63 | + return ( |
| 64 | + <span className="loading-animation" aria-label={props.children}> |
| 65 | + {props.children |
| 66 | + .toString() |
| 67 | + .split("") |
| 68 | + .map((val, idx) => ( |
| 69 | + <span |
| 70 | + key={idx} |
| 71 | + style={{ |
| 72 | + animationDelay: |
| 73 | + (props.children.toString().length - idx) * -100 + "ms", |
| 74 | + }} |
| 75 | + > |
| 76 | + {val} |
| 77 | + </span> |
| 78 | + ))} |
| 79 | + </span> |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +export function PendingEosSdkVersion() { |
| 84 | + return ( |
| 85 | + <span |
| 86 | + className="loading-animation" |
| 87 | + aria-label="The required EOS SDK version has not loaded from our API server yet." |
| 88 | + > |
| 89 | + {"████████-v██████" |
| 90 | + .toString() |
| 91 | + .split("") |
| 92 | + .map((val, idx) => ( |
| 93 | + <span |
| 94 | + key={idx} |
| 95 | + style={{ |
| 96 | + animationDelay: |
| 97 | + ("████████-v██████".toString().length - idx) * -100 + "ms", |
| 98 | + }} |
| 99 | + > |
| 100 | + {val} |
| 101 | + </span> |
| 102 | + ))} |
| 103 | + </span> |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +export function ConditionalEosSdkVersion(props: { version?: string }) { |
| 108 | + if (props.version !== undefined) { |
| 109 | + return <strong>{props.version}</strong>; |
| 110 | + } else { |
| 111 | + return <PendingEosSdkVersion />; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export function CodeWithEosSdkVersionSuffix(props: { |
| 116 | + children?: React.ReactNode; |
| 117 | +}) { |
| 118 | + return ( |
| 119 | + <EosSdkVersionContext.Consumer> |
| 120 | + {(value) => { |
| 121 | + if (value === undefined) { |
| 122 | + return ( |
| 123 | + <code> |
| 124 | + {props.children} |
| 125 | + <LoadingText>████████-v██████</LoadingText> |
| 126 | + </code> |
| 127 | + ); |
| 128 | + } else { |
| 129 | + return ( |
| 130 | + <code> |
| 131 | + {props.children} |
| 132 | + {value} |
| 133 | + </code> |
| 134 | + ); |
| 135 | + } |
| 136 | + }} |
| 137 | + </EosSdkVersionContext.Consumer> |
| 138 | + ); |
| 139 | +} |
0 commit comments