@alauda/doom
Version:
Doctor Doom making docs.
70 lines (69 loc) • 4.25 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/**
* modified based on @see https://github.com/crdsdev/doc/blob/main/template/doc.html
*/
import { Badge, Button } from '@rspress/core/theme';
import crdsMap from 'doom-@api-crdsMap';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useIsPrint, useTranslation } from '../hooks/index.js';
import { Directive } from './Directive.js';
import { Markdown } from './Markdown.js';
import { X } from './_X.js';
export const K8sCrdSchemaPart = ({ name, parent, schema: property, expandAll, }) => {
const [open, setOpen] = useState(expandAll);
const onToggle = useCallback((open) => {
setOpen(open);
}, []);
useEffect(() => {
// eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect
setOpen(expandAll);
}, [expandAll]);
const [props, required, type, schema] = useMemo(() => {
let schema = property;
let props = property.properties;
let type = property.type;
if (property.type === 'array') {
const itemsSchema = property.items;
if (itemsSchema.type === 'object') {
schema = itemsSchema;
props = itemsSchema.properties;
type = `[]object`;
}
else if (itemsSchema.type) {
type = `[]${itemsSchema.type}`;
}
}
let required = false;
if (parent.required?.includes(name)) {
required = true;
}
return [props, required, type, schema];
}, [name, parent, property]);
return (_jsx(_Fragment, { children: _jsxs(Directive, { type: "details", title: _jsxs(_Fragment, { children: [name, type && (_jsxs(_Fragment, { children: [' ', _jsx("code", { children: type })] })), required && (_jsxs(_Fragment, { children: [' ', _jsx(Badge, { children: "required" })] }))] }), open: open, onToggle: onToggle, children: [_jsx(Markdown, { children: schema.description }), props &&
Object.entries(props).map(([name, subSchema]) => (_jsx(K8sCrdSchemaPart, { name: name, parent: schema, schema: subSchema, expandAll: expandAll }, name)))] }) }));
};
const SKIPPED_PROPERTIES = ['apiVersion', 'kind', 'metadata'];
export const K8sCrdSchema = ({ schema, version, }) => {
const { description, properties } = schema;
const isPrint = useIsPrint();
const t = useTranslation();
const [expandAll, setExpandAll] = useState(isPrint);
const toggleExpandAll = useCallback(() => {
setExpandAll((all) => !all);
}, []);
return (_jsxs(_Fragment, { children: [_jsx(Markdown, { children: description }), _jsxs("div", { className: "rp-flex rp-items-center", children: [_jsxs("span", { children: [_jsx("code", { children: version }), " ", _jsx(Badge, { children: "version" })] }), !isPrint && properties != null && (_jsx(Button, { className: "rp-ml-auto doom-btn", type: "button", size: "medium", theme: "alt", children: _jsxs("span", { onClick: toggleExpandAll, children: [expandAll ? '-' : '+', ' ', t(expandAll ? 'collapse_all' : 'expand_all')] }) }))] }), properties == null ? (_jsx(Directive, { type: "warning", children: t('crd_no_schema') })) : (_jsx("div", { className: "doom-collapse-group", children: Object.entries(properties).map(([name, subSchema]) => SKIPPED_PROPERTIES.includes(name) || (_jsx(K8sCrdSchemaPart, { name: name, parent: schema, schema: subSchema, expandAll: expandAll }, name))) }))] }));
};
export const K8sCrd = ({ name, crdPath }) => {
const [, crd] = useMemo(() => Object.entries(crdsMap).find(([pathname, crd]) => {
if (crdPath && pathname !== crdPath) {
return false;
}
return crd.metadata.name === name;
}) || [], [crdPath, name]);
if (!crd) {
console.error(`No CustomResourceDefinition found for ${name}`);
return null;
}
return (_jsxs(_Fragment, { children: [_jsxs(X.p, { children: [_jsx("code", { children: crd.spec.group }), " ", _jsx(Badge, { children: "group" })] }), crd.spec.versions.map((version) => (_jsx(K8sCrdSchema, { schema: version.schema.openAPIV3Schema, version: version.name }, version.name)))] }));
};
export default K8sCrd;