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

fix: branch selector #2518

Merged
merged 4 commits into from
Feb 5, 2024
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
2 changes: 1 addition & 1 deletion packages/elements-dev-portal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stoplight/elements-dev-portal",
"version": "2.0.2",
"version": "2.0.3",
"description": "UI components for composing beautiful developer documentation.",
"keywords": [],
"sideEffects": [
Expand Down
54 changes: 28 additions & 26 deletions packages/elements-dev-portal/src/__fixtures__/branches.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
[
{
"id": "YnI6NDc4MDUy",
"name": null,
"slug": "master",
"is_default": true,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
},
{
"id": "YnI6OTYwNDM4",
"name": null,
"slug": "v2",
"is_default": false,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
},
{
"id": "YnI6OTYwNDM5",
"name": null,
"slug": "feat/toc-file",
"is_default": false,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
}
]
{
"items": [
{
"id": "YnI6NDc4MDUy",
"name": null,
"slug": "master",
"is_default": true,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
},
{
"id": "YnI6OTYwNDM4",
"name": null,
"slug": "v2",
"is_default": false,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
},
{
"id": "YnI6OTYwNDM5",
"name": null,
"slug": "feat/toc-file",
"is_default": false,
"is_published": true,
"project_id": "cHJqOjM1ODEw"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@ import { render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import * as React from 'react';

import { Branch } from '../../types';
import { BranchSelector } from './BranchSelector';

export const branches = [
export const branches: Branch[] = [
{
id: 0,
slug: 'main',
is_default: true,
is_published: false,
projectId: 123,
name: 'main-name',
},
{
id: 1,
slug: 'beta',
is_default: false,
is_published: false,
projectId: 123,
},
{
id: 2,
slug: 'feature-branch',
is_default: false,
is_published: false,
projectId: 123,
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BranchSelectorWrapper = ({ projectId }: { projectId: string }) => {
if (data) {
return (
<Box mt={10} w={40}>
<BranchSelector branchSlug={branchSlug} branches={data} onChange={branch => setBranchSlug(branch.slug)} />
<BranchSelector branchSlug={branchSlug} branches={data.items} onChange={branch => setBranchSlug(branch.slug)} />
</Box>
);
}
Expand Down
26 changes: 14 additions & 12 deletions packages/elements-dev-portal/src/containers/StoplightProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@stoplight/elements-core';
import { Box, Button, Icon, useModalState } from '@stoplight/mosaic';
import * as React from 'react';
import { Link, Redirect, Route, useHistory, useParams } from 'react-router-dom';
import { Link, Redirect, Route, Switch, useHistory, useParams } from 'react-router-dom';

import { BranchSelector } from '../components/BranchSelector';
import { DevPortalProvider } from '../components/DevPortalProvider';
Expand Down Expand Up @@ -198,10 +198,10 @@ const StoplightProjectImpl: React.FC<StoplightProjectProps> = ({
ref={container}
sidebar={
<>
{branches && branches.length > 1 ? (
{branches && branches.items.length > 1 ? (
<BranchSelector
branchSlug={branchSlug}
branches={branches}
branches={branches.items}
onChange={branch => {
const encodedBranchSlug = encodeURIComponent(branch.slug);
history.push(branch.is_default ? `/${nodeSlug}` : `/branches/${encodedBranchSlug}/${nodeSlug}`);
Expand Down Expand Up @@ -238,17 +238,19 @@ const StoplightProjectRouter = ({
<DevPortalProvider platformUrl={platformUrl}>
<RouterTypeContext.Provider value={router}>
<Router {...routerProps} key={basePath}>
<Route path="/branches/:branchSlug/:nodeSlug+" exact>
<StoplightProjectImpl {...props} />
</Route>
<Switch>
<Route path="/branches/:branchSlug/:nodeSlug+" exact>
<StoplightProjectImpl {...props} />
</Route>

<Route path="/:nodeSlug+" exact>
<StoplightProjectImpl {...props} />
</Route>
<Route path="/:nodeSlug+" exact>
<StoplightProjectImpl {...props} />
</Route>

<Route path="/" exact>
<StoplightProjectImpl {...props} />
</Route>
<Route path="/" exact>
<StoplightProjectImpl {...props} />
</Route>
</Switch>
</Router>
</RouterTypeContext.Provider>
</DevPortalProvider>
Expand Down
4 changes: 2 additions & 2 deletions packages/elements-dev-portal/src/handlers/getBranches.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Branch } from '../types';
import { Branches } from '../types';
import { appVersion } from '../version';

export const getBranches = async ({
Expand All @@ -9,7 +9,7 @@ export const getBranches = async ({
projectId: string;
platformUrl?: string;
platformAuthToken?: string;
}): Promise<Branch[]> => {
}): Promise<Branches> => {
const encodedProjectId = encodeURIComponent(projectId);
const response = await fetch(`${platformUrl}/api/v1/projects/${encodedProjectId}/branches`, {
headers: {
Expand Down
5 changes: 4 additions & 1 deletion packages/elements-dev-portal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ export type Branch = {
slug: string;
is_default: boolean;
is_published: boolean;
projectId: number;
name?: string;
};

export type Branches = {
items: Branch[];
};

export type ProjectTableOfContents = {
items: TableOfContentsItem[];
hide_powered_by?: boolean;
Expand Down