|
1 | 1 | import React from 'react'; |
2 | 2 | import SwaggerUI from 'swagger-ui-react'; |
3 | 3 | import 'swagger-ui-react/swagger-ui.css'; |
4 | | -// eslint-disable-next-line import/no-webpack-loader-syntax |
5 | | -import localSpec from '!!raw-loader!@signer-api-spec'; |
6 | 4 |
|
7 | | -const IS_LOCALHOST = typeof window !== 'undefined' && window.location.hostname === 'localhost'; |
| 5 | +function getSpecUrl() { |
| 6 | + if (typeof window === 'undefined') return null; |
8 | 7 |
|
9 | | -const PROD_URL = 'https://raw.githubusercontent.com/Commit-Boost/commit-boost-client/main/api/signer-api.yml'; |
| 8 | + // localhost dev: webpack bundles the spec via raw-loader |
| 9 | + if (window.location.hostname === 'localhost') return null; |
| 10 | + |
| 11 | + // Production GitHub Pages: derive org/repo/branch from the current URL |
| 12 | + // e.g. https://commit-boost.github.io/commit-boost-client/ → Commit-Boost/commit-boost-client/main |
| 13 | + const host = window.location.hostname; // e.g. commit-boost.github.io |
| 14 | + const org = host.replace('.github.io', ''); // e.g. commit-boost → Commit-Boost |
| 15 | + const pathParts = window.location.pathname.split('/').filter(Boolean); |
| 16 | + const repo = pathParts[0] || 'commit-boost-client'; |
| 17 | + const branch = window.location.searchParams?.get('branch') || 'main'; |
| 18 | + |
| 19 | + return `https://raw.githubusercontent.com/${org}/${repo}/${branch}/api/signer-api.yml`; |
| 20 | +} |
10 | 21 |
|
11 | 22 | const SwaggerUIComponent = () => { |
12 | | - if (IS_LOCALHOST) { |
| 23 | + const specUrl = getSpecUrl(); |
| 24 | + if (!specUrl) { |
| 25 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 26 | + const localSpec = require('!!raw-loader!@signer-api-spec').default; |
13 | 27 | return <SwaggerUI spec={localSpec} />; |
14 | 28 | } |
15 | | - return <SwaggerUI url={PROD_URL} />; |
| 29 | + return <SwaggerUI url={specUrl} />; |
16 | 30 | }; |
17 | 31 |
|
18 | 32 | export default SwaggerUIComponent; |
0 commit comments