Skip to content

Open-source status page, incident response and uptime monitoring

feat: @openstatus/react (#366)

Maximilian Kaske2c4562a88c40
diff --git a/apps/docs/pages/_meta.json b/apps/docs/pages/_meta.json
index de3461a4e..6e52ae7a7 100644
--- a/apps/docs/pages/_meta.json
+++ b/apps/docs/pages/_meta.json
@@ -4,6 +4,7 @@
"integrations": "Integrations",
"developer-guide": "Developer guide",
"api-server": "API",
+ "packages": "Packages",
"contact": {
"title": "🌐 Website",
"type": "page",
diff --git a/apps/docs/pages/api-server/status-widget.mdx b/apps/docs/pages/api-server/status-widget.mdx
index d4b0d84ef..8d2fd11e5 100644
--- a/apps/docs/pages/api-server/status-widget.mdx
+++ b/apps/docs/pages/api-server/status-widget.mdx
@@ -1,3 +1,5 @@
+import { Callout } from "nextra/components";
+
# Public Status Widget
We have added a public endpoint where you can access the status of your status
@@ -8,6 +10,12 @@ page.
curl https://api.openstatus.dev/public/status/:slug
```
+<Callout type="info">
+ We have released an `@openstatus/react` npm package that allows you to easily
+ integrate the pre-typed status into your React projects or use the default
+ widget. Read more [here](/packages/react).
+</Callout>
+
The response is a JSON object with the following structure:
```json
diff --git a/apps/docs/pages/packages/_meta.json b/apps/docs/pages/packages/_meta.json
new file mode 100644
index 000000000..8dc2c9692
--- /dev/null
+++ b/apps/docs/pages/packages/_meta.json
@@ -0,0 +1,3 @@
+{
+ "react": "@openstatus/react"
+}
\ No newline at end of file
diff --git a/apps/docs/pages/packages/react.mdx b/apps/docs/pages/packages/react.mdx
new file mode 100644
index 000000000..c9e166d60
--- /dev/null
+++ b/apps/docs/pages/packages/react.mdx
@@ -0,0 +1,64 @@
+## React Server Component
+
+```tsx
+import { StatusWidget } from "@openstatus/react";
+
+export function Page() {
+ return <StatusWidget slug="status" />;
+}
+```
+
+It will automatically attach the slug to the href to allow the user to open a
+new tab on click to `https://slug.openstatus.dev`. If you want to redirect him
+to a specific page, use the `href` property, like so:
+
+```tsx
+<StatusWidget slug="documenso" href="https://status.documenso.com" />
+```
+
+> `StatusWidget` is an **async function** and will only work with RSC. Using it
+> within a dead simple React App will not work.
+
+### Styling
+
+#### With tailwindcss
+
+```ts
+// tailwind.config.js
+module.exports = {
+ content: [
+ "./app/**/*.{tsx,ts,mdx,md}",
+ "./node_modules/@openstatus/react/**/*.{js,ts,jsx,tsx}",
+ ],
+ theme: {
+ extend: {},
+ },
+ plugins: [],
+};
+```
+
+#### Without tailwindcss
+
+```tsx
+// app/layout.tsx
+import "@openstatus/react/dist/styles.css";
+```
+
+## Typed fetch function
+
+```tsx
+import { getStatus } from "@openstatus/react";
+
+// React Server Component
+async function CustomStatusWidget() {
+ const res = await getStatus("slug");
+ // ^StatusResponse = { status: Status }
+
+ const { status } = res;
+ // ^Status = "unknown" | "operational" | "degraded_performance" | "partial_outage" | "major_outage" | "under_maintenance"
+
+ return <div>{/* customize */}</div>;
+}
+```
+
+[Link to npm](https://www.npmjs.com/package/@openstatus/react)
diff --git a/apps/web/package.json b/apps/web/package.json
index 4a7a01c2d..fbd01ba4d 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -19,6 +19,7 @@
"@openstatus/emails": "workspace:*",
"@openstatus/notification-emails": "workspace:*",
"@openstatus/plans": "workspace:*",
+ "@openstatus/react": "workspace:*",
"@openstatus/tinybird": "workspace:*",
"@openstatus/ui": "workspace:*",
"@openstatus/upstash": "workspace:*",
diff --git a/apps/web/src/components/layout/marketing-footer.tsx b/apps/web/src/components/layout/marketing-footer.tsx
index ebe4d29d6..d0bd6040a 100644
--- a/apps/web/src/components/layout/marketing-footer.tsx
+++ b/apps/web/src/components/layout/marketing-footer.tsx
@@ -1,7 +1,8 @@
import Link from "next/link";
import { ArrowUpRight } from "lucide-react";
-import { StatusWidget } from "@/components/status-widget";
+import { StatusWidget } from "@openstatus/react";
+
import { cn } from "@/lib/utils";
import { Shell } from "../dashboard/shell";
diff --git a/apps/web/src/components/status-widget.tsx b/apps/web/src/components/status-widget.tsx
deleted file mode 100644
index 6d5d0a03f..000000000
--- a/apps/web/src/components/status-widget.tsx
+++ /dev/null
@@ -1,75 +0,0 @@
-import * as z from "zod";
-
-const statusEnum = z.enum([
- "operational",
- "degraded_performance",
- "partial_outage",
- "major_outage",
- "under_maintenance",
- "unknown",
-]);
-
-const statusSchema = z.object({ status: statusEnum });
-
-const dictionary = {
- operational: {
- label: "Operational",
- color: "bg-green-500",
- },
- degraded_performance: {
- label: "Degraded Performance",
- color: "bg-yellow-500",
- },
- partial_outage: {
- label: "Partial Outage",
- color: "bg-yellow-500",
- },
- major_outage: {
- label: "Major Outage",
- color: "bg-red-500",
- },
- unknown: {
- label: "Unknown",
- color: "bg-gray-500",
- },
- under_maintenance: {
- label: "Under Maintenance",
- color: "bg-gray-500",
- },
-} as const;
-
-export async function StatusWidget({ slug }: { slug: string }) {
- const res = await fetch(`https://api.openstatus.dev/public/status/${slug}`, {
- next: { revalidate: 60 }, // cache request for 60 seconds
- });
- const data = await res.json();
- const parsed = statusSchema.safeParse(data);
-
- if (!parsed.success) {
- return null;
- }
-
- const key = parsed.data.status;
- const { label, color } = dictionary[key];
-
- return (
- <a
- className="border-border text-foreground/70 hover:bg-muted hover:text-foreground inline-flex max-w-fit items-center gap-2 rounded-md border px-3 py-1 text-sm"
- href={`https://${slug}.openstatus.dev`}
- target="_blank"
- rel="noreferrer"
- >
- {label}
- <span className="relative flex h-2 w-2">
- {parsed.data.status === "operational" ? (
- <span
- className={`absolute inline-flex h-full w-full animate-ping rounded-full ${color} opacity-75 duration-1000`}
- />
- ) : null}
- <span
- className={`relative inline-flex h-2 w-2 rounded-full ${color}`}
- />
- </span>
- </a>
- );
-}
diff --git a/apps/web/tailwind.config.ts b/apps/web/tailwind.config.ts
index 505e0c9f9..bea28c54b 100644
--- a/apps/web/tailwind.config.ts
+++ b/apps/web/tailwind.config.ts
@@ -14,6 +14,7 @@ module.exports = {
// our vercel integration
"../../packages/integrations/**/*.{ts,tsx}",
"../../packages/ui/**/*.{ts,tsx}",
+ "./node_modules/@openstatus/react/**/*.{js,ts,jsx,tsx}",
],
theme: {
/* Tremor */
diff --git a/packages/react/README.md b/packages/react/README.md
new file mode 100644
index 000000000..606bb17d6
--- /dev/null
+++ b/packages/react/README.md
@@ -0,0 +1,24 @@
+## Status Widget
+
+Create an account on [openstatus.dev](https://openstatus.dev) and integrate a
+simple wiget into your React Application.
+
+### Install
+
+```bash
+npm install @openstatus/react
+```
+
+```bash
+pnpm add @openstatus/react
+```
+
+```bash
+yarn add @openstatus/react
+```
+
+```bash
+bun add @openstatus/react
+```
+
+Learn more [here](https://docs.openstatus.dev/packages/react).
diff --git a/packages/react/package.json b/packages/react/package.json
new file mode 100644
index 000000000..5165c7815
--- /dev/null
+++ b/packages/react/package.json
@@ -0,0 +1,26 @@
+{
+ "name": "@openstatus/react",
+ "version": "0.0.1",
+ "main": "./dist/index.js",
+ "types": "./dist/index.d.ts",
+ "module": "./dist/index.mjs",
+ "files": [
+ "./dist/**"
+ ],
+ "license": "MIT",
+ "scripts": {
+ "build:tailwind": "tailwindcss -i ./src/styles.css -o ./dist/styles.css --minify",
+ "build": "tsup && pnpm run build:tailwind"
+ },
+ "devDependencies": {
+ "@openstatus/tsconfig": "workspace:*",
+ "@types/node": "20.8.0",
+ "@types/react": "18.2.24",
+ "tailwindcss": "3.3.2",
+ "tsup": "7.2.0",
+ "typescript": "5.2.2"
+ },
+ "peerDependencies": {
+ "react": "^18.0.0"
+ }
+}
diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts
new file mode 100644
index 000000000..97866ce19
--- /dev/null
+++ b/packages/react/src/index.ts
@@ -0,0 +1 @@
+export * from "./widget";
diff --git a/packages/react/src/styles.css b/packages/react/src/styles.css
new file mode 100644
index 000000000..b5c61c956
--- /dev/null
+++ b/packages/react/src/styles.css
@@ -0,0 +1,3 @@
+@tailwind base;
+@tailwind components;
+@tailwind utilities;
diff --git a/packages/react/src/utils.ts b/packages/react/src/utils.ts
new file mode 100644
index 000000000..a25ce3ef0
--- /dev/null
+++ b/packages/react/src/utils.ts
@@ -0,0 +1,26 @@
+export const statusDictionary = {
+ operational: {
+ label: "Operational",
+ color: "bg-green-500",
+ },
+ degraded_performance: {
+ label: "Degraded Performance",
+ color: "bg-yellow-500",
+ },
+ partial_outage: {
+ label: "Partial Outage",
+ color: "bg-yellow-500",
+ },
+ major_outage: {
+ label: "Major Outage",
+ color: "bg-red-500",
+ },
+ unknown: {
+ label: "Unknown",
+ color: "bg-gray-500",
+ },
+ under_maintenance: {
+ label: "Under Maintenance",
+ color: "bg-gray-500",
+ },
+} as const;
diff --git a/packages/react/src/widget.tsx b/packages/react/src/widget.tsx
new file mode 100644
index 000000000..f04917203
--- /dev/null
+++ b/packages/react/src/widget.tsx
@@ -0,0 +1,57 @@
+import { statusDictionary } from "./utils";
+
+export type Status =
+ | "operational"
+ | "degraded_performance"
+ | "partial_outage"
+ | "major_outage"
+ | "under_maintenance"
+ | "unknown";
+
+export type StatusResponse = { status: Status };
+
+export async function getStatus(slug: string): Promise<StatusResponse> {
+ const res = await fetch(`https://api.openstatus.dev/public/status/${slug}`, {
+ cache: "no-cache",
+ });
+
+ if (res.ok) {
+ const data = (await res.json()) as StatusResponse;
+ return data;
+ }
+
+ return { status: "unknown" };
+}
+
+type StatusWidgetProps = {
+ slug: string;
+ href?: string;
+};
+
+export async function StatusWidget({ slug, href }: StatusWidgetProps) {
+ const data = await getStatus(slug);
+
+ const key = data.status;
+ const { label, color } = statusDictionary[key];
+
+ return (
+ <a
+ className="inline-flex max-w-fit items-center gap-2 rounded-md border border-gray-200 px-3 py-1 text-sm text-gray-700 hover:bg-gray-100 hover:text-black"
+ href={href || `https://${slug}.openstatus.dev`}
+ target="_blank"
+ rel="noreferrer"
+ >
+ {label}
+ <span className="relative flex h-2 w-2">
+ {data.status === "operational" ? (
+ <span
+ className={`absolute inline-flex h-full w-full animate-ping rounded-full ${color} opacity-75 duration-1000`}
+ />
+ ) : null}
+ <span
+ className={`relative inline-flex h-2 w-2 rounded-full ${color}`}
+ />
+ </span>
+ </a>
+ );
+}
diff --git a/packages/react/tailwind.config.js b/packages/react/tailwind.config.js
new file mode 100644
index 000000000..de550ddb4
--- /dev/null
+++ b/packages/react/tailwind.config.js
@@ -0,0 +1,8 @@
+/** @type {import('tailwindcss').Config} */
+module.exports = {
+ content: ["./src/**/*.{ts,tsx}"],
+ theme: {
+ extend: {},
+ },
+ plugins: [],
+};
diff --git a/packages/react/tsconfig.json b/packages/react/tsconfig.json
new file mode 100644
index 000000000..a91dc0e59
--- /dev/null
+++ b/packages/react/tsconfig.json
@@ -0,0 +1,14 @@
+{
+ "extends": "@openstatus/tsconfig/react-library.json",
+ "exclude": ["node_modules"],
+ "compilerOptions": {
+ "esModuleInterop": true,
+ "forceConsistentCasingInFileNames": true,
+ "isolatedModules": true,
+ "moduleResolution": "node",
+ "preserveWatchOutput": true,
+ "skipLibCheck": true,
+ "noEmit": true,
+ "strict": true
+ }
+}
diff --git a/packages/react/tsup.config.js b/packages/react/tsup.config.js
new file mode 100644
index 000000000..cf7c3fea9
--- /dev/null
+++ b/packages/react/tsup.config.js
@@ -0,0 +1,11 @@
+import { defineConfig } from "tsup";
+
+export default defineConfig({
+ entry: ["src/index.ts"],
+ format: ["cjs", "esm"],
+ splitting: false,
+ sourcemap: true,
+ clean: true,
+ bundle: true,
+ dts: true,
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6fae5de85..ec0f53309 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -136,6 +136,9 @@ importers:
'@openstatus/plans':
specifier: workspace:*
version: link:../../packages/plans
+ '@openstatus/react':
+ specifier: workspace:*
+ version: link:../../packages/react
'@openstatus/tinybird':
specifier: workspace:*
version: link:../../packages/tinybird
@@ -623,6 +626,31 @@ importers:
specifier: 5.2.2
version: 5.2.2
+ packages/react:
+ dependencies:
+ react:
+ specifier: ^18.0.0
+ version: 18.2.0
+ devDependencies:
+ '@openstatus/tsconfig':
+ specifier: workspace:*
+ version: link:../tsconfig
+ '@types/node':
+ specifier: 20.8.0
+ version: 20.8.0
+ '@types/react':
+ specifier: 18.2.24
+ version: 18.2.24
+ tailwindcss:
+ specifier: 3.3.2
+ version: 3.3.2
+ tsup:
+ specifier: 7.2.0
+ typescript:
+ specifier: 5.2.2
+ version: 5.2.2
+
packages/tinybird:
dependencies:
'@chronark/zod-bird':
@@ -785,7 +813,7 @@ importers:
version: 20.8.0
tsup:
specifier: 7.2.0
- version: 7.2.0([email protected])
typescript:
specifier: 5.2.2
version: 5.2.2
@@ -14462,7 +14490,7 @@ packages:
resolution: {integrity: sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==}
resolution: {integrity: sha512-vDHlczXbgUvY3rWvqFEbSqmC1L7woozbzngMqTtL2PGBODTtWlRwGDDawhvWzr5c1QjKe4OAKqJGfE1xeXUvtQ==}
engines: {node: '>=16.14'}
hasBin: true
@@ -14486,6 +14514,7 @@ packages:
execa: 5.1.1
globby: 11.1.0
joycon: 3.1.1
+ postcss: 8.4.31
postcss-load-config: 4.0.1([email protected])
resolve-from: 5.0.0
rollup: 3.29.4