Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@
"license": "Apache-2.0",
"devDependencies": {
"@ai-sdk/azure": "^2.0.53",
"@emotion/css": "^11.13.5",
"@ai-sdk/google": "^2.0.23",
"@ai-sdk/mcp": "^0.0.8",
"@ai-sdk/openai": "^2.0.52",
"@emotion/css": "^11.13.5",
"@eslint/js": "^9.34.0",
"@leafygreen-ui/table": "^15.2.2",
"@leafygreen-ui/tokens": "^4.2.0",
"@leafygreen-ui/typography": "^22.2.3",
"@modelcontextprotocol/inspector": "^0.17.1",
"@mongodb-js/oidc-mock-provider": "^0.12.0",
"@redocly/cli": "^2.0.8",
Expand All @@ -101,8 +103,6 @@
"@types/proper-lockfile": "^4.1.4",
"@types/react": "^18.3.0",
"@types/react-dom": "^19.2.3",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"@types/semver": "^7.7.0",
"@types/yargs-parser": "^21.0.3",
"@typescript-eslint/parser": "^8.44.0",
Expand All @@ -123,6 +123,8 @@
"openapi-typescript": "^7.9.1",
"prettier": "^3.6.2",
"proper-lockfile": "^4.1.2",
"react": "^18.3.0",
"react-dom": "^18.3.0",
"semver": "^7.7.2",
"simple-git": "^3.28.0",
"testcontainers": "^11.7.1",
Expand Down
34 changes: 25 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/ui/build/mount.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// <reference types="vite/client" />
import "../styles/fonts.css";
import React from "react";
import { createRoot } from "react-dom/client";

Expand Down
12 changes: 10 additions & 2 deletions src/ui/components/ListDatabases/ListDatabases.styles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { css } from "@emotion/css";
import { color, InteractionState, Property, spacing, Variant } from "@leafygreen-ui/tokens";

export const tableStyles = css`
background: white;
export const getContainerStyles = (darkMode: boolean): string => css`
background-color: ${color[darkMode ? "dark" : "light"][Property.Background][Variant.Primary][
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth importing lg Theme enum?

InteractionState.Default
]};
padding: ${spacing[200]}px;
`;

export const AmountTextStyles = css`
margin-bottom: ${spacing[400]}px;
`;
80 changes: 50 additions & 30 deletions src/ui/components/ListDatabases/ListDatabases.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { useRenderData } from "../../hooks/index.js";
import React, { type ComponentPropsWithoutRef, type FC, type ReactElement } from "react";
import { useDarkMode, useRenderData } from "../../hooks/index.js";
import {
Cell as LGCell,
HeaderCell as LGHeaderCell,
Expand All @@ -9,12 +9,20 @@ import {
TableBody,
TableHead,
} from "@leafygreen-ui/table";
import { tableStyles } from "./ListDatabases.styles.js";
import { Body } from "@leafygreen-ui/typography";
import type { ListDatabasesOutput } from "../../../tools/mongodb/metadata/listDatabases.js";
import { AmountTextStyles, getContainerStyles } from "./ListDatabases.styles.js";

const HeaderCell = LGHeaderCell as React.FC<React.ComponentPropsWithoutRef<"th">>;
const Cell = LGCell as React.FC<React.ComponentPropsWithoutRef<"td">>;
const Row = LGRow as React.FC<React.ComponentPropsWithoutRef<"tr">>;
const HeaderCell = LGHeaderCell as FC<ComponentPropsWithoutRef<"th">>;
const Cell = LGCell as FC<ComponentPropsWithoutRef<"td">>;
const Row = LGRow as FC<ComponentPropsWithoutRef<"tr">>;
Comment on lines +16 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious why this is necessary?


export type Database = ListDatabasesOutput["databases"][number];

interface ListDatabasesProps {
databases?: Database[];
darkMode?: boolean;
}

function formatBytes(bytes: number): string {
if (bytes === 0) return "0 Bytes";
Expand All @@ -26,37 +34,49 @@ function formatBytes(bytes: number): string {
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + " " + sizes[i];
}

export const ListDatabases = (): React.ReactElement | null => {
const { data, isLoading, error } = useRenderData<ListDatabasesOutput>();
export const ListDatabases = ({
databases: propDatabases,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would this come in as a prop?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, if the client is rendering this directly, right?

darkMode: darkModeProp,
}: ListDatabasesProps): ReactElement | null => {
const darkMode = useDarkMode(darkModeProp);
const { data: hookData, isLoading, error } = useRenderData<ListDatabasesOutput>();
const databases = propDatabases ?? hookData?.databases;

if (isLoading) {
return <div>Loading...</div>;
}
if (!propDatabases) {
if (isLoading) {
return <div>Loading...</div>;
}

if (error) {
return <div>Error: {error}</div>;
if (error) {
return <div>Error: {error}</div>;
}
}

if (!data) {
if (!databases) {
return null;
}

return (
<Table className={tableStyles}>
<TableHead>
<HeaderRow>
<HeaderCell>DB Name</HeaderCell>
<HeaderCell>DB Size</HeaderCell>
</HeaderRow>
</TableHead>
<TableBody>
{data.databases.map((db) => (
<Row key={db.name}>
<Cell>{db.name}</Cell>
<Cell>{formatBytes(db.size)}</Cell>
</Row>
))}
</TableBody>
</Table>
<div className={getContainerStyles(darkMode)}>
<Body className={AmountTextStyles} darkMode={darkMode}>
Your cluster has <strong>{databases.length} databases</strong>:
</Body>
<Table darkMode={darkMode}>
<TableHead>
<HeaderRow>
<HeaderCell>Database</HeaderCell>
<HeaderCell>Size</HeaderCell>
</HeaderRow>
</TableHead>
<TableBody>
{databases.map((db) => (
<Row key={db.name}>
<Cell>{db.name}</Cell>
<Cell>{formatBytes(db.size)}</Cell>
</Row>
))}
</TableBody>
</Table>
</div>
);
};
1 change: 1 addition & 0 deletions src/ui/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { useDarkMode } from "./useDarkMode.js";
export { useRenderData } from "./useRenderData.js";
20 changes: 20 additions & 0 deletions src/ui/hooks/useDarkMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useSyncExternalStore } from "react";

function subscribeToPrefersColorScheme(callback: () => void): () => void {
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
mediaQuery.addEventListener("change", callback);
return () => mediaQuery.removeEventListener("change", callback);
}

function getPrefersDarkMode(): boolean {
return window.matchMedia("(prefers-color-scheme: dark)").matches;
}

function getServerSnapshot(): boolean {
return false;
}

export function useDarkMode(override?: boolean): boolean {
const prefersDarkMode = useSyncExternalStore(subscribeToPrefersColorScheme, getPrefersDarkMode, getServerSnapshot);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect to define getServerSnapshot in the future? Or should this just be empty?

Suggested change
const prefersDarkMode = useSyncExternalStore(subscribeToPrefersColorScheme, getPrefersDarkMode, getServerSnapshot);
const prefersDarkMode = useSyncExternalStore(subscribeToPrefersColorScheme, getPrefersDarkMode);

return override ?? prefersDarkMode;
}
2 changes: 2 additions & 0 deletions src/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { UIRegistry } from "./registry/index.js";
export { ListDatabases } from "./components/ListDatabases/index.js";
export type { Database } from "./components/ListDatabases/ListDatabases.js";
Loading