Spaces:
Running
Running
File size: 5,001 Bytes
a8d792f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
"use client"
import type React from "react"
import { Book, Code2, Terminal, Copy, Check } from "lucide-react"
import { Button } from "@/components/ui/button"
import { useState } from "react"
const CodeBlock = ({ children }: { children: React.ReactNode }) => {
const [copied, setCopied] = useState(false)
const copyToClipboard = async (text: string) => {
try {
await navigator.clipboard.writeText(text)
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch (err) {
console.error("Failed to copy:", err)
}
}
const codeText = typeof children === "string" ? children : children?.toString() || ""
return (
<div className="bg-muted/50 dark:bg-black/40 border border-border dark:border-white/10 rounded-md overflow-hidden my-4 relative">
<pre className="p-4 text-sm overflow-x-auto">
<code>{children}</code>
</pre>
<Button
variant="outline"
size="sm"
onClick={() => copyToClipboard(codeText)}
className="absolute top-2 right-2 h-7 w-7 p-0 transition-all"
>
{copied ? <Check className="w-3 h-3" /> : <Copy className="w-3 h-3" />}
</Button>
</div>
)
}
export function DocsSection() {
return (
<div className="font-mono">
<div className="mb-6">
<h2 className="text-2xl font-bold text-primary tracking-wider mb-2 uppercase flex items-center gap-3">
<Book className="w-6 h-6" />
Docs
</h2>
<p className="text-sm text-muted-foreground">A quick guide to get started with the @lerobot/web API.</p>
</div>
<div className="bg-muted/40 dark:bg-black/30 border-l-4 border-cyan-500 dark:border-accent-cyan p-6 md:p-8 rounded-r-lg space-y-8">
{/* Getting Started */}
<div>
<h3 className="text-xl font-bold text-cyan-600 dark:text-accent-cyan tracking-wider uppercase flex items-center gap-2">
<Terminal className="w-5 h-5" />
Getting Started
</h3>
<p className="text-muted-foreground text-sm mt-2 mb-4">
First, import the API from the library. Then you can call its methods to find and control your robot.
</p>
<CodeBlock>
{`import { lerobot } from '@lerobot/web';
async function connectToRobot() {
const { result } = await lerobot.findPort({
onMessage: (message) => console.log('Status:', message),
});
const robots = await result;
console.log('Found robots:', robots);
}`}
</CodeBlock>
</div>
{/* API Reference */}
<div>
<h3 className="text-xl font-bold text-cyan-600 dark:text-accent-cyan tracking-wider uppercase flex items-center gap-2">
<Code2 className="w-5 h-5" />
API Reference
</h3>
<div className="space-y-6 mt-4">
{/* findPort */}
<div>
<h4 className="font-bold text-primary">lerobot.findPort(options)</h4>
<p className="text-muted-foreground text-sm mt-1">Scans for available robot connections via WebSerial.</p>
<CodeBlock>
{`// Options
{
robotConfigs?: RobotConfig[], // Optional: Saved configs to try reconnecting
onMessage?: (msg: string) => void // Callback for status updates
}
// Returns
{
result: Promise<RobotConnection[]> // A promise that resolves with found robots
}`}
</CodeBlock>
</div>
{/* calibrate */}
<div>
<h4 className="font-bold text-primary">lerobot.calibrate(robot, options)</h4>
<p className="text-muted-foreground text-sm mt-1">Starts the calibration process for a specific robot.</p>
<CodeBlock>
{`// Parameters
robot: RobotConnection // The robot instance to calibrate
// Options
{
onLiveUpdate: (data: LiveCalibrationData) => void, // Callback for real-time joint data
onProgress: (msg: string) => void // Callback for status messages
}
// Returns
{
result: Promise<WebCalibrationResults>, // A promise that resolves with final calibration
stop: () => void // Function to stop the calibration recording
}`}
</CodeBlock>
</div>
{/* teleoperate */}
<div>
<h4 className="font-bold text-primary">lerobot.teleoperate(robot, options)</h4>
<p className="text-muted-foreground text-sm mt-1">Initializes the teleoperation interface for a robot.</p>
<CodeBlock>
{`// Parameters
robot: RobotConnection,
calibrationData: WebCalibrationResults
// Options
{
onStateUpdate: (state: TeleoperationState) => void // Callback for real-time state updates
}
// Returns
{
start: () => void,
stop: () => void,
updateKeyState: (key: string, pressed: boolean) => void,
moveMotor: (motorName: string, position: number) => void
}`}
</CodeBlock>
</div>
</div>
</div>
</div>
</div>
)
}
|