import * as React from 'react'; import { createRoot } from 'react-dom/client'; import styled from 'styled-components'; import { RedocStandalone } from '../src'; import ComboBox from './ComboBox'; import FileInput from './components/FileInput'; const DEFAULT_SPEC = 'museum.yaml'; const NEW_VERSION_PETSTORE = 'openapi-3-1.yaml'; const demos = [ { value: DEFAULT_SPEC, label: 'Museum API' }, { value: NEW_VERSION_PETSTORE, label: 'Petstore OpenAPI 3.1' }, { value: 'https://api.apis.guru/v2/specs/instagram.com/1.0.0/swagger.yaml', label: 'Instagram' }, { value: 'https://api.apis.guru/v2/specs/googleapis.com/calendar/v3/openapi.yaml', label: 'Google Calendar', }, { value: 'https://api.apis.guru/v2/specs/slack.com/1.7.0/openapi.yaml', label: 'Slack' }, { value: 'https://api.apis.guru/v2/specs/zoom.us/2.0.0/openapi.yaml', label: 'Zoom.us' }, ]; class DemoApp extends React.Component< Record, { spec: object | undefined; specUrl: string; dropdownOpen: boolean; cors: boolean } > { constructor(props) { super(props); let parts = window.location.search.match(/url=([^&]+)/); let url = DEFAULT_SPEC; if (parts && parts.length > 1) { url = decodeURIComponent(parts[1]); } parts = window.location.search.match(/[?&]nocors(&|#|$)/); let cors = true; if (parts && parts.length > 1) { cors = false; } this.state = { spec: undefined, specUrl: url, dropdownOpen: false, cors, }; } handleUploadFile = (spec: object) => { this.setState({ spec, specUrl: '', }); }; handleChange = (url: string) => { if (url === NEW_VERSION_PETSTORE) { this.setState({ cors: false }); 0; } this.setState({ specUrl: url, }); window.history.pushState( undefined, '', updateQueryStringParameter(location.search, 'url', url), ); }; toggleCors = (e: React.ChangeEvent) => { const cors = e.currentTarget.checked; this.setState({ cors, }); window.history.pushState( undefined, '', updateQueryStringParameter(location.search, 'nocors', cors ? undefined : ''), ); }; render() { const { specUrl, cors } = this.state; let proxiedUrl = specUrl; if (specUrl !== DEFAULT_SPEC) { proxiedUrl = cors ? 'https://cors.redoc.ly/' + new URL(specUrl, window.location.href).href : specUrl; } return ( <>