@tscircuit/fake-snippets 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/CODEOWNERS +1 -0
- package/.github/workflows/bun-formatcheck.yml +26 -0
- package/.github/workflows/bun-test.yml +28 -0
- package/.github/workflows/bun-typecheck.yml +26 -0
- package/.github/workflows/bundle-size-analysis.yml +79 -0
- package/.github/workflows/playwright-test.yml +37 -0
- package/.github/workflows/stale.yml +40 -0
- package/.github/workflows/update-snapshots.yml +49 -0
- package/CONTRIBUTING.md +59 -0
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/biome.json +60 -0
- package/bun-tests/fake-snippets-api/fixtures/get-circuit-json.ts +148 -0
- package/bun-tests/fake-snippets-api/fixtures/get-test-server.ts +96 -0
- package/bun-tests/fake-snippets-api/fixtures/start-server.ts +44 -0
- package/bun-tests/fake-snippets-api/routes/accounts/get_account_balance.test.ts +18 -0
- package/bun-tests/fake-snippets-api/routes/health.test.ts +9 -0
- package/bun-tests/fake-snippets-api/routes/order_files/get.test.ts +48 -0
- package/bun-tests/fake-snippets-api/routes/order_files/upload.test.ts +77 -0
- package/bun-tests/fake-snippets-api/routes/orders/create.test.ts +19 -0
- package/bun-tests/fake-snippets-api/routes/orders/get.test.ts +38 -0
- package/bun-tests/fake-snippets-api/routes/orders/list.test.ts +30 -0
- package/bun-tests/fake-snippets-api/routes/orders/update.test.ts +46 -0
- package/bun-tests/fake-snippets-api/routes/snippets/add_star.test.ts +114 -0
- package/bun-tests/fake-snippets-api/routes/snippets/create.test.ts +28 -0
- package/bun-tests/fake-snippets-api/routes/snippets/delete.test.ts +106 -0
- package/bun-tests/fake-snippets-api/routes/snippets/download.test.ts +90 -0
- package/bun-tests/fake-snippets-api/routes/snippets/generate_from_jlcpcb.test.ts +25 -0
- package/bun-tests/fake-snippets-api/routes/snippets/get_image.test.ts +113 -0
- package/bun-tests/fake-snippets-api/routes/snippets/images.test.ts +112 -0
- package/bun-tests/fake-snippets-api/routes/snippets/list.test.ts +62 -0
- package/bun-tests/fake-snippets-api/routes/snippets/list_newest.test.ts +48 -0
- package/bun-tests/fake-snippets-api/routes/snippets/list_trending.test.ts +69 -0
- package/bun-tests/fake-snippets-api/routes/snippets/remove_star.test.ts +110 -0
- package/bun-tests/fake-snippets-api/routes/snippets/search.test.ts +75 -0
- package/bun-tests/fake-snippets-api/routes/snippets/star-count.test.ts +44 -0
- package/bun-tests/fake-snippets-api/routes/snippets/update.test.ts +116 -0
- package/bun-tests/parts-engine.test.ts +18 -0
- package/bun.lockb +0 -0
- package/bunfig.toml +2 -0
- package/components.json +20 -0
- package/dist/assets/editor_example_1-1000w.webp +0 -0
- package/dist/assets/editor_example_1-1200w.webp +0 -0
- package/dist/assets/editor_example_1-1600w.webp +0 -0
- package/dist/assets/editor_example_1-2000w.webp +0 -0
- package/dist/assets/editor_example_1-400w.webp +0 -0
- package/dist/assets/editor_example_1-600w.webp +0 -0
- package/dist/assets/editor_example_1-800w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-1000w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-1200w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-1600w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-2000w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-400w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-600w.webp +0 -0
- package/dist/assets/editor_example_1_more_square-800w.webp +0 -0
- package/dist/assets/editor_example_2-1000w.webp +0 -0
- package/dist/assets/editor_example_2-1200w.webp +0 -0
- package/dist/assets/editor_example_2-1600w.webp +0 -0
- package/dist/assets/editor_example_2-2000w.webp +0 -0
- package/dist/assets/editor_example_2-400w.webp +0 -0
- package/dist/assets/editor_example_2-600w.webp +0 -0
- package/dist/assets/editor_example_2-800w.webp +0 -0
- package/dist/assets/example_schematic-1000w.webp +0 -0
- package/dist/assets/example_schematic-1200w.webp +0 -0
- package/dist/assets/example_schematic-1600w.webp +0 -0
- package/dist/assets/example_schematic-2000w.webp +0 -0
- package/dist/assets/example_schematic-400w.webp +0 -0
- package/dist/assets/example_schematic-600w.webp +0 -0
- package/dist/assets/example_schematic-800w.webp +0 -0
- package/dist/bundle.js +3270 -0
- package/dist/robots.txt +9 -0
- package/dist/sitemap.xml +118 -0
- package/docs/CIRCUIT_JSON_SOURCE_COMPONENT_OVERVIEW.md +151 -0
- package/fake-snippets-api/README.md +6 -0
- package/fake-snippets-api/biome.json +47 -0
- package/fake-snippets-api/bun.lockb +0 -0
- package/fake-snippets-api/lib/db/autoload-dev-snippets.ts +84 -0
- package/fake-snippets-api/lib/db/autoload-snippets.json +24 -0
- package/fake-snippets-api/lib/db/db-client.ts +343 -0
- package/fake-snippets-api/lib/db/schema.ts +112 -0
- package/fake-snippets-api/lib/db/seed.ts +1608 -0
- package/fake-snippets-api/lib/middleware/with-ctx-error.ts +26 -0
- package/fake-snippets-api/lib/middleware/with-db.ts +15 -0
- package/fake-snippets-api/lib/middleware/with-error-handling.ts +24 -0
- package/fake-snippets-api/lib/middleware/with-optional-session-auth.ts +34 -0
- package/fake-snippets-api/lib/middleware/with-request-logging.ts +54 -0
- package/fake-snippets-api/lib/middleware/with-session-auth.ts +39 -0
- package/fake-snippets-api/lib/middleware/with-winter-spec.ts +24 -0
- package/fake-snippets-api/next-env.d.ts +5 -0
- package/fake-snippets-api/routes/api/accounts/get.ts +21 -0
- package/fake-snippets-api/routes/api/accounts/get_account_balance.ts +22 -0
- package/fake-snippets-api/routes/api/accounts/update.ts +32 -0
- package/fake-snippets-api/routes/api/ai/[...anyroute].ts +31 -0
- package/fake-snippets-api/routes/api/ai.ts +2 -0
- package/fake-snippets-api/routes/api/aistream/[...anyroute].ts +65 -0
- package/fake-snippets-api/routes/api/health.ts +9 -0
- package/fake-snippets-api/routes/api/internal/sessions/create_without_auth.ts +63 -0
- package/fake-snippets-api/routes/api/order_files/get.ts +28 -0
- package/fake-snippets-api/routes/api/order_files/upload.ts +43 -0
- package/fake-snippets-api/routes/api/orders/create.ts +41 -0
- package/fake-snippets-api/routes/api/orders/get.ts +28 -0
- package/fake-snippets-api/routes/api/orders/list.ts +15 -0
- package/fake-snippets-api/routes/api/orders/update.ts +50 -0
- package/fake-snippets-api/routes/api/snippets/add_star.ts +42 -0
- package/fake-snippets-api/routes/api/snippets/create.ts +55 -0
- package/fake-snippets-api/routes/api/snippets/delete.ts +41 -0
- package/fake-snippets-api/routes/api/snippets/download.ts +148 -0
- package/fake-snippets-api/routes/api/snippets/generate_from_jlcpcb.ts +55 -0
- package/fake-snippets-api/routes/api/snippets/get.ts +50 -0
- package/fake-snippets-api/routes/api/snippets/get_image.ts +65 -0
- package/fake-snippets-api/routes/api/snippets/images/[author]/[snippet_name]/[typeFormat].ts +74 -0
- package/fake-snippets-api/routes/api/snippets/list.ts +28 -0
- package/fake-snippets-api/routes/api/snippets/list_newest.ts +13 -0
- package/fake-snippets-api/routes/api/snippets/list_trending.ts +21 -0
- package/fake-snippets-api/routes/api/snippets/remove_star.ts +42 -0
- package/fake-snippets-api/routes/api/snippets/search.ts +18 -0
- package/fake-snippets-api/routes/api/snippets/update.ts +86 -0
- package/favicon.ico +0 -0
- package/index.html +23 -0
- package/landing.html +23 -0
- package/package.json +164 -0
- package/playwright-tests/ai-page.spec.ts +19 -0
- package/playwright-tests/cmd-click.spec.ts +43 -0
- package/playwright-tests/dashboard-page.spec.ts +10 -0
- package/playwright-tests/editor-page.spec.ts +15 -0
- package/playwright-tests/files-dialog.spec.ts +19 -0
- package/playwright-tests/footprint-dialog/footprint-dialog.spec.ts +27 -0
- package/playwright-tests/footprint-dialog/footprint-insertion.spec.ts +38 -0
- package/playwright-tests/footprint-dialog/footprint-preview.spec.ts +34 -0
- package/playwright-tests/footprint-dialog/footprint-selection.spec.ts +29 -0
- package/playwright-tests/handle-manual-edits.spec.ts +55 -0
- package/playwright-tests/home-page.spec.ts +10 -0
- package/playwright-tests/images.spec.ts +17 -0
- package/playwright-tests/manual-edits.spec.ts +89 -0
- package/playwright-tests/preview-page.spec.ts +14 -0
- package/playwright-tests/quickstart-page.spec.ts +10 -0
- package/playwright-tests/search-links.spec.ts +21 -0
- package/playwright-tests/search.spec.ts +27 -0
- package/playwright-tests/snapshots/ai-page.spec.ts-AI-Page-lg.png +0 -0
- package/playwright-tests/snapshots/ai-page.spec.ts-AI-Page-md.png +0 -0
- package/playwright-tests/snapshots/ai-page.spec.ts-AI-Page-xs.png +0 -0
- package/playwright-tests/snapshots/cmd-click.spec.ts-underlined-imports.png +0 -0
- package/playwright-tests/snapshots/dashboard-page.spec.ts-Dashboard-page-lg.png +0 -0
- package/playwright-tests/snapshots/dashboard-page.spec.ts-Dashboard-page-md.png +0 -0
- package/playwright-tests/snapshots/dashboard-page.spec.ts-Dashboard-page-xs.png +0 -0
- package/playwright-tests/snapshots/editor-page.spec.ts-editor-with-snippet.png +0 -0
- package/playwright-tests/snapshots/error-fallback.spec.ts-error-fallback-lg.png +0 -0
- package/playwright-tests/snapshots/error-fallback.spec.ts-error-fallback-md.png +0 -0
- package/playwright-tests/snapshots/error-fallback.spec.ts-error-fallback-xs.png +0 -0
- package/playwright-tests/snapshots/files-dialog.spec.ts-view-snippet-files.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-dialog.spec.ts-footprint-preview-lg.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-dialog.spec.ts-footprint-preview-md.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-dialog.spec.ts-footprint-preview-xs.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-insertion.spec.ts-footprint-insertion-lg.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-insertion.spec.ts-footprint-insertion-md.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-insertion.spec.ts-footprint-insertion-xs.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-preview.spec.ts-footprint-preview-lg.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-preview.spec.ts-footprint-preview-md.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-preview.spec.ts-footprint-preview-xs.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-selection.spec.ts-footprint-preview-lg.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-selection.spec.ts-footprint-preview-md.png +0 -0
- package/playwright-tests/snapshots/footprint-dialog/footprint-selection.spec.ts-footprint-preview-xs.png +0 -0
- package/playwright-tests/snapshots/handle-manual-edits.spec.ts-handle-manual-edits.png +0 -0
- package/playwright-tests/snapshots/home-page.spec.ts-Home-page-lg.png +0 -0
- package/playwright-tests/snapshots/home-page.spec.ts-Home-page-md.png +0 -0
- package/playwright-tests/snapshots/home-page.spec.ts-Home-page-xs.png +0 -0
- package/playwright-tests/snapshots/images.spec.ts-pcb-image.png +0 -0
- package/playwright-tests/snapshots/images.spec.ts-schematic-image.png +0 -0
- package/playwright-tests/snapshots/manual-edits.spec.ts-editor-manual-edits.png +0 -0
- package/playwright-tests/snapshots/manual-edits.spec.ts-manual-edits-view.png +0 -0
- package/playwright-tests/snapshots/preview-page.spec.ts-preview-snippet-pcb.png +0 -0
- package/playwright-tests/snapshots/preview-page.spec.ts-preview-snippet-schematic.png +0 -0
- package/playwright-tests/snapshots/quickstart-page.spec.ts-Quickstart-Pagelg.png +0 -0
- package/playwright-tests/snapshots/quickstart-page.spec.ts-Quickstart-Pagemd.png +0 -0
- package/playwright-tests/snapshots/quickstart-page.spec.ts-Quickstart-Pagexs.png +0 -0
- package/playwright-tests/snapshots/search-links.spec.ts-search-links.png +0 -0
- package/playwright-tests/snapshots/search.spec.ts-search-lg.png +0 -0
- package/playwright-tests/snapshots/search.spec.ts-search-md.png +0 -0
- package/playwright-tests/snapshots/search.spec.ts-search-xs.png +0 -0
- package/playwright-tests/snapshots/star.spec.ts-remove-star-button.png +0 -0
- package/playwright-tests/snapshots/star.spec.ts-star-button.png +0 -0
- package/playwright-tests/snapshots/update-description.spec.ts-update-description.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-after-lg.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-after-md.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-after-xs.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-before-lg.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-before-md.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-before-xs.png +0 -0
- package/playwright-tests/snapshots/view-snippet.spec.ts-view-snippet-files.png +0 -0
- package/playwright-tests/star.spec.ts +40 -0
- package/playwright-tests/update-description.spec.ts +18 -0
- package/playwright-tests/view-snippet.spec.ts +35 -0
- package/playwright-tests/viewports.ts +5 -0
- package/playwright.config.ts +27 -0
- package/postcss.config.js +6 -0
- package/public/robots.txt +9 -0
- package/renovate.json +24 -0
- package/scripts/generate-image-sizes.ts +58 -0
- package/scripts/generate-sitemap.ts +103 -0
- package/scripts/generate_bundle_stats.js +192 -0
- package/scripts/snapshot.ts +35 -0
- package/src/App.tsx +113 -0
- package/src/ContextProviders.tsx +9 -0
- package/src/assets/originals/editor_example_1.webp +0 -0
- package/src/assets/originals/editor_example_1_more_square.webp +0 -0
- package/src/assets/originals/editor_example_2.webp +0 -0
- package/src/assets/originals/example_schematic.webp +0 -0
- package/src/components/AiChatInterface.tsx +221 -0
- package/src/components/AiChatMessage.tsx +86 -0
- package/src/components/Analytics.tsx +30 -0
- package/src/components/BomTable.tsx +69 -0
- package/src/components/ChatInput.tsx +53 -0
- package/src/components/CircuitToSvgWithMouseControl.tsx +78 -0
- package/src/components/CmdKMenu.tsx +301 -0
- package/src/components/CodeAndPreview.tsx +258 -0
- package/src/components/CodeEditor.tsx +460 -0
- package/src/components/CodeEditorHeader.tsx +160 -0
- package/src/components/CreateNewSnippetWithAiHero.tsx +77 -0
- package/src/components/DownloadButtonAndMenu.tsx +212 -0
- package/src/components/EditorNav.tsx +428 -0
- package/src/components/ErrorFallback.tsx +25 -0
- package/src/components/ErrorTabContent.tsx +122 -0
- package/src/components/FAQ.tsx +113 -0
- package/src/components/Footer.tsx +134 -0
- package/src/components/Footer2.tsx +100 -0
- package/src/components/FootprintDialog.tsx +342 -0
- package/src/components/Header.tsx +187 -0
- package/src/components/Header2.tsx +135 -0
- package/src/components/HeaderDropdown.tsx +83 -0
- package/src/components/HeaderLogin.tsx +94 -0
- package/src/components/JLCPCBImportDialog.tsx +155 -0
- package/src/components/LandingHero.tsx +175 -0
- package/src/components/LatestSnippets.tsx +39 -0
- package/src/components/OptimizedImage.tsx +96 -0
- package/src/components/OrderPreviewContent.tsx +61 -0
- package/src/components/ParametersEditor.tsx +140 -0
- package/src/components/PcbViewerWithContainerHeight.tsx +47 -0
- package/src/components/PrefetchPageLink.tsx +45 -0
- package/src/components/PreviewContent.tsx +375 -0
- package/src/components/PreviewEmptyState.tsx +16 -0
- package/src/components/RunButton.tsx +27 -0
- package/src/components/SearchComponent.tsx +163 -0
- package/src/components/ShippingInformationForm.tsx +423 -0
- package/src/components/SnippetLink.tsx +37 -0
- package/src/components/StaticPreviewContent.tsx +89 -0
- package/src/components/StaticViewSnippetHeader.tsx +70 -0
- package/src/components/StaticViewSnippetSidebar.tsx +100 -0
- package/src/components/TableViewer/CircuitJsonTableViewer.tsx +316 -0
- package/src/components/TableViewer/ClickableText.tsx +21 -0
- package/src/components/TableViewer/HeaderCell.tsx +27 -0
- package/src/components/TableViewer/Modal.tsx +39 -0
- package/src/components/TrendingSnippetCarousel.tsx +77 -0
- package/src/components/TypeBadge.tsx +31 -0
- package/src/components/ViewSnippetHeader.tsx +144 -0
- package/src/components/ViewSnippetSidebar.tsx +162 -0
- package/src/components/dialogs/confirm-delete-snippet-dialog.tsx +80 -0
- package/src/components/dialogs/create-order-dialog.tsx +146 -0
- package/src/components/dialogs/create-use-dialog.tsx +34 -0
- package/src/components/dialogs/edit-description-dialog.tsx +96 -0
- package/src/components/dialogs/files-dialog.tsx +70 -0
- package/src/components/dialogs/import-snippet-dialog.tsx +84 -0
- package/src/components/dialogs/rename-snippet-dialog.tsx +81 -0
- package/src/components/dialogs/view-ts-files-dialog.tsx +89 -0
- package/src/components/ui/accordion.tsx +55 -0
- package/src/components/ui/alert-dialog.tsx +139 -0
- package/src/components/ui/alert.tsx +59 -0
- package/src/components/ui/aspect-ratio.tsx +5 -0
- package/src/components/ui/avatar.tsx +48 -0
- package/src/components/ui/badge.tsx +36 -0
- package/src/components/ui/breadcrumb.tsx +118 -0
- package/src/components/ui/button.tsx +58 -0
- package/src/components/ui/calendar.tsx +73 -0
- package/src/components/ui/card.tsx +76 -0
- package/src/components/ui/carousel.tsx +260 -0
- package/src/components/ui/chart.tsx +363 -0
- package/src/components/ui/checkbox.tsx +28 -0
- package/src/components/ui/collapsible.tsx +9 -0
- package/src/components/ui/combobox.tsx +178 -0
- package/src/components/ui/command.tsx +151 -0
- package/src/components/ui/context-menu.tsx +202 -0
- package/src/components/ui/dialog.tsx +120 -0
- package/src/components/ui/drawer.tsx +116 -0
- package/src/components/ui/dropdown-menu.tsx +203 -0
- package/src/components/ui/form.tsx +182 -0
- package/src/components/ui/hover-card.tsx +27 -0
- package/src/components/ui/input-otp.tsx +69 -0
- package/src/components/ui/input.tsx +25 -0
- package/src/components/ui/label.tsx +24 -0
- package/src/components/ui/menubar.tsx +238 -0
- package/src/components/ui/navigation-menu.tsx +129 -0
- package/src/components/ui/pagination.tsx +121 -0
- package/src/components/ui/popover.tsx +31 -0
- package/src/components/ui/progress.tsx +26 -0
- package/src/components/ui/radio-group.tsx +42 -0
- package/src/components/ui/resizable.tsx +43 -0
- package/src/components/ui/scroll-area.tsx +46 -0
- package/src/components/ui/searchable-select.tsx +94 -0
- package/src/components/ui/select.tsx +162 -0
- package/src/components/ui/separator.tsx +29 -0
- package/src/components/ui/sheet.tsx +141 -0
- package/src/components/ui/skeleton.tsx +18 -0
- package/src/components/ui/slider.tsx +26 -0
- package/src/components/ui/sonner.tsx +30 -0
- package/src/components/ui/switch.tsx +27 -0
- package/src/components/ui/table.tsx +120 -0
- package/src/components/ui/tabs.tsx +53 -0
- package/src/components/ui/textarea.tsx +24 -0
- package/src/components/ui/toast.tsx +128 -0
- package/src/components/ui/toaster.tsx +33 -0
- package/src/components/ui/toggle-group.tsx +59 -0
- package/src/components/ui/toggle.tsx +43 -0
- package/src/components/ui/tooltip.tsx +28 -0
- package/src/entry-server.tsx +12 -0
- package/src/hooks/use-account-balance.ts +24 -0
- package/src/hooks/use-ai-api.ts +35 -0
- package/src/hooks/use-axios.ts +20 -0
- package/src/hooks/use-code-completion-ai-api.ts +11 -0
- package/src/hooks/use-compiled-tsx.ts +37 -0
- package/src/hooks/use-copy-to-clipboard.ts +26 -0
- package/src/hooks/use-create-snippet-mutation.ts +62 -0
- package/src/hooks/use-current-snippet-id.ts +75 -0
- package/src/hooks/use-current-snippet.ts +24 -0
- package/src/hooks/use-global-store.ts +33 -0
- package/src/hooks/use-is-using-fake-api.ts +3 -0
- package/src/hooks/use-run-tsx/construct-circuit.tsx +64 -0
- package/src/hooks/use-run-tsx/eval-compiled-js.ts +9 -0
- package/src/hooks/use-run-tsx/index.tsx +251 -0
- package/src/hooks/use-save-snippet.ts +66 -0
- package/src/hooks/use-sign-in.ts +22 -0
- package/src/hooks/use-snippet-by-name.ts +25 -0
- package/src/hooks/use-snippet.ts +23 -0
- package/src/hooks/use-snippets-base-api-url.ts +3 -0
- package/src/hooks/use-toast.tsx +208 -0
- package/src/hooks/use-typecheck.ts +54 -0
- package/src/hooks/use-url-params.ts +31 -0
- package/src/hooks/use-warn-user-on-page-change.ts +23 -0
- package/src/hooks/useForkSnippetMutation.ts +52 -0
- package/src/index.css +21 -0
- package/src/lib/__tests__/constants.test.ts +66 -0
- package/src/lib/base64ToBytes.ts +5 -0
- package/src/lib/bytesToBase64.ts +4 -0
- package/src/lib/codemirror/basic-setup.ts +51 -0
- package/src/lib/constants.ts +12 -0
- package/src/lib/decodeUrlHashToText.ts +15 -0
- package/src/lib/defaultCodeForBlankCode.tsx +7 -0
- package/src/lib/download-fns/createBlobURL.ts +4 -0
- package/src/lib/download-fns/download-assembly-svg.ts +12 -0
- package/src/lib/download-fns/download-circuit-json-fn.ts +12 -0
- package/src/lib/download-fns/download-dsn-file-fn.ts +12 -0
- package/src/lib/download-fns/download-fabrication-files.ts +233 -0
- package/src/lib/download-fns/download-gltf.ts +49 -0
- package/src/lib/download-fns/download-kicad-files.ts +27 -0
- package/src/lib/download-fns/download-readable-netlist.ts +12 -0
- package/src/lib/download-fns/download-schematic-svg.ts +12 -0
- package/src/lib/download-fns/download-simple-route-json.ts +17 -0
- package/src/lib/encodeTextToUrlHash.ts +17 -0
- package/src/lib/get-snippet-template.ts +26 -0
- package/src/lib/handleManualEditsImport.tsx +65 -0
- package/src/lib/jlc-parts-engine.ts +69 -0
- package/src/lib/templates/blank-3d-model-template.ts +12 -0
- package/src/lib/templates/blank-circuit-board-template.ts +24 -0
- package/src/lib/templates/blank-footprint-template.ts +29 -0
- package/src/lib/templates/blank-package-template.ts +22 -0
- package/src/lib/templates/blinking-led-board-template.ts +63 -0
- package/src/lib/templates/manual-edits-template.ts +5 -0
- package/src/lib/templates/usb-c-led-flashlight-template.ts +22 -0
- package/src/lib/utils/checkIfManualEditsImported.ts +6 -0
- package/src/lib/utils/getSyntaxError.ts +13 -0
- package/src/lib/utils/index.ts +6 -0
- package/src/lib/utils/load-prettier.ts +18 -0
- package/src/lib/utils/parseFootprintParams.ts +52 -0
- package/src/lib/utils/parseJsonOrNull.ts +8 -0
- package/src/lib/utils/pcbManualEditEventHandler.ts +156 -0
- package/src/main.tsx +10 -0
- package/src/pages/ai.tsx +92 -0
- package/src/pages/authorize.tsx +54 -0
- package/src/pages/dashboard.tsx +165 -0
- package/src/pages/dev-login.tsx +68 -0
- package/src/pages/editor.tsx +28 -0
- package/src/pages/landing.tsx +236 -0
- package/src/pages/my-orders.tsx +54 -0
- package/src/pages/newest.tsx +16 -0
- package/src/pages/preview.tsx +70 -0
- package/src/pages/quickstart.tsx +198 -0
- package/src/pages/search.tsx +26 -0
- package/src/pages/settings.tsx +25 -0
- package/src/pages/user-profile.tsx +97 -0
- package/src/pages/view-order.tsx +123 -0
- package/src/pages/view-snippet.tsx +149 -0
- package/src/prettier.ts +9 -0
- package/src/vite-env.d.ts +1 -0
- package/tailwind.config.js +47 -0
- package/tsconfig.json +30 -0
- package/vercel.json +50 -0
- package/vite.config.ts +146 -0
- package/winterspec.config.ts +6 -0
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { useSnippetsBaseApiUrl } from "@/hooks/use-snippets-base-api-url"
|
|
2
|
+
import { basicSetup } from "@/lib/codemirror/basic-setup"
|
|
3
|
+
import manualEditsTemplate from "@/lib/templates/manual-edits-template"
|
|
4
|
+
import { autocompletion } from "@codemirror/autocomplete"
|
|
5
|
+
import { indentWithTab } from "@codemirror/commands"
|
|
6
|
+
import { javascript } from "@codemirror/lang-javascript"
|
|
7
|
+
import { json } from "@codemirror/lang-json"
|
|
8
|
+
import { EditorState } from "@codemirror/state"
|
|
9
|
+
import { Decoration, hoverTooltip, keymap } from "@codemirror/view"
|
|
10
|
+
import { getImportsFromCode } from "@tscircuit/prompt-benchmarks/code-runner-utils"
|
|
11
|
+
import type { ATABootstrapConfig } from "@typescript/ata"
|
|
12
|
+
import { setupTypeAcquisition } from "@typescript/ata"
|
|
13
|
+
import { TSCI_PACKAGE_PATTERN } from "../lib/constants"
|
|
14
|
+
import {
|
|
15
|
+
createDefaultMapFromCDN,
|
|
16
|
+
createSystem,
|
|
17
|
+
createVirtualTypeScriptEnvironment,
|
|
18
|
+
} from "@typescript/vfs"
|
|
19
|
+
import {
|
|
20
|
+
tsAutocomplete,
|
|
21
|
+
tsFacet,
|
|
22
|
+
tsHover,
|
|
23
|
+
tsLinter,
|
|
24
|
+
tsSync,
|
|
25
|
+
} from "@valtown/codemirror-ts"
|
|
26
|
+
import { EditorView } from "codemirror"
|
|
27
|
+
import { useEffect, useMemo, useRef, useState } from "react"
|
|
28
|
+
import ts from "typescript"
|
|
29
|
+
import CodeEditorHeader from "./CodeEditorHeader"
|
|
30
|
+
import { copilotPlugin, Language } from "@valtown/codemirror-codeium"
|
|
31
|
+
import { useCodeCompletionApi } from "@/hooks/use-code-completion-ai-api"
|
|
32
|
+
const defaultImports = `
|
|
33
|
+
import React from "@types/react/jsx-runtime"
|
|
34
|
+
import { Circuit, createUseComponent } from "@tscircuit/core"
|
|
35
|
+
import type { CommonLayoutProps } from "@tscircuit/props"
|
|
36
|
+
`
|
|
37
|
+
export const CodeEditor = ({
|
|
38
|
+
onCodeChange,
|
|
39
|
+
onDtsChange,
|
|
40
|
+
readOnly = false,
|
|
41
|
+
initialCode = "",
|
|
42
|
+
manualEditsFileContent,
|
|
43
|
+
isStreaming = false,
|
|
44
|
+
showImportAndFormatButtons = true,
|
|
45
|
+
onManualEditsFileContentChanged,
|
|
46
|
+
}: {
|
|
47
|
+
onCodeChange: (code: string, filename?: string) => void
|
|
48
|
+
onDtsChange?: (dts: string) => void
|
|
49
|
+
initialCode: string
|
|
50
|
+
readOnly?: boolean
|
|
51
|
+
isStreaming?: boolean
|
|
52
|
+
manualEditsFileContent: string
|
|
53
|
+
showImportAndFormatButtons?: boolean
|
|
54
|
+
onManualEditsFileContentChanged?: (newContent: string) => void
|
|
55
|
+
}) => {
|
|
56
|
+
const editorRef = useRef<HTMLDivElement>(null)
|
|
57
|
+
const viewRef = useRef<EditorView | null>(null)
|
|
58
|
+
const ataRef = useRef<ReturnType<typeof setupTypeAcquisition> | null>(null)
|
|
59
|
+
const apiUrl = useSnippetsBaseApiUrl()
|
|
60
|
+
const codeCompletionApi = useCodeCompletionApi()
|
|
61
|
+
|
|
62
|
+
const [cursorPosition, setCursorPosition] = useState<number | null>(null)
|
|
63
|
+
const [code, setCode] = useState(initialCode)
|
|
64
|
+
|
|
65
|
+
const files = useMemo(
|
|
66
|
+
() => ({
|
|
67
|
+
"index.tsx": code,
|
|
68
|
+
"manual-edits.json": manualEditsFileContent,
|
|
69
|
+
}),
|
|
70
|
+
[code, manualEditsFileContent],
|
|
71
|
+
)
|
|
72
|
+
const [currentFile, setCurrentFile] =
|
|
73
|
+
useState<keyof typeof files>("index.tsx")
|
|
74
|
+
|
|
75
|
+
const isInitialCodeLoaded = Boolean(initialCode)
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
if (initialCode !== code) {
|
|
79
|
+
setCode(initialCode)
|
|
80
|
+
if (currentFile === "index.tsx") {
|
|
81
|
+
updateCurrentEditorContent(initialCode)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}, [isInitialCodeLoaded])
|
|
85
|
+
|
|
86
|
+
// Whenever streaming completes, reset the code to the initial code
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (!isStreaming && code !== initialCode && initialCode) {
|
|
89
|
+
console.log("Resetting code to initial code", initialCode)
|
|
90
|
+
setCode(initialCode)
|
|
91
|
+
|
|
92
|
+
// HACK: Timeout because we need to wait for the editor to mount again
|
|
93
|
+
setTimeout(() => {
|
|
94
|
+
updateCurrentEditorContent(initialCode)
|
|
95
|
+
}, 200)
|
|
96
|
+
}
|
|
97
|
+
}, [isStreaming])
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
if (!editorRef.current) return
|
|
101
|
+
|
|
102
|
+
const fsMap = new Map<string, string>()
|
|
103
|
+
Object.entries(files).forEach(([filename, content]) => {
|
|
104
|
+
fsMap.set(filename, content)
|
|
105
|
+
})
|
|
106
|
+
;(window as any).__DEBUG_CODE_EDITOR_FS_MAP = fsMap
|
|
107
|
+
|
|
108
|
+
createDefaultMapFromCDN(
|
|
109
|
+
{ target: ts.ScriptTarget.ES2022 },
|
|
110
|
+
"5.6.3",
|
|
111
|
+
true,
|
|
112
|
+
ts,
|
|
113
|
+
).then((defaultFsMap) => {
|
|
114
|
+
defaultFsMap.forEach((content, filename) => {
|
|
115
|
+
fsMap.set(filename, content)
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
const system = createSystem(fsMap)
|
|
120
|
+
const env = createVirtualTypeScriptEnvironment(system, [], ts, {
|
|
121
|
+
jsx: ts.JsxEmit.ReactJSX,
|
|
122
|
+
declaration: true,
|
|
123
|
+
allowJs: true,
|
|
124
|
+
target: ts.ScriptTarget.ES2022,
|
|
125
|
+
resolveJsonModule: true,
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
// Initialize ATA
|
|
129
|
+
const ataConfig: ATABootstrapConfig = {
|
|
130
|
+
projectName: "my-project",
|
|
131
|
+
typescript: ts,
|
|
132
|
+
logger: console,
|
|
133
|
+
fetcher: async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
134
|
+
const registryPrefixes = [
|
|
135
|
+
"https://data.jsdelivr.com/v1/package/resolve/npm/@tsci/",
|
|
136
|
+
"https://data.jsdelivr.com/v1/package/npm/@tsci/",
|
|
137
|
+
"https://cdn.jsdelivr.net/npm/@tsci/",
|
|
138
|
+
]
|
|
139
|
+
if (
|
|
140
|
+
typeof input === "string" &&
|
|
141
|
+
registryPrefixes.some((prefix) => input.startsWith(prefix))
|
|
142
|
+
) {
|
|
143
|
+
const fullPackageName = input
|
|
144
|
+
.replace(registryPrefixes[0], "")
|
|
145
|
+
.replace(registryPrefixes[1], "")
|
|
146
|
+
.replace(registryPrefixes[2], "")
|
|
147
|
+
const packageName = fullPackageName.split("/")[0].replace(/\./, "/")
|
|
148
|
+
const pathInPackage = fullPackageName.split("/").slice(1).join("/")
|
|
149
|
+
const jsdelivrPath = `${packageName}${pathInPackage ? `/${pathInPackage}` : ""}`
|
|
150
|
+
return fetch(
|
|
151
|
+
`${apiUrl}/snippets/download?jsdelivr_resolve=${input.includes("/resolve/")}&jsdelivr_path=${encodeURIComponent(jsdelivrPath)}`,
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
return fetch(input, init)
|
|
155
|
+
},
|
|
156
|
+
delegate: {
|
|
157
|
+
started: () => {
|
|
158
|
+
const manualEditsTypeDeclaration = `
|
|
159
|
+
declare module "*.json" {
|
|
160
|
+
const value: {
|
|
161
|
+
pcb_placements?: any[],
|
|
162
|
+
edit_events?: any[],
|
|
163
|
+
manual_trace_hints?: any[],
|
|
164
|
+
} | undefined;
|
|
165
|
+
export default value;
|
|
166
|
+
}
|
|
167
|
+
`
|
|
168
|
+
env.createFile("manual-edits.d.ts", manualEditsTypeDeclaration)
|
|
169
|
+
},
|
|
170
|
+
receivedFile: (code: string, path: string) => {
|
|
171
|
+
fsMap.set(path, code)
|
|
172
|
+
env.createFile(path, code)
|
|
173
|
+
if (viewRef.current) {
|
|
174
|
+
viewRef.current.dispatch({
|
|
175
|
+
changes: {
|
|
176
|
+
from: 0,
|
|
177
|
+
to: viewRef.current.state.doc.length,
|
|
178
|
+
insert: viewRef.current.state.doc.toString(),
|
|
179
|
+
},
|
|
180
|
+
selection: viewRef.current.state.selection,
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const ata = setupTypeAcquisition(ataConfig)
|
|
188
|
+
ataRef.current = ata
|
|
189
|
+
|
|
190
|
+
// Set up base extensions
|
|
191
|
+
const baseExtensions = [
|
|
192
|
+
basicSetup,
|
|
193
|
+
currentFile.endsWith(".json")
|
|
194
|
+
? json()
|
|
195
|
+
: javascript({ typescript: true, jsx: true }),
|
|
196
|
+
keymap.of([indentWithTab]),
|
|
197
|
+
EditorState.readOnly.of(readOnly),
|
|
198
|
+
EditorView.updateListener.of((update) => {
|
|
199
|
+
if (update.docChanged) {
|
|
200
|
+
const newContent = update.state.doc.toString()
|
|
201
|
+
if (newContent === files[currentFile]) return
|
|
202
|
+
|
|
203
|
+
if (currentFile === "index.tsx") {
|
|
204
|
+
setCode(newContent)
|
|
205
|
+
onCodeChange(newContent)
|
|
206
|
+
} else if (currentFile === "manual-edits.json") {
|
|
207
|
+
onManualEditsFileContentChanged?.(newContent)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (currentFile === "index.tsx") {
|
|
211
|
+
const { outputFiles } = env.languageService.getEmitOutput(
|
|
212
|
+
currentFile,
|
|
213
|
+
true,
|
|
214
|
+
)
|
|
215
|
+
const indexDts = outputFiles.find(
|
|
216
|
+
(file) => file.name === "index.d.ts",
|
|
217
|
+
)
|
|
218
|
+
if (indexDts?.text && onDtsChange) {
|
|
219
|
+
onDtsChange(indexDts.text)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (update.selectionSet) {
|
|
224
|
+
const pos = update.state.selection.main.head
|
|
225
|
+
setCursorPosition(pos)
|
|
226
|
+
}
|
|
227
|
+
}),
|
|
228
|
+
]
|
|
229
|
+
if (codeCompletionApi?.apiKey) {
|
|
230
|
+
baseExtensions.push(
|
|
231
|
+
copilotPlugin({
|
|
232
|
+
apiKey: codeCompletionApi.apiKey,
|
|
233
|
+
language: Language.TYPESCRIPT,
|
|
234
|
+
}),
|
|
235
|
+
EditorView.theme({
|
|
236
|
+
".cm-ghostText, .cm-ghostText *": {
|
|
237
|
+
opacity: "0.6",
|
|
238
|
+
filter: "grayscale(20%)",
|
|
239
|
+
cursor: "pointer",
|
|
240
|
+
},
|
|
241
|
+
".cm-ghostText:hover": {
|
|
242
|
+
background: "#eee",
|
|
243
|
+
},
|
|
244
|
+
}),
|
|
245
|
+
)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Add TypeScript-specific extensions and handlers
|
|
249
|
+
const tsExtensions =
|
|
250
|
+
currentFile.endsWith(".tsx") || currentFile.endsWith(".ts")
|
|
251
|
+
? [
|
|
252
|
+
tsFacet.of({ env, path: currentFile }),
|
|
253
|
+
tsSync(),
|
|
254
|
+
tsLinter(),
|
|
255
|
+
autocompletion({ override: [tsAutocomplete()] }),
|
|
256
|
+
tsHover(),
|
|
257
|
+
hoverTooltip((view, pos) => {
|
|
258
|
+
const line = view.state.doc.lineAt(pos)
|
|
259
|
+
const lineStart = line.from
|
|
260
|
+
const lineEnd = line.to
|
|
261
|
+
const lineText = view.state.sliceDoc(lineStart, lineEnd)
|
|
262
|
+
const matches = Array.from(
|
|
263
|
+
lineText.matchAll(TSCI_PACKAGE_PATTERN),
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
for (const match of matches) {
|
|
267
|
+
if (match.index !== undefined) {
|
|
268
|
+
const start = lineStart + match.index
|
|
269
|
+
const end = start + match[0].length
|
|
270
|
+
if (pos >= start && pos <= end) {
|
|
271
|
+
return {
|
|
272
|
+
pos: start,
|
|
273
|
+
end: end,
|
|
274
|
+
above: true,
|
|
275
|
+
create() {
|
|
276
|
+
const dom = document.createElement("div")
|
|
277
|
+
dom.textContent = "Ctrl/Cmd+Click to open snippet"
|
|
278
|
+
return { dom }
|
|
279
|
+
},
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return null
|
|
285
|
+
}),
|
|
286
|
+
EditorView.domEventHandlers({
|
|
287
|
+
click: (event, view) => {
|
|
288
|
+
if (!event.ctrlKey && !event.metaKey) return false
|
|
289
|
+
const pos = view.posAtCoords({
|
|
290
|
+
x: event.clientX,
|
|
291
|
+
y: event.clientY,
|
|
292
|
+
})
|
|
293
|
+
if (pos === null) return false
|
|
294
|
+
|
|
295
|
+
const line = view.state.doc.lineAt(pos)
|
|
296
|
+
const lineStart = line.from
|
|
297
|
+
const lineEnd = line.to
|
|
298
|
+
const lineText = view.state.sliceDoc(lineStart, lineEnd)
|
|
299
|
+
const matches = Array.from(
|
|
300
|
+
lineText.matchAll(TSCI_PACKAGE_PATTERN),
|
|
301
|
+
)
|
|
302
|
+
for (const match of matches) {
|
|
303
|
+
if (match.index !== undefined) {
|
|
304
|
+
const start = lineStart + match.index
|
|
305
|
+
const end = start + match[0].length
|
|
306
|
+
if (pos >= start && pos <= end) {
|
|
307
|
+
const importName = match[0]
|
|
308
|
+
// Handle potential dots and dashes in package names
|
|
309
|
+
const [owner, name] = importName
|
|
310
|
+
.replace("@tsci/", "")
|
|
311
|
+
.split(".")
|
|
312
|
+
window.open(`/${owner}/${name}`, "_blank")
|
|
313
|
+
return true
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return false
|
|
318
|
+
},
|
|
319
|
+
}),
|
|
320
|
+
EditorView.theme({
|
|
321
|
+
".cm-content .cm-underline": {
|
|
322
|
+
textDecoration: "underline",
|
|
323
|
+
textDecorationColor: "rgba(0, 0, 255, 0.3)",
|
|
324
|
+
cursor: "pointer",
|
|
325
|
+
},
|
|
326
|
+
}),
|
|
327
|
+
EditorView.decorations.of((view) => {
|
|
328
|
+
const decorations = []
|
|
329
|
+
for (const { from, to } of view.visibleRanges) {
|
|
330
|
+
for (let pos = from; pos < to; ) {
|
|
331
|
+
const line = view.state.doc.lineAt(pos)
|
|
332
|
+
const lineText = line.text
|
|
333
|
+
const matches = lineText.matchAll(TSCI_PACKAGE_PATTERN)
|
|
334
|
+
for (const match of matches) {
|
|
335
|
+
if (match.index !== undefined) {
|
|
336
|
+
const start = line.from + match.index
|
|
337
|
+
const end = start + match[0].length
|
|
338
|
+
decorations.push(
|
|
339
|
+
Decoration.mark({
|
|
340
|
+
class: "cm-underline",
|
|
341
|
+
}).range(start, end),
|
|
342
|
+
)
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
pos = line.to + 1
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return Decoration.set(decorations)
|
|
349
|
+
}),
|
|
350
|
+
]
|
|
351
|
+
: []
|
|
352
|
+
|
|
353
|
+
const state = EditorState.create({
|
|
354
|
+
doc: files[currentFile],
|
|
355
|
+
extensions: [...baseExtensions, ...tsExtensions],
|
|
356
|
+
})
|
|
357
|
+
|
|
358
|
+
const view = new EditorView({
|
|
359
|
+
state,
|
|
360
|
+
parent: editorRef.current,
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
viewRef.current = view
|
|
364
|
+
|
|
365
|
+
// Initial ATA run for index.tsx
|
|
366
|
+
if (currentFile === "index.tsx") {
|
|
367
|
+
ata(`${defaultImports}${code}`)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return () => {
|
|
371
|
+
view.destroy()
|
|
372
|
+
}
|
|
373
|
+
}, [!isStreaming, currentFile, code !== ""])
|
|
374
|
+
|
|
375
|
+
const updateCurrentEditorContent = (newContent: string) => {
|
|
376
|
+
if (viewRef.current) {
|
|
377
|
+
const state = viewRef.current.state
|
|
378
|
+
if (state.doc.toString() !== newContent) {
|
|
379
|
+
viewRef.current.dispatch({
|
|
380
|
+
changes: { from: 0, to: state.doc.length, insert: newContent },
|
|
381
|
+
})
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const updateEditorToMatchCurrentFile = () => {
|
|
387
|
+
const currentContent = files[currentFile] || ""
|
|
388
|
+
updateCurrentEditorContent(currentContent)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const codeImports = getImportsFromCode(code)
|
|
392
|
+
|
|
393
|
+
useEffect(() => {
|
|
394
|
+
if (ataRef.current && currentFile === "index.tsx") {
|
|
395
|
+
ataRef.current(`${defaultImports}${code}`)
|
|
396
|
+
}
|
|
397
|
+
}, [codeImports])
|
|
398
|
+
|
|
399
|
+
const handleFileChange = (filename: keyof typeof files) => {
|
|
400
|
+
setCurrentFile(filename)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const updateFileContent = (
|
|
404
|
+
filename: keyof typeof files,
|
|
405
|
+
newContent: string,
|
|
406
|
+
) => {
|
|
407
|
+
if (filename === "index.tsx") {
|
|
408
|
+
setCode(newContent)
|
|
409
|
+
onCodeChange(newContent)
|
|
410
|
+
} else if (filename === "manual-edits.json") {
|
|
411
|
+
onManualEditsFileContentChanged?.(newContent)
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (viewRef.current && currentFile === filename) {
|
|
415
|
+
viewRef.current.dispatch({
|
|
416
|
+
changes: {
|
|
417
|
+
from: 0,
|
|
418
|
+
to: viewRef.current.state.doc.length,
|
|
419
|
+
insert: newContent,
|
|
420
|
+
},
|
|
421
|
+
})
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Whenever the current file changes, updated the editor content
|
|
426
|
+
useEffect(() => {
|
|
427
|
+
updateEditorToMatchCurrentFile()
|
|
428
|
+
}, [currentFile])
|
|
429
|
+
|
|
430
|
+
// Whenever the manual edits json content changes, update the editor if
|
|
431
|
+
// it's currently viewing the manual edits file
|
|
432
|
+
useEffect(() => {
|
|
433
|
+
if (currentFile === "manual-edits.json") {
|
|
434
|
+
updateEditorToMatchCurrentFile()
|
|
435
|
+
}
|
|
436
|
+
}, [manualEditsFileContent])
|
|
437
|
+
|
|
438
|
+
if (isStreaming) {
|
|
439
|
+
return (
|
|
440
|
+
<div className="font-mono whitespace-pre-wrap text-xs">{initialCode}</div>
|
|
441
|
+
)
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
return (
|
|
445
|
+
<div className="flex flex-col h-full">
|
|
446
|
+
{showImportAndFormatButtons && (
|
|
447
|
+
<CodeEditorHeader
|
|
448
|
+
currentFile={currentFile}
|
|
449
|
+
files={files}
|
|
450
|
+
handleFileChange={handleFileChange}
|
|
451
|
+
updateFileContent={(...args) => {
|
|
452
|
+
return updateFileContent(...args)
|
|
453
|
+
}}
|
|
454
|
+
cursorPosition={cursorPosition}
|
|
455
|
+
/>
|
|
456
|
+
)}
|
|
457
|
+
<div ref={editorRef} className="flex-1 overflow-auto" />
|
|
458
|
+
</div>
|
|
459
|
+
)
|
|
460
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Button } from "@/components/ui/button"
|
|
2
|
+
import {
|
|
3
|
+
Select,
|
|
4
|
+
SelectContent,
|
|
5
|
+
SelectItem,
|
|
6
|
+
SelectTrigger,
|
|
7
|
+
SelectValue,
|
|
8
|
+
} from "@/components/ui/select"
|
|
9
|
+
import { useImportSnippetDialog } from "./dialogs/import-snippet-dialog"
|
|
10
|
+
import { useToast } from "@/hooks/use-toast"
|
|
11
|
+
import { FootprintDialog } from "./FootprintDialog"
|
|
12
|
+
import { useState } from "react"
|
|
13
|
+
import {
|
|
14
|
+
DropdownMenu,
|
|
15
|
+
DropdownMenuContent,
|
|
16
|
+
DropdownMenuItem,
|
|
17
|
+
DropdownMenuTrigger,
|
|
18
|
+
} from "./ui/dropdown-menu"
|
|
19
|
+
import { AlertTriangle } from "lucide-react"
|
|
20
|
+
import { checkIfManualEditsImported } from "@/lib/utils/checkIfManualEditsImported"
|
|
21
|
+
import { handleManualEditsImport } from "@/lib/handleManualEditsImport"
|
|
22
|
+
|
|
23
|
+
export type FileName = "index.tsx" | "manual-edits.json"
|
|
24
|
+
|
|
25
|
+
interface CodeEditorHeaderProps {
|
|
26
|
+
currentFile: FileName
|
|
27
|
+
files: Record<FileName, string>
|
|
28
|
+
handleFileChange: (filename: FileName) => void
|
|
29
|
+
updateFileContent: (filename: FileName, content: string) => void
|
|
30
|
+
cursorPosition: number | null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const CodeEditorHeader = ({
|
|
34
|
+
currentFile,
|
|
35
|
+
files,
|
|
36
|
+
handleFileChange,
|
|
37
|
+
updateFileContent,
|
|
38
|
+
cursorPosition,
|
|
39
|
+
}: CodeEditorHeaderProps) => {
|
|
40
|
+
const { Dialog: ImportSnippetDialog, openDialog: openImportDialog } =
|
|
41
|
+
useImportSnippetDialog()
|
|
42
|
+
const [footprintDialogOpen, setFootprintDialogOpen] = useState(false)
|
|
43
|
+
const { toast } = useToast()
|
|
44
|
+
|
|
45
|
+
const formatCurrentFile = () => {
|
|
46
|
+
if (!window.prettier || !window.prettierPlugins) return
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const currentContent = files[currentFile]
|
|
50
|
+
|
|
51
|
+
if (currentFile.endsWith(".json")) {
|
|
52
|
+
try {
|
|
53
|
+
const jsonObj = JSON.parse(currentContent)
|
|
54
|
+
const formattedJson = JSON.stringify(jsonObj, null, 2)
|
|
55
|
+
updateFileContent(currentFile, formattedJson)
|
|
56
|
+
} catch (jsonError) {
|
|
57
|
+
throw new Error("Invalid JSON content")
|
|
58
|
+
}
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const formattedCode = window.prettier.format(currentContent, {
|
|
63
|
+
semi: false,
|
|
64
|
+
parser: "typescript",
|
|
65
|
+
plugins: window.prettierPlugins,
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
updateFileContent(currentFile, formattedCode)
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error("Formatting error:", error)
|
|
71
|
+
toast({
|
|
72
|
+
title: "Formatting error",
|
|
73
|
+
description:
|
|
74
|
+
error instanceof Error
|
|
75
|
+
? error.message
|
|
76
|
+
: "Failed to format the code. Please check for syntax errors.",
|
|
77
|
+
variant: "destructive",
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div className="flex items-center gap-2 px-2 border-b border-gray-200">
|
|
84
|
+
<div>
|
|
85
|
+
<Select value={currentFile} onValueChange={handleFileChange}>
|
|
86
|
+
<SelectTrigger className="h-7 px-3 bg-white">
|
|
87
|
+
<SelectValue placeholder="Select file" />
|
|
88
|
+
</SelectTrigger>
|
|
89
|
+
<SelectContent>
|
|
90
|
+
{Object.keys(files).map((filename) => (
|
|
91
|
+
<SelectItem className="py-1" key={filename} value={filename}>
|
|
92
|
+
<span className="text-xs pr-1">{filename}</span>
|
|
93
|
+
</SelectItem>
|
|
94
|
+
))}
|
|
95
|
+
</SelectContent>
|
|
96
|
+
</Select>
|
|
97
|
+
</div>
|
|
98
|
+
<div className="flex items-center gap-2 px-2 py-1 ml-auto">
|
|
99
|
+
{checkIfManualEditsImported(files) && (
|
|
100
|
+
<DropdownMenu>
|
|
101
|
+
<DropdownMenuTrigger asChild>
|
|
102
|
+
<Button
|
|
103
|
+
size="sm"
|
|
104
|
+
variant="ghost"
|
|
105
|
+
className="text-red-500 hover:bg-red-50"
|
|
106
|
+
>
|
|
107
|
+
<AlertTriangle className="mr-2 h-4 w-4" />
|
|
108
|
+
Error
|
|
109
|
+
</Button>
|
|
110
|
+
</DropdownMenuTrigger>
|
|
111
|
+
<DropdownMenuContent>
|
|
112
|
+
<DropdownMenuItem
|
|
113
|
+
className="text-red-600 cursor-pointer"
|
|
114
|
+
onClick={() =>
|
|
115
|
+
handleManualEditsImport(files, updateFileContent, toast)
|
|
116
|
+
}
|
|
117
|
+
>
|
|
118
|
+
Manual edits exist but have not been imported. (Click to fix)
|
|
119
|
+
</DropdownMenuItem>
|
|
120
|
+
</DropdownMenuContent>
|
|
121
|
+
</DropdownMenu>
|
|
122
|
+
)}
|
|
123
|
+
<DropdownMenu>
|
|
124
|
+
<DropdownMenuTrigger asChild>
|
|
125
|
+
<Button size="sm" variant="ghost">
|
|
126
|
+
Insert
|
|
127
|
+
</Button>
|
|
128
|
+
</DropdownMenuTrigger>
|
|
129
|
+
<DropdownMenuContent>
|
|
130
|
+
<DropdownMenuItem onClick={() => setFootprintDialogOpen(true)}>
|
|
131
|
+
Chip
|
|
132
|
+
</DropdownMenuItem>
|
|
133
|
+
</DropdownMenuContent>
|
|
134
|
+
</DropdownMenu>
|
|
135
|
+
<Button size="sm" variant="ghost" onClick={() => openImportDialog()}>
|
|
136
|
+
Import
|
|
137
|
+
</Button>
|
|
138
|
+
<Button size="sm" variant="ghost" onClick={formatCurrentFile}>
|
|
139
|
+
Format
|
|
140
|
+
</Button>
|
|
141
|
+
</div>
|
|
142
|
+
<ImportSnippetDialog
|
|
143
|
+
onSnippetSelected={(snippet) => {
|
|
144
|
+
const newContent = `import {} from "@tsci/${snippet.owner_name}.${snippet.unscoped_name}"\n${files[currentFile]}`
|
|
145
|
+
updateFileContent(currentFile, newContent)
|
|
146
|
+
}}
|
|
147
|
+
/>
|
|
148
|
+
<FootprintDialog
|
|
149
|
+
currentFile={currentFile}
|
|
150
|
+
open={footprintDialogOpen}
|
|
151
|
+
onOpenChange={setFootprintDialogOpen}
|
|
152
|
+
updateFileContent={updateFileContent}
|
|
153
|
+
files={files}
|
|
154
|
+
cursorPosition={cursorPosition}
|
|
155
|
+
/>
|
|
156
|
+
</div>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default CodeEditorHeader
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Button } from "@/components/ui/button"
|
|
2
|
+
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
|
3
|
+
import { Input } from "@/components/ui/input"
|
|
4
|
+
import { Battery, Bot, Cpu, LayoutGrid } from "lucide-react"
|
|
5
|
+
import { useState } from "react"
|
|
6
|
+
import { useLocation } from "wouter"
|
|
7
|
+
|
|
8
|
+
export function CreateNewSnippetWithAiHero() {
|
|
9
|
+
const [inputValue, setInputValue] = useState("")
|
|
10
|
+
const [, navigate] = useLocation()
|
|
11
|
+
|
|
12
|
+
const handleSubmit = (e: React.FormEvent) => {
|
|
13
|
+
e.preventDefault()
|
|
14
|
+
if (inputValue.trim()) {
|
|
15
|
+
navigate(`/ai?initial_prompt=${encodeURIComponent(inputValue)}`)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const handleQuickPrompt = (prompt: string) => {
|
|
20
|
+
navigate(`/ai?initial_prompt=${encodeURIComponent(prompt)}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<Card className="mb-6 bg-blue-50 rounded-sm">
|
|
25
|
+
<CardHeader>
|
|
26
|
+
<CardTitle className="text-blue-600 flex items-center gap-2">
|
|
27
|
+
<span className="text-2xl">
|
|
28
|
+
<Bot size={24} />
|
|
29
|
+
</span>{" "}
|
|
30
|
+
Create a circuit using AI, type anything below!
|
|
31
|
+
</CardTitle>
|
|
32
|
+
</CardHeader>
|
|
33
|
+
<CardContent className="pb-4">
|
|
34
|
+
<form onSubmit={handleSubmit} className="mb-4">
|
|
35
|
+
<Input
|
|
36
|
+
placeholder="Create a 3x3 usb keyboard with a dial"
|
|
37
|
+
className="w-full bg-white"
|
|
38
|
+
value={inputValue}
|
|
39
|
+
onChange={(e) => setInputValue(e.target.value)}
|
|
40
|
+
/>
|
|
41
|
+
</form>
|
|
42
|
+
<div className="flex justify-between items-start sm:flex-row flex-col ">
|
|
43
|
+
<div className="flex flex-wrap w-full justify-start sm:justify-between">
|
|
44
|
+
<Button
|
|
45
|
+
variant="ghost"
|
|
46
|
+
className="flex items-center w-full sm:w-auto sm:gap-2 opacity-70 flex-row-reverse hover:bg-white hover:opacity-100 justify-between sm:justify-center"
|
|
47
|
+
onClick={() => handleQuickPrompt("Battery-powered flashlight")}
|
|
48
|
+
>
|
|
49
|
+
<Battery size={20} />
|
|
50
|
+
<span className="capitalize font-semibold">
|
|
51
|
+
Battery powered flashlight
|
|
52
|
+
</span>
|
|
53
|
+
</Button>
|
|
54
|
+
<Button
|
|
55
|
+
variant="ghost"
|
|
56
|
+
className="flex items-center w-full sm:w-auto sm:gap-2 opacity-70 flex-row-reverse hover:bg-white hover:opacity-100 justify-between sm:justify-center"
|
|
57
|
+
onClick={() => handleQuickPrompt("Motor driver module")}
|
|
58
|
+
>
|
|
59
|
+
<Cpu size={20} />
|
|
60
|
+
<span className="capitalize font-semibold">
|
|
61
|
+
Motor driver module
|
|
62
|
+
</span>
|
|
63
|
+
</Button>
|
|
64
|
+
<Button
|
|
65
|
+
variant="ghost"
|
|
66
|
+
className="flex items-center w-full sm:w-auto sm:gap-2 opacity-70 flex-row-reverse hover:bg-white hover:opacity-100 justify-between sm:justify-center"
|
|
67
|
+
onClick={() => handleQuickPrompt("NA555 Timer Chip")}
|
|
68
|
+
>
|
|
69
|
+
<LayoutGrid size={20} />
|
|
70
|
+
<span className="capitalize font-semibold">NA555 Timer Chip</span>
|
|
71
|
+
</Button>
|
|
72
|
+
</div>
|
|
73
|
+
</div>
|
|
74
|
+
</CardContent>
|
|
75
|
+
</Card>
|
|
76
|
+
)
|
|
77
|
+
}
|