Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(catalog): Allow consumers to update Camel Catalog URL #366

Merged
merged 1 commit into from
Nov 20, 2023
Merged
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
5 changes: 3 additions & 2 deletions packages/ui-tests/stories/ContextToolbar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
CatalogLoaderProvider,
CatalogSchemaLoader,
CatalogTilesProvider,
ContextToolbar,
EntitiesContext,
Expand All @@ -14,8 +15,8 @@ import camelRouteMock from '../cypress/fixtures/camelRouteMock.json';
const EntitiesContextDecorator = (Story: StoryFn) => (
<SourceCodeContext.Provider value={{ sourceCode: '', setCodeAndNotify: () => {} }}>
<EntitiesContext.Provider value={camelRouteMock}>
<SchemasLoaderProvider>
<CatalogLoaderProvider>
<SchemasLoaderProvider catalogUrl={CatalogSchemaLoader.DEFAULT_CATALOG_PATH}>
<CatalogLoaderProvider catalogUrl={CatalogSchemaLoader.DEFAULT_CATALOG_PATH}>
<CatalogTilesProvider>
<VisibleFlowsProvider>
<Story />
Expand Down
5 changes: 3 additions & 2 deletions packages/ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import { CatalogLoaderProvider } from './providers/catalog.provider';
import { EntitiesProvider } from './providers/entities.provider';
import { SchemasLoaderProvider } from './providers/schemas.provider';
import { SourceCodeProvider } from './providers/source-code.provider';
import { CatalogSchemaLoader } from './utils/catalog-schema-loader';

function App() {
return (
<SourceCodeProvider>
<EntitiesProvider>
<Shell>
<SchemasLoaderProvider>
<CatalogLoaderProvider>
<SchemasLoaderProvider catalogUrl={CatalogSchemaLoader.DEFAULT_CATALOG_PATH}>
<CatalogLoaderProvider catalogUrl={CatalogSchemaLoader.DEFAULT_CATALOG_PATH}>
<CatalogTilesProvider>
<Outlet />
</CatalogTilesProvider>
Expand Down
14 changes: 7 additions & 7 deletions packages/ui/src/providers/catalog.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@ export const CatalogContext = createContext<typeof CamelCatalogService>(CamelCat
/**
* Loader for the components catalog.
*/
export const CatalogLoaderProvider: FunctionComponent<PropsWithChildren> = (props) => {
export const CatalogLoaderProvider: FunctionComponent<PropsWithChildren<{ catalogUrl: string }>> = (props) => {
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
fetch(`.${CatalogSchemaLoader.DEFAULT_CATALOG_PATH}/index.json`)
fetch(`${props.catalogUrl}/index.json`)
.then((response) => response.json())
.then(async (catalogIndex: CamelCatalogIndex) => {
const camelComponentsFiles = CatalogSchemaLoader.fetchFile<ComponentsCatalog[CatalogKind.Component]>(
catalogIndex.catalogs.components.file,
`${props.catalogUrl}/${catalogIndex.catalogs.components.file}`,
);
const camelProcessorsFiles = CatalogSchemaLoader.fetchFile<ComponentsCatalog[CatalogKind.Processor]>(
catalogIndex.catalogs.models.file,
`${props.catalogUrl}/${catalogIndex.catalogs.models.file}`,
);
const camelLanguagesFiles = CatalogSchemaLoader.fetchFile<ComponentsCatalog[CatalogKind.Language]>(
catalogIndex.catalogs.languages.file,
`${props.catalogUrl}/${catalogIndex.catalogs.languages.file}`,
);
const camelDataformatsFiles = CatalogSchemaLoader.fetchFile<ComponentsCatalog[CatalogKind.Dataformat]>(
catalogIndex.catalogs.dataformats.file,
`${props.catalogUrl}/${catalogIndex.catalogs.dataformats.file}`,
);
const kameletsFiles = CatalogSchemaLoader.fetchFile<ComponentsCatalog[CatalogKind.Kamelet]>(
catalogIndex.catalogs.kamelets.file,
`${props.catalogUrl}/${catalogIndex.catalogs.kamelets.file}`,
);

const [camelComponents, camelProcessors, camelLanguages, camelDataformats, kamelets] = await Promise.all([
Expand Down
6 changes: 3 additions & 3 deletions packages/ui/src/providers/schemas.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ export const SchemasContext = createContext<Record<string, Schema>>({});
/**
* Loader for the components schemas.
*/
export const SchemasLoaderProvider: FunctionComponent<PropsWithChildren> = (props) => {
export const SchemasLoaderProvider: FunctionComponent<PropsWithChildren<{ catalogUrl: string }>> = (props) => {
const setSchema = useSchemasStore((state) => state.setSchema);
const [isLoading, setIsLoading] = useState(true);
const [schemas, setSchemas] = useState<Record<string, Schema>>({});

useEffect(() => {
fetch(`.${CatalogSchemaLoader.DEFAULT_CATALOG_PATH}/index.json`)
fetch(`${props.catalogUrl}/index.json`)
.then((response) => response.json())
.then(async (catalogIndex: CamelCatalogIndex) => {
const schemaFilesPromise = CatalogSchemaLoader.getSchemasFiles(catalogIndex.schemas);
const schemaFilesPromise = CatalogSchemaLoader.getSchemasFiles(props.catalogUrl, catalogIndex.schemas);

const loadedSchemas = await Promise.all(schemaFilesPromise);
const combinedSchemas = loadedSchemas.reduce(
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/utils/catalog-schema-loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe('CatalogSchemaLoader', () => {
});

describe('fetchFile', () => {
it('should use the file and the DEFAULT_CATALOG_PATH in the path', async () => {
it('should fetch the file', async () => {
await CatalogSchemaLoader.fetchFile('file.json');

expect(fetchMock).toHaveBeenCalledWith('./camel-catalog/file.json');
expect(fetchMock).toHaveBeenCalledWith('file.json');
});

it('should return the body and the uri', async () => {
Expand All @@ -32,7 +32,7 @@ describe('CatalogSchemaLoader', () => {
body: {
content: 'file-content',
},
uri: 'http://localhost/./camel-catalog/file.json',
uri: 'http://localhost/file.json',
});
});
});
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('CatalogSchemaLoader', () => {
});

it('should return the schemas files', async () => {
const schemaPromises = CatalogSchemaLoader.getSchemasFiles(schemasEntries);
const schemaPromises = CatalogSchemaLoader.getSchemasFiles('http://localhost', schemasEntries);

const result = await Promise.all(schemaPromises);

Expand Down
10 changes: 5 additions & 5 deletions packages/ui/src/utils/catalog-schema-loader.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Schema, SchemaEntry } from '../models';

export class CatalogSchemaLoader {
static readonly DEFAULT_CATALOG_PATH = '/camel-catalog';
/** The `.` is required to support relative routes in GitHub pages */
static readonly DEFAULT_CATALOG_PATH = './camel-catalog';
static readonly VISUAL_FLOWS = ['route', 'Integration', 'Kamelet', 'KameletBinding', 'Pipe'];

static async fetchFile<T>(file: string): Promise<{ body: T; uri: string }> {
/** The `.` is required to support relative routes in GitHub pages */
const response = await fetch(`.${this.DEFAULT_CATALOG_PATH}/${file}`);
const response = await fetch(file);
const body = await response.json();

return { body, uri: response.url };
}

static getSchemasFiles(schemaFiles: Record<string, SchemaEntry>): Promise<Schema>[] {
static getSchemasFiles(basePath: string, schemaFiles: Record<string, SchemaEntry>): Promise<Schema>[] {
return Object.entries(schemaFiles).map(async ([name, schemaDef]) => {
const fetchedSchema = await this.fetchFile(schemaDef.file);
const fetchedSchema = await this.fetchFile(`${basePath}/${schemaDef.file}`);
const tags = [];

if (this.VISUAL_FLOWS.includes(name)) {
Expand Down
Loading