-
Notifications
You must be signed in to change notification settings - Fork 9
feat: added example admin page at /_fake/admin #7
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import React from "react" | ||
| import { renderToString } from "react-dom/server" | ||
| import type { Middleware } from "winterspec/middleware" | ||
| import type { ReactNode } from "react" | ||
| import { Fragment } from "react" | ||
|
|
||
| const tailwindStyle = ` | ||
| .btn { | ||
| @apply text-white visited:text-white m-1 bg-blue-500 hover:bg-blue-700 font-bold py-2 px-4 rounded; | ||
| } | ||
| a { | ||
| @apply underline text-blue-600 hover:text-blue-800 visited:text-purple-800 m-1; | ||
| } | ||
| h2 { | ||
| @apply text-xl font-bold my-2; | ||
| } | ||
| input, select { | ||
| @apply border border-gray-300 rounded p-1 ml-0.5; | ||
| } | ||
| form { | ||
| @apply inline-flex flex-col gap-2 border border-gray-300 rounded p-2 m-2 text-xs; | ||
| } | ||
| button { | ||
| @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded; | ||
| } | ||
| ` | ||
|
|
||
| const renderBreadcrumbs = (pathComponents: string[]) => | ||
| pathComponents.map((component, index) => ( | ||
| <Fragment key={index}> | ||
| <span className="px-0.5 text-gray-500">/</span> | ||
| <a href={`/${pathComponents.slice(0, index + 1).join("/")}`}> | ||
| {component} | ||
| </a> | ||
| </Fragment> | ||
| )) | ||
|
|
||
| const renderTimezoneDropdown = (timezone: string) => ( | ||
| <select | ||
| id="timezone-select" | ||
| className="text-xs" | ||
| onChange={(e) => { | ||
| document.cookie = `timezone=${e.currentTarget.value};path=/` | ||
| location.reload() | ||
| }} | ||
| defaultValue={timezone} | ||
| > | ||
| <option value="UTC">UTC</option> | ||
| <option value="America/Los_Angeles">Pacific</option> | ||
| <option value="Asia/Kolkata">IST</option> | ||
| </select> | ||
| ) | ||
|
|
||
| export const withReact: Middleware< | ||
| {}, | ||
| { react: (component: ReactNode) => Response } | ||
| > = async (req, ctx, next) => { | ||
| ctx.react = (component: ReactNode) => { | ||
| const pathComponents = new URL(req.url).pathname.split("/").filter(Boolean) | ||
| const timezone = req.headers.get("X-Timezone") || "UTC" | ||
|
|
||
| const html = renderToString( | ||
| <html lang="en"> | ||
| <head> | ||
| <script src="https://cdn.tailwindcss.com" /> | ||
| <style type="text/tailwindcss">{tailwindStyle}</style> | ||
| </head> | ||
| <body> | ||
| <div> | ||
| <div className="border-b border-gray-300 py-1 flex justify-between items-center"> | ||
| <div> | ||
| <span className="px-1 pr-2">admin panel</span> | ||
| {renderBreadcrumbs(pathComponents)} | ||
| </div> | ||
| <div className="mr-2 flex items-center"> | ||
| <div className="text-xs text-gray-500 mr-1"> | ||
| {new Date().toLocaleString()} | ||
| </div> | ||
| {renderTimezoneDropdown(timezone)} | ||
| </div> | ||
| </div> | ||
| <div className="flex flex-col text-xs p-1">{component}</div> | ||
| </div> | ||
| </body> | ||
| </html> | ||
| ) | ||
|
|
||
| return new Response(html, { | ||
| headers: { | ||
| "Content-Type": "text/html", | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| return await next(req, ctx) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import { createWithWinterSpec } from "winterspec" | ||
| import type { Middleware } from "winterspec/middleware" | ||
| import { withDb } from "./with-db" | ||
| import { withReact } from "./with-react" | ||
|
|
||
| export const withRouteSpec = createWithWinterSpec({ | ||
| apiName: "tscircuit Debug API", | ||
| productionServerUrl: "https://debug-api.tscircuit.com", | ||
| beforeAuthMiddleware: [], | ||
| beforeAuthMiddleware: [withReact], | ||
| authMiddleware: {}, | ||
| afterAuthMiddleware: [withDb], | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import React from "react" | ||
| import { withRouteSpec } from "lib/middleware/with-winter-spec" | ||
| import { z } from "zod" | ||
|
|
||
| const AdminPage = ({ things }: { things: any[] }) => { | ||
| return ( | ||
| <div> | ||
| <h2>Things Management</h2> | ||
| <div className="flex flex-col gap-4"> | ||
| <form | ||
| action="/things/create" | ||
| method="POST" | ||
| className="border p-4 rounded" | ||
| > | ||
| <h3 className="font-bold mb-2">Create New Thing</h3> | ||
| <div className="flex flex-col gap-2"> | ||
| <input | ||
| type="text" | ||
| name="name" | ||
| placeholder="Name" | ||
| required | ||
| className="border p-2 rounded" | ||
| /> | ||
| <input | ||
| type="text" | ||
| name="description" | ||
| placeholder="Description" | ||
| required | ||
| className="border p-2 rounded" | ||
| /> | ||
| <button type="submit" className="btn"> | ||
| Create Thing | ||
| </button> | ||
| </div> | ||
| </form> | ||
|
|
||
| <div className="border p-4 rounded"> | ||
| <h3 className="font-bold mb-2">Existing Things</h3> | ||
| <div className="grid gap-2"> | ||
| {things.map((thing) => ( | ||
| <div | ||
| key={thing.thing_id} | ||
| className="border p-2 rounded flex justify-between items-center" | ||
| > | ||
| <div> | ||
| <div className="font-bold">{thing.name}</div> | ||
| <div className="text-gray-600">{thing.description}</div> | ||
| </div> | ||
| <div className="text-xs text-gray-500"> | ||
| ID: {thing.thing_id} | ||
| </div> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| export default withRouteSpec({ | ||
| methods: ["GET"], | ||
| jsonResponse: z.object({ | ||
| things: z.array( | ||
| z.object({ | ||
| thing_id: z.string(), | ||
| name: z.string(), | ||
| description: z.string(), | ||
| }) | ||
| ), | ||
| }), | ||
| })(async (req, ctx) => { | ||
| const things = ctx.db.things | ||
| return ctx.react(<AdminPage things={things} />) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
incorrect route spec, surprised this works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no jsonResponse