Skip to content

An open-source, self-hosted system for apps built with web technologies.

Organize reusable View components

zohayrslileh4072664e679b
diff --git a/source/client/view2/components/direction-switch.tsx b/source/client/view2/components/direction-switch.tsx
new file mode 100644
index 0000000..ed3d4b7
--- /dev/null
+++ b/source/client/view2/components/direction-switch.tsx
@@ -0,0 +1,21 @@
+import { Switch } from "@heroui/react"
+import useStorage from "@libs/storage-hook"
+
+export default function () {
+
+ const direction = useStorage("direction")
+
+ const reversed = direction.value === "rtl"
+
+ return <Switch isSelected={reversed} onChange={value => direction.update(value ? "rtl" : "ltr")}>
+
+ <Switch.Content>
+
+ <Switch.Control><Switch.Thumb /></Switch.Control>
+
+ Reverse reading direction
+
+ </Switch.Content>
+
+ </Switch>
+}
diff --git a/source/client/view2/components/failure.tsx b/source/client/view2/components/failure.tsx
new file mode 100644
index 0000000..cf038aa
--- /dev/null
+++ b/source/client/view2/components/failure.tsx
@@ -0,0 +1,23 @@
+import { Button } from "@heroui/react"
+
+export default function ({ title, error, retry }: FailureProps) {
+
+ return <section className="grid place-self-center justify-items-center gap-4 p-6 text-center">
+
+ <h1 className="text-xl font-semibold">{title}</h1>
+
+ <p className="text-muted">{error instanceof Error ? error.message : String(error)}</p>
+
+ <Button onPress={() => void retry()}>Try again</Button>
+
+ </section>
+}
+
+interface FailureProps {
+
+ title: string
+
+ error: unknown
+
+ retry: () => Promise<unknown>
+}
diff --git a/source/client/view2/components/theme-switch.tsx b/source/client/view2/components/theme-switch.tsx
new file mode 100644
index 0000000..ce7c47d
--- /dev/null
+++ b/source/client/view2/components/theme-switch.tsx
@@ -0,0 +1,19 @@
+import { Switch } from "@heroui/react"
+import { useTheme } from "next-themes"
+
+export default function () {
+
+ const { resolvedTheme, setTheme } = useTheme()
+
+ return <Switch isSelected={resolvedTheme === "dark"} onChange={dark => setTheme(dark ? "dark" : "light")}>
+
+ <Switch.Content>
+
+ <Switch.Control><Switch.Thumb /></Switch.Control>
+
+ Dark theme
+
+ </Switch.Content>
+
+ </Switch>
+}
diff --git a/source/client/view2/structure/authentication/authentication.tsx b/source/client/view2/structure/authentication/authentication.tsx
index 24939cc..c150f81 100644
--- a/source/client/view2/structure/authentication/authentication.tsx
+++ b/source/client/view2/structure/authentication/authentication.tsx
@@ -1,8 +1,9 @@
import AuthManager from "@client/core/link-manager/auth-manager/auth-manager"
import { AuthManagerContext, LinkManagerContext } from "../../contexts"
-import { Button, Spinner } from "@heroui/react"
+import { Spinner } from "@heroui/react"
import Credentials from "./credentials"
import Desktop from "../desktop/desktop"
+import Failure from "../../components/failure"
import usePromise from "@libs/react-promise"
import useStorage from "@libs/storage-hook"
import { useEffect, useState } from "react"
@@ -50,7 +51,7 @@ export default function () {
if (session.isPending) return <Spinner aria-label="Restoring your session" className="place-self-center" />
- if (session.exception) return <SessionFailure error={session.exception.current} retry={session.safeExecute} />
+ if (session.exception) return <Failure title="Your session could not be restored" error={session.exception.current} retry={session.safeExecute} />
if (session.solve.kind === "authenticated") return <AuthManagerContext.Provider value={session.solve.manager}>
@@ -69,19 +70,6 @@ export default function () {
/>
}
-function SessionFailure({ error, retry }: { error: unknown, retry: () => Promise<Session | undefined> }) {
-
- return <section className="grid place-self-center justify-items-center gap-4 p-6 text-center">
-
- <h1 className="text-xl font-semibold">Your session could not be restored</h1>
-
- <p className="text-muted">{error instanceof Error ? error.message : String(error)}</p>
-
- <Button onPress={() => void retry()}>Try again</Button>
-
- </section>
-}
-
type Session = {
kind: "anonymous"
diff --git a/source/client/view2/structure/structure.tsx b/source/client/view2/structure/structure.tsx
index a6ec629..3572bd3 100644
--- a/source/client/view2/structure/structure.tsx
+++ b/source/client/view2/structure/structure.tsx
@@ -2,49 +2,24 @@ import ReactTunnel from "@libs/the-link/plugins/react-helper/react-tunnel"
import { type TransmittedLinkManager } from "@server/core/link-manager/link-manager"
import SocketLink from "@libs/the-link/plugins/client-link/socket-link"
import LinkManager from "@client/core/link-manager/link-manager"
-import { Button, Spinner, Switch, SwitchGroup } from "@heroui/react"
+import { Spinner, SwitchGroup } from "@heroui/react"
import { ApplicationContext, LinkManagerContext } from "../contexts"
import Authentication from "./authentication/authentication"
+import DirectionSwitch from "../components/direction-switch"
+import ThemeSwitch from "../components/theme-switch"
+import Failure from "../components/failure"
import usePromise from "@libs/react-promise"
-import useStorage from "@libs/storage-hook"
-import { useTheme } from "next-themes"
import { useCallback, useState } from "react"
export default function () {
- const { resolvedTheme, setTheme } = useTheme()
-
- const direction = useStorage("direction")
-
- const reversed = direction.value === "rtl"
-
return <>
<SwitchGroup orientation="horizontal" className="justify-self-end p-4">
- <Switch isSelected={resolvedTheme === "dark"} onChange={dark => setTheme(dark ? "dark" : "light")}>
-
- <Switch.Content>
-
- <Switch.Control><Switch.Thumb /></Switch.Control>
-
- Dark theme
-
- </Switch.Content>
-
- </Switch>
-
- <Switch isSelected={reversed} onChange={value => direction.update(value ? "rtl" : "ltr")}>
-
- <Switch.Content>
+ <ThemeSwitch />
- <Switch.Control><Switch.Thumb /></Switch.Control>
-
- Reverse reading direction
-
- </Switch.Content>
-
- </Switch>
+ <DirectionSwitch />
</SwitchGroup>
@@ -79,7 +54,7 @@ function Connection() {
}, [])
- if (connection.exception) return <ConnectionFailure error={connection.exception.current} retry={connection.safeExecute} />
+ if (connection.exception) return <Failure title="PhreshOS is unavailable" error={connection.exception.current} retry={connection.safeExecute} />
if (!linkManager) return <Spinner aria-label="Connecting to PhreshOS" className="place-self-center" />
@@ -89,16 +64,3 @@ function Connection() {
</LinkManagerContext.Provider>
}
-
-function ConnectionFailure({ error, retry }: { error: unknown, retry: () => Promise<void | undefined> }) {
-
- return <section className="grid place-self-center justify-items-center gap-4 p-6 text-center">
-
- <h1 className="text-xl font-semibold">PhreshOS is unavailable</h1>
-
- <p className="text-muted">{error instanceof Error ? error.message : String(error)}</p>
-
- <Button onPress={() => void retry()}>Try again</Button>
-
- </section>
-}