Path: blob/main/web/ui/src/pages/ComponentDetailPage.tsx
5370 views
import { FC, useEffect, useState } from 'react';1import { useParams } from 'react-router-dom';23import { ComponentView } from '../features/component/ComponentView';4import { ComponentDetail, componentInfoByID } from '../features/component/types';5import { useComponentInfo } from '../hooks/componentInfo';67const ComponentDetailPage: FC = () => {8const { '*': id } = useParams();910const components = useComponentInfo(id);11const infoByID = componentInfoByID(components);1213const [component, setComponent] = useState<ComponentDetail | undefined>(undefined);1415useEffect(16function () {17if (id === undefined) {18return;19}2021const fragments = id.split('/');2223const infoRoot =24fragments.length === 125? './api/v0/web/components/'26: `./api/v0/component/${fragments.slice(0, fragments.length - 1).join('/')}/components/`;2728const worker = async () => {29// Request is relative to the <base> tag inside of <head>.30const resp = await fetch(infoRoot + fragments[fragments.length - 1], {31cache: 'no-cache',32credentials: 'same-origin',33});34const data: ComponentDetail = await resp.json();3536// Set parent.37if (fragments.length > 1) {38data.parent = fragments.slice(0, fragments.length - 1).join('/');39}4041// Get data from the component module API.42if (data.id.startsWith('module.')) {43const resp = await fetch(`./api/v0/component/${id}/components`);44data.moduleInfo = await resp.json();45}4647setComponent(data);48};4950worker().catch(console.error);51},52[id]53);5455return component ? <ComponentView component={component} info={infoByID} /> : <div></div>;56};5758export default ComponentDetailPage;596061