1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 14:54:22 +00:00

Add basic support for the instance details

This commit is contained in:
Steffo 2021-09-20 18:18:45 +02:00
parent 126451f900
commit 6d172a5a44
4 changed files with 95 additions and 16 deletions

View file

@ -43,9 +43,14 @@ export class ErrorCatcherBox extends React.Component<ErrorCatcherBoxProps, Error
} }
render() { render() {
return ( if(this.state.error) {
<ErrorBox error={this.state.error}/> return (
) <ErrorBox error={this.state.error}/>
)
}
else {
return this.props.children
}
} }
} }

View file

@ -0,0 +1,77 @@
import * as React from "react"
import {Heading} from "@steffo/bluelib-react"
import {useInstance, useInstanceAxios} from "./InstanceContext";
import {InstanceDetails} from "../types";
import {Link} from "./Link";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";
import {faSpinner, faTimesCircle, faUniversity} from "@fortawesome/free-solid-svg-icons";
import {Loading} from "./Loading";
export function InstanceNameHeading(): JSX.Element {
const instance = useInstance()
const api = useInstanceAxios()
const [details, setDetails] = React.useState<InstanceDetails | null>(null)
const [error, setError] = React.useState<Error | null>(null)
React.useEffect(
() => {
if(instance.validity === true) {
const controller = new AbortController()
setError(null)
api.get("/api/core/instance", {signal: controller.signal})
.then(r => setDetails(r.data))
.catch(e => {
if(!controller.signal.aborted) {
setError(e)
}
})
return () => {
controller.abort()
}
}
},
[api, setDetails, setError]
)
if(!instance.validity) {
return (
<Heading level={1} bluelibClassNames={"color-red"}>
<Link href={"/"}>
Sophon
</Link>
</Heading>
)
}
if(error) {
return (
<Heading level={1} bluelibClassNames={"color-red"}>
<Link href={"/"}>
<FontAwesomeIcon icon={faTimesCircle}/>&nbsp;Error
</Link>
</Heading>
)
}
else if(details === null) {
return (
<Heading level={1} bluelibClassNames={"color-cyan"}>
<Link href={"/"}>
<Loading/>
</Link>
</Heading>
)
}
else {
return (
<Heading level={1}>
<Link href={"/"}>
<FontAwesomeIcon icon={faUniversity}/>&nbsp;{details.name}
</Link>
</Heading>
)
}
}

View file

@ -1,28 +1,18 @@
import * as React from "react" import * as React from "react"
import * as Reach from "@reach/router" import * as Reach from "@reach/router"
import { LoginPage } from "./LoginPage" import { LoginPage } from "./LoginPage"
import { Heading } from "@steffo/bluelib-react"
import { SelectResearchGroupPage } from "./SelectResearchGroupPage" import { SelectResearchGroupPage } from "./SelectResearchGroupPage"
import {Link} from "../components/Link"
import { ErrorCatcherBox, NotFoundBox } from "../components/ErrorBox" import { ErrorCatcherBox, NotFoundBox } from "../components/ErrorBox"
import { InstanceNameHeading } from "../components/InstanceNameHeading"
function Header() {
return (
<Heading level={1}>
<Link href={"/"}>Sophon</Link>
</Heading>
)
}
export function Router() { export function Router() {
return <> return <>
<Reach.Router primary={false}> <Reach.Router primary={false}>
<Header default/> <InstanceNameHeading default/>
</Reach.Router> </Reach.Router>
<ErrorCatcherBox> <ErrorCatcherBox>
<Reach.Router> <Reach.Router primary={true}>
<LoginPage path={"/"}/> <LoginPage path={"/"}/>
<SelectResearchGroupPage path={"/g/"}/> <SelectResearchGroupPage path={"/g/"}/>
<NotFoundBox default/> <NotFoundBox default/>

View file

@ -34,6 +34,13 @@ export interface User {
} }
export interface InstanceDetails {
name: string,
description?: string,
theme: "sophon" | "paper" | "royalblue" | "hacker",
}
export type ResearchProjectSlug = string export type ResearchProjectSlug = string
export interface ResearchProject { export interface ResearchProject {