API Reference
Complete API reference for @legit-sdk/react.
Provider
LegitProvider
Wraps your application and initializes the LegitFS SDK instance. Provides the filesystem context to all child components.
Props
interface LegitProviderProps {
children: ReactNode;
config?: LegitConfig;
}Config Options
type LegitConfig = {
gitRoot: string; // Root path of the Git repository
initialBranch?: string; // Initial branch name (default: 'anonymous')
serverUrl?: string; // Sync server URL
publicKey?: string; // Public key for authentication
};Example
<LegitProvider
config={{
gitRoot: '/',
initialBranch: 'main',
serverUrl: 'https://hub.legitcontrol.com',
publicKey: process.env.NEXT_PUBLIC_LEGIT_PUBLIC_KEY,
}}
>
<YourApp />
</LegitProvider>Behavior
- Initializes
legitFsexactly once on mount - Polls repository HEAD every 100ms to detect changes
- Provides shared
legitFsinstance through React context - Exposes global
loadinganderrorstates
Hooks
useLegitFile(path, options?)
Hook for file operations with automatic version control.
Parameters
- path (string): File path relative to the branch root (e.g.,
'/document.txt') - options (optional): Configuration options
interface UseLegitFileOptions {
initialData?: string; // Auto-create file with this content if missing
}Returns
type UseLegitFileReturn = {
data: string | null; // Current file content
setData: (newText: string) => Promise<void>; // Save and commit
history: HistoryItem[]; // Commit history
getPastState: (commitHash: string) => Promise<string>; // Read file at commit
loading: boolean; // Loading state
error?: Error; // Error state
legitFs: LegitFs | null; // Direct access to legitFs instance
};Behavior
- Auto-initialization: If
initialDatais provided and the file doesn’t exist, it will be created automatically on first mount - Reactive updates: Automatically reloads when HEAD changes (detected by provider polling)
- Automatic commits: Each
setData()call creates a Git commit with a descriptive message - History tracking: Fetches commit history from
.legit/branches/{branch}/.legit/history - Error handling: Gracefully handles missing files (returns
nullfor data, empty array for history)
useLegitContext()
Access the provider context directly for advanced use cases.
Returns
interface LegitContextValue {
legitFs: Awaited<ReturnType<typeof openLegitFs>> | null;
loading: boolean;
head: string | null; // Current HEAD commit SHA
rollback: (commitHash: string) => Promise<void>;
error?: Error;
}Use Cases
- Access
legitFsdirectly for advanced operations - Monitor HEAD changes
- Implement custom rollback logic
- Access branch management functions
- Use authentication API (
legitFs.auth) - Use sync API (
legitFs.sync)
Sync with React wrapper
The sync api is currently in experimental mode. If you want to read more about it follow the instructions here.
Types
HistoryItem
type HistoryItem = {
oid: string; // Commit SHA
message: string; // Commit message
parent: string[]; // Parent commit SHAs
author: {
name: string;
email: string;
timestamp: number;
};
};LegitConfig
type LegitConfig = {
gitRoot: string;
initialBranch?: string;
serverUrl?: string;
publicKey?: string;
};UseLegitFileOptions
interface UseLegitFileOptions {
initialData?: string;
}UseLegitFileReturn
type UseLegitFileReturn = {
data: string | null;
setData: (newText: string) => Promise<void>;
history: HistoryItem[];
getPastState: (commitHash: string) => Promise<string>;
loading: boolean;
error?: Error;
legitFs: Awaited<ReturnType<typeof openLegitFs>> | null;
};LegitContextValue
interface LegitContextValue {
legitFs: Awaited<ReturnType<typeof openLegitFs>> | null;
loading: boolean;
head: string | null;
rollback: (commitHash: string) => Promise<void>;
error?: Error;
}Lifecycle
Provider Lifecycle
- Mount: Initializes
legitFsinstance - Polling: Starts polling HEAD every 100ms
- Updates: When HEAD changes, triggers re-renders in consuming hooks
- Unmount: Cleans up polling interval
Hook Lifecycle
- Mount: Reads file and history, initializes state
- Auto-init: If file doesn’t exist and
initialDatais provided, creates file - Updates: Reloads when HEAD changes (detected by provider)
- Save:
setData()writes file and creates commit, updates local state optimistically
Error Handling
- Missing files return
nullfordata(not an error) - Network errors are caught and exposed via
errorproperty - File system errors are propagated through the
errorproperty - Auto-initialization failures are logged but don’t crash the component
Performance Considerations
- Polling interval: Default 100ms (configurable via provider)
- Optimistic updates:
setData()updates local state immediately - Memoized history: History array is memoized to prevent unnecessary re-renders
- Cancellation: File reads are cancelled on unmount to prevent race conditions
See Also
- Working with One File - Using
useLegitFilehook and how the abstraction works - Accessing Legit Context - Using
useLegitContextfor advanced operations
Last updated on