-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: open in chat button in doc examples #4996
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
Changes from all commits
2008b49
1bec998
e963480
58c353a
869ccb1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||||||||
| "use server"; | ||||||||||||||||||
|
|
||||||||||||||||||
| import {SandpackFiles} from "@codesandbox/sandpack-react/types"; | ||||||||||||||||||
|
|
||||||||||||||||||
| import {parseDependencies} from "@/components/docs/components/code-demo/parse-dependencies"; | ||||||||||||||||||
|
|
||||||||||||||||||
| const importReact = 'import React from "react";'; | ||||||||||||||||||
|
|
||||||||||||||||||
| export const openInChat = async ({title, files}: {title?: string; files: SandpackFiles}) => { | ||||||||||||||||||
| try { | ||||||||||||||||||
| // assumes one file for now | ||||||||||||||||||
| let content = files["/App.jsx"]; | ||||||||||||||||||
|
|
||||||||||||||||||
| if (!content || typeof content !== "string") { | ||||||||||||||||||
| return { | ||||||||||||||||||
| error: "Content is not a string", | ||||||||||||||||||
| data: null, | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check if the file content includes 'React' import statements, if not, add it | ||||||||||||||||||
| if ( | ||||||||||||||||||
| content.includes("React.") && | ||||||||||||||||||
| !content.includes("from 'react'") && | ||||||||||||||||||
| !content.includes('from "react"') | ||||||||||||||||||
| ) { | ||||||||||||||||||
| content = `${importReact}\n${content}\n`; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| const dependencies = parseDependencies(content); | ||||||||||||||||||
|
|
||||||||||||||||||
| const response = await fetch(`${process.env.CHAT_API_URL}/import`, { | ||||||||||||||||||
| method: "POST", | ||||||||||||||||||
| headers: { | ||||||||||||||||||
| "Content-Type": "application/json", | ||||||||||||||||||
| Authorization: `Bearer ${process.env.IMPORT_API_KEY}`, | ||||||||||||||||||
| }, | ||||||||||||||||||
| body: JSON.stringify({ | ||||||||||||||||||
| title, | ||||||||||||||||||
| content, | ||||||||||||||||||
| dependencies, | ||||||||||||||||||
| }), | ||||||||||||||||||
| }); | ||||||||||||||||||
|
|
||||||||||||||||||
| const result = await response.json(); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (result.error || !result.path) { | ||||||||||||||||||
| return { | ||||||||||||||||||
| error: result.error ?? "Unknown error", | ||||||||||||||||||
| data: null, | ||||||||||||||||||
| }; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| return {error: null, data: `${process.env.CHAT_URL}${result.path}`}; | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
| return {error: error, data: null}; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+55
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling in catch block The error message may not be a string, which could cause issues when returned. - return {error: error, data: null};
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
+ console.error("Error in openInChat:", error);
+ return {error: errorMessage, data: null};📝 Committable suggestion
Suggested change
|
||||||||||||||||||
| }; | ||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| const packageRegex = /(?:from|import)\s+(?:.*\s+from\s+)?['"]([^'"]+)['"]/g; | ||
|
|
||
| export const parseDependencies = (content: string) => { | ||
| const dependencies: {name: string; version: string}[] = []; | ||
|
|
||
| content.match(packageRegex)?.forEach((match) => { | ||
| if (match.includes("@heroui")) { | ||
| return; | ||
| } | ||
|
|
||
| if (match.includes("./") || match.includes("../")) { | ||
| return; | ||
| } | ||
|
|
||
| const packageName = match.match(/['"]([^'"]+)['"]/)?.[1]; | ||
|
|
||
| if (!packageName) { | ||
| return; | ||
| } | ||
|
|
||
| dependencies.push({ | ||
| name: packageName, | ||
| version: fixedVersions[packageName] || "latest", | ||
| }); | ||
| }); | ||
|
|
||
| return dependencies; | ||
| }; | ||
|
|
||
| const fixedVersions = { | ||
| "@internationalized/date": "3.7.0", | ||
| "@react-aria/i18n": "3.12.5", | ||
| }; |
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.
🛠️ Refactor suggestion
Add error handling for API request
The fetch request lacks timeout and network error handling.
📝 Committable suggestion