1- import { WithPrivateUser } from "web/components/user/with-user" ;
2- import { PrivateUser } from "common/user" ;
3- import { Col } from "web/components/layout/col" ;
4- import { HOSTING_ENV , IS_VERCEL } from "common/hosting/constants" ;
5- import { Capacitor } from "@capacitor/core" ;
6- import { LiveUpdate } from "@capawesome/capacitor-live-update" ;
7- import { useEffect , useState } from "react" ;
8- import { App } from "@capacitor/app" ;
9- import { api } from "web/lib/api" ;
10- import { githubRepo } from "common/constants" ;
11- import { CustomLink } from "web/components/links" ;
1+ import { WithPrivateUser } from "web/components/user/with-user"
2+ import { PrivateUser } from "common/user"
3+ import { Col } from "web/components/layout/col"
4+ import { HOSTING_ENV , IS_VERCEL } from "common/hosting/constants"
5+ import { Capacitor } from "@capacitor/core"
6+ import { LiveUpdate } from "@capawesome/capacitor-live-update"
7+ import { useEffect , useState } from "react"
8+ import { App } from "@capacitor/app"
9+ import { api } from "web/lib/api"
10+ import { githubRepo } from "common/constants"
11+ import { CustomLink } from "web/components/links"
12+ import { Button } from "web/components/buttons/button"
13+
14+ export type WebBuild = {
15+ gitSha ?: string
16+ gitMessage ?: string
17+ deploymentId ?: string
18+ environment ?: string
19+ }
20+
21+ export type LiveUpdateInfo = {
22+ bundleId ?: string | null
23+ commitSha ?: string
24+ commitMessage ?: string
25+ }
26+
27+ export type Android = {
28+ appVersion ?: string
29+ buildNumber ?: string
30+ liveUpdate ?: LiveUpdateInfo
31+ }
32+
33+ export type Backend = {
34+ version ?: string
35+ gitSha ?: string
36+ gitMessage ?: string
37+ commitDate ?: string
38+ }
39+
40+ export type Runtime = {
41+ platform : string
42+ }
43+
44+ export type Diagnostics = {
45+ web ?: WebBuild ,
46+ android ?: Android
47+ backend ?: Backend
48+ runtime : Runtime
49+ }
50+
51+ function useDiagnostics ( ) {
52+ const [ data , setData ] = useState < Diagnostics | null > ( null )
53+
54+ useEffect ( ( ) => {
55+ const load = async ( ) => {
56+ const diagnostics : Diagnostics = {
57+ runtime : {
58+ platform : IS_VERCEL
59+ ? 'web'
60+ : Capacitor . isNativePlatform ( )
61+ ? 'android'
62+ : HOSTING_ENV
63+ }
64+ }
65+
66+ if ( IS_VERCEL ) {
67+ diagnostics . web = {
68+ environment : process . env . NEXT_PUBLIC_VERCEL_ENV ,
69+ gitSha : process . env . NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA ,
70+ gitMessage : process . env . NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE ,
71+ deploymentId : process . env . NEXT_PUBLIC_VERCEL_DEPLOYMENT_ID ,
72+ }
73+ }
74+
75+ if ( Capacitor . isNativePlatform ( ) ) {
76+ const appInfo = await App . getInfo ( )
77+ const bundle = await LiveUpdate . getCurrentBundle ( ) . catch ( ( ) => { return { bundleId : null } } )
78+ diagnostics . android = {
79+ appVersion : appInfo . version ,
80+ buildNumber : appInfo . build ,
81+ liveUpdate : {
82+ bundleId : bundle . bundleId ,
83+ commitSha : process . env . CAPAWESOME_BUILD_GIT_COMMIT_SHA || 'N/A' ,
84+ commitMessage : process . env . CAPAWESOME_BUILD_GIT_COMMIT_MESSAGE || 'N/A' ,
85+ }
86+ }
87+ }
88+
89+ const backend = await api ( 'health' ) . catch ( ( ) => null )
90+ if ( backend ) {
91+ diagnostics . backend = {
92+ version : backend . version ,
93+ gitSha : backend . git ?. revision ,
94+ gitMessage : backend . git ?. message ,
95+ commitDate : backend . git ?. commitDate
96+ }
97+ }
98+
99+ setData ( diagnostics )
100+ }
101+
102+ load ( )
103+ } , [ ] )
104+
105+ return data
106+ }
107+
108+ function diagnosticsToText ( d : Diagnostics ) : string {
109+ const replacer = ( key : string , value : any ) => {
110+ if ( value === null ) return 'null'
111+ if ( value === undefined ) return 'undefined'
112+ return value
113+ }
114+
115+ return JSON . stringify ( d , replacer , 2 )
116+ . replace ( / [ " { } \[ \] ] / g, '' )
117+ . replace ( / ^ [ \t ] * \n / gm, '' )
118+ . replace ( / , \n / g, '\n' )
119+ . trim ( )
120+ }
121+
12122
13123export const AboutSettings = ( ) => (
14124 < WithPrivateUser >
@@ -21,75 +131,68 @@ const LoadedAboutSettings = (props: {
21131} ) => {
22132 const { } = props
23133
24- return < Col className = { 'custom-link' } >
25- { IS_VERCEL && < WebBuildInfo /> }
26- { Capacitor . isNativePlatform ( ) && < AndroidInfo /> }
27- < BackendInfo />
28- < RuntimeInfo />
134+ const [ copyFeedback , setCopyFeedback ] = useState ( '' )
135+
136+ const diagnostics = useDiagnostics ( )
137+ if ( ! diagnostics ) return null
138+
139+ const handleCopy = async ( ) => {
140+ if ( ! diagnostics ) return
141+ await navigator . clipboard . writeText ( diagnosticsToText ( diagnostics ) )
142+ setCopyFeedback ( 'Copied!' )
143+ }
144+
145+ return < Col className = { '' } >
146+ < WebBuildInfo info = { diagnostics . web } />
147+ < AndroidInfo info = { diagnostics . android } />
148+ < BackendInfo info = { diagnostics . backend } />
149+ < RuntimeInfo info = { diagnostics . runtime } />
150+ < Button
151+ onClick = { handleCopy }
152+ className = "w-fit mt-4"
153+ >
154+ { copyFeedback || 'Copy Info' }
155+ </ Button >
29156 </ Col >
30157}
31158
32- const WebBuildInfo = ( ) => {
33- const env = process . env . NEXT_PUBLIC_VERCEL_ENV
34- const msg = process . env . NEXT_PUBLIC_VERCEL_GIT_COMMIT_MESSAGE
35- const sha = process . env . NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA
36- const deploymentId = process . env . NEXT_PUBLIC_VERCEL_DEPLOYMENT_ID
159+ const WebBuildInfo = ( props : { info ?: WebBuild } ) => {
160+ const { info} = props
161+ if ( ! info ) return
162+ const env = info . environment
163+ const gitMessage = info . gitMessage
164+ const sha = info . gitSha
165+ const deploymentId = info . deploymentId
37166 const url = `${ githubRepo } /commit/${ sha } `
38- return < Col >
167+ return < Col className = { 'custom-link' } >
39168 < h3 > Web build (Vercel)</ h3 >
40169 < p > Commit SHA: < CustomLink href = { url } > { sha } </ CustomLink > </ p >
41- < p > Commit message: { msg } </ p >
170+ < p > Commit message: { gitMessage } </ p >
42171 < p > Vercel deployment ID: { deploymentId } </ p >
43172 < p > Environment: { env } </ p >
44173 </ Col >
45174}
46175
47- const AndroidInfo = ( ) => {
48- const liveUpdateInfo = {
49- commitSha : process . env . CAPAWESOME_BUILD_GIT_COMMIT_SHA || 'N/A' ,
50- commitMessage : process . env . CAPAWESOME_BUILD_GIT_COMMIT_MESSAGE || 'N/A' ,
51- gitRef : process . env . CAPAWESOME_BUILD_GIT_REF || 'N/A' ,
52- } ;
53- console . log ( `Current Commit SHA: ${ liveUpdateInfo . commitSha } ` ) ;
54- const [ liveUpdateBundleId , setLiveUpdateBundleId ] = useState < string | null > ( null )
55- const [ androidAppVersion , setAndroidAppVersion ] = useState < string | null > ( null )
56- const [ androidBuildNumber , setAndroidBuildNumber ] = useState < string | null > ( null )
57- useEffect ( ( ) => {
58- const load = async ( ) => {
59- const liveUpdateBundle = await LiveUpdate . getCurrentBundle ( )
60- console . log ( 'liveUpdateBundle' , liveUpdateBundle )
61- setLiveUpdateBundleId ( liveUpdateBundle . bundleId )
62- alert ( liveUpdateBundle )
63-
64- const info = await App . getInfo ( )
65- setAndroidAppVersion ( info . version )
66- setAndroidBuildNumber ( info . build )
67- }
68- load ( )
69- } , [ ] )
70- return < Col >
176+ const AndroidInfo = ( props : { info ?: Android } ) => {
177+ const { info} = props
178+ if ( ! info ) return
179+ return < Col className = { 'custom-link' } >
71180 < h3 > Android (Capacitor / Capawesome)</ h3 >
72- < p > App version (Android): { androidAppVersion } </ p >
73- < p > Native build number (Android): { androidBuildNumber } </ p >
74- < p > Live update build ID (Capawesome): { liveUpdateBundleId } </ p >
75- < p > Live update commit
76- (Capawesome): { liveUpdateInfo . commitSha } , { liveUpdateInfo . commitMessage } , { liveUpdateInfo . gitRef } </ p >
77- < p > Env: { JSON . stringify ( Object . fromEntries ( Object . entries ( process . env ) . sort ( ) ) , null , 2 ) } </ p >
181+ < p > App version (Android): { info . appVersion } </ p >
182+ < p > Native build number (Android): { info . buildNumber } </ p >
183+ < p > Live update build ID (Capawesome): { info . liveUpdate ?. bundleId } </ p >
184+ < p > Live update commit (Capawesome): { JSON . stringify ( info . liveUpdate ) } </ p >
78185 </ Col >
79186}
80187
81- const BackendInfo = ( ) => {
82- const [ info , setInfo ] = useState < any > ( { } )
83- useEffect ( ( ) => {
84- api ( 'health' ) . then ( setInfo )
85- } , [ ] )
86- console . log ( 'Backend info' , info )
87- const gitInfo = info . git || { }
88- const sha = gitInfo . revision
89- const commitDate = gitInfo . commitDate
90- const commitMessage = gitInfo . message
188+ const BackendInfo = ( props : { info ?: Backend } ) => {
189+ const { info} = props
190+ if ( ! info ) return
191+ const sha = info . gitSha
192+ const commitDate = info . commitDate
193+ const commitMessage = info . gitMessage
91194 const url = `${ githubRepo } /commit/${ sha } `
92- return < Col >
195+ return < Col className = { 'custom-link' } >
93196 < h3 > Backend</ h3 >
94197 < p > API version: { info . version } </ p >
95198 { sha && < p > API commit SHA: < CustomLink href = { url } > { sha } </ CustomLink > </ p > }
@@ -98,9 +201,12 @@ const BackendInfo = () => {
98201 </ Col >
99202}
100203
101- const RuntimeInfo = ( ) => {
102- return < Col >
204+ const RuntimeInfo = ( props : { info ?: Runtime } ) => {
205+ const { info} = props
206+ if ( ! info ) return
207+ return < Col className = { 'custom-link' } >
103208 < h3 > Runtime</ h3 >
104- < p > Platform: { IS_VERCEL ? 'Web' : Capacitor . isNativePlatform ( ) ? 'Android' : HOSTING_ENV } </ p >
209+ < p > Platform: { info . platform } </ p >
210+ < p > Env: { JSON . stringify ( Object . fromEntries ( Object . entries ( process . env ) . sort ( ) ) , null , 2 ) } </ p >
105211 </ Col >
106212}
0 commit comments