-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display system stats and project size
- Loading branch information
1 parent
e2b6876
commit ac18b83
Showing
6 changed files
with
129 additions
and
13 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
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,86 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-assignment */ | ||
/* | ||
We need any, because we don't know the type of the children, | ||
and we need the return those children again which is an "unsafe return" | ||
*/ | ||
|
||
import { createContext, useContext, useEffect, useState } from 'react' | ||
import { useMessageBanner } from './MessageBannerProvider' | ||
|
||
|
||
type Stats = { | ||
n_projects: number | ||
n_versions: number | ||
storage: string | ||
} | ||
|
||
interface StatsState { | ||
stats: Stats | null | ||
loadingFailed: boolean | ||
reload: () => void | ||
} | ||
|
||
const Context = createContext<StatsState>({ | ||
stats: null, | ||
loadingFailed: false, | ||
reload: (): void => { | ||
console.warn('StatsProvider not initialized') | ||
} | ||
}) | ||
|
||
/** | ||
* Provides the stats of the docat instance | ||
* If reloading is required, call the reload function. | ||
*/ | ||
export function StatsDataProvider({ children }: any): JSX.Element { | ||
const { showMessage } = useMessageBanner() | ||
|
||
const loadData = (): void => { | ||
void (async (): Promise<void> => { | ||
try { | ||
const response = await fetch('/api/stats') | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to load stats, status code: ${response.status}` | ||
) | ||
} | ||
|
||
const data: Stats = await response.json() | ||
setState({ | ||
stats: data, | ||
loadingFailed: false, | ||
reload: loadData | ||
}) | ||
} catch (e) { | ||
console.error(e) | ||
|
||
showMessage({ | ||
content: 'Failed to load stats', | ||
type: 'error', | ||
showMs: 6000 | ||
}) | ||
|
||
setState({ | ||
stats: null, | ||
loadingFailed: true, | ||
reload: loadData | ||
}) | ||
} | ||
})() | ||
} | ||
|
||
const [state, setState] = useState<StatsState>({ | ||
stats: null, | ||
loadingFailed: false, | ||
reload: loadData | ||
}) | ||
|
||
useEffect(() => { | ||
loadData() | ||
}, []) | ||
|
||
return <Context.Provider value={state}>{children}</Context.Provider> | ||
} | ||
|
||
export const useStats = (): StatsState => useContext(Context) |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
|
||
.project-overview { | ||
display: flex; | ||
flex-direction: column; | ||
flex-direction: row; | ||
} | ||
|
||
.card { | ||
|