Skip to content

Commit

Permalink
Searchbar style + Breadcrumb names (#21)
Browse files Browse the repository at this point in the history
* sidebar now collapses properly

* remove _explorer endpoint

* breadcrumbs show proper names now
  • Loading branch information
rambleraptor authored Dec 31, 2024
1 parent 9ee31ee commit d2f6334
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
4 changes: 0 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ function createRoutes(resources: ResourceSchema[]): RouteObject[] {
path: "/",
element: <SpecSpecifierPage />,
},
{
path: "/_explorer",
element: <div />,
},
].concat(resources.map((resource) => {
return [
{
Expand Down
17 changes: 12 additions & 5 deletions src/app/spec_specifier/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { LoginForm } from "@/components/login-form";
import { useAppSelector } from "@/hooks/store";
import { schemaState } from "@/state/store";

export default function SpecSpecifierPage() {
return (
<div className="flex h-screen w-full items-center justify-center px-4">
<LoginForm />
</div>
);
const state = useAppSelector(schemaState);
if(state == 'unset') {
return (
<div className="flex h-screen w-full items-center justify-center px-4">
<LoginForm />
</div>
)
} else {
return <div />
}
}
2 changes: 1 addition & 1 deletion src/components/app-sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
const resources = useAppSelector(selectResources);

return (
<Sidebar collapsible="icon" {...props}>
<Sidebar {...props}>
<SidebarHeader>
<HeadersInput />
</SidebarHeader>
Expand Down
8 changes: 7 additions & 1 deletion src/components/breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { useMatches } from "react-router-dom";
import { Breadcrumb, BreadcrumbItem, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator } from "./ui/breadcrumb"
import { createRouteObjects } from "@/lib/utils";
import { useAppSelector } from "@/hooks/store";
import { selectResources } from "@/state/store";

export default function AppBreadcrumb() {
const breadcrumbs = useMatches();
const resources = useAppSelector(selectResources);

const routeObjects = createRouteObjects(resources);

const breadcrumb_elements = [];
for(const i of breadcrumbs) {
breadcrumb_elements.push(<BreadcrumbItem className="hidden md:block">
<BreadcrumbPage>{i.pathname}</BreadcrumbPage>
<BreadcrumbPage>{routeObjects[i.pathname]}</BreadcrumbPage>
</BreadcrumbItem>);
breadcrumb_elements.push(<BreadcrumbSeparator className="hidden md:block" />);
}
Expand Down
3 changes: 0 additions & 3 deletions src/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import { toast } from "@/hooks/use-toast";
import { OpenAPI } from "@/state/openapi";
import { setSchema } from "@/state/store";
import { useState } from "react";
import { useNavigate } from "react-router-dom";

export function LoginForm() {
const [state, setState] = useState("");
const navigate = useNavigate();
const dispatch = useAppDispatch();

const onSubmit = async (event: React.FormEvent) => {
Expand All @@ -26,7 +24,6 @@ export function LoginForm() {
const response = await fetch(state);
const data = await response.json();
dispatch(setSchema(new OpenAPI(data)));
navigate("/_explorer");
} catch (error) {
toast({description: `Failed to fetch OpenAPI spec: ${error}`})
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ export function cn(...inputs: ClassValue[]) {
}

export function createRouteObjects(resources: ResourceSchema[]): object {
let base = {
'/': 'Home',
}
if(resources === null) {
return {};
return base;
}
return resources.reduce((acc, resource) => {
acc[resource.base_url()] = `${resource.plural_name}`;
acc[`${resource.base_url()}/_create`] = `${resource.singular_name} Create`;
acc[`${resource.base_url()}/:resourceId`] = `${resource.singular_name} Info`;
acc[`${resource.base_url()}/:resourceId/_update`] = `${resource.singular_name} Update`;
return acc;
}, {});
}, base);
}
9 changes: 6 additions & 3 deletions src/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { OpenAPI } from './openapi'
// Schema reducers + selectors.
interface SchemaState {
value: OpenAPI | null
state: 'unset' | 'set'
}

const initialState: SchemaState = {
value: null
value: null,
state: 'unset'
}

const schemaSlice = createSlice({
Expand All @@ -17,6 +19,7 @@ const schemaSlice = createSlice({
reducers: {
setSchema: (state, action: PayloadAction<OpenAPI>) => {
state.value = action.payload
state.state = 'set';
}
}
})
Expand All @@ -32,6 +35,8 @@ export const selectResources = (state: RootState) => {
}
}

export const schemaState = (state: RootState) => state.schema.state

// Headers reducers + selectors.
interface HeadersState {
value: string
Expand All @@ -56,8 +61,6 @@ const headersReducer = headersSlice.reducer;

export const selectHeaders = (state: RootState) => state.headers.value;



// Store
const store = configureStore({
reducer: {
Expand Down

0 comments on commit d2f6334

Please sign in to comment.