Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext useDocLoader

From Leeroopedia
Knowledge Sources
Domains UI, Document_Management
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete tool for loading and managing document content state in the React frontend, handling document selection, content loading, URL synchronization, and remote update detection.

Description

The useDocLoader hook is a React custom hook that manages the document loading lifecycle in the OpenContext UI. It uses a `useReducer` pattern for state management with the following state:

  • selectedDoc: Currently selected document metadata (or null).
  • docContent: Current document content string.
  • isLoadingContent: Loading state flag.
  • diffGate: Remote update detection gate (prevents overwriting remote changes).
  • spaceNewDocs: New documents in the current space (for refresh hints).

The hook receives a rich set of props from the parent component including the `api` abstraction (Tauri or HTTP), folder cache hooks, and callback functions.

Key behaviors:

  • Document selection: When a document is selected, the hook dispatches `LOAD_DOC_START`, fetches content via `api.getDocContent()`, and dispatches `LOAD_DOC_SUCCESS` with the result.
  • URL synchronization: Uses `saveLastUrl`/`restoreLastUrl`/`clearLastUrl` from `useDataIntegrity` to persist the current document path in the URL.
  • Remote update detection: The `diffGate` state tracks when the server-side content differs from the local content, prompting the user before overwriting.
  • Error handling: Not-found errors (`isNotFoundError`) are handled gracefully, clearing the selection.

Usage

  • Document Lifecycle: Used by the main App component to manage document editing state.
  • Content editing: Works with the TiptapMarkdown editor component for content display and editing.
  • URL routing: Enables deep-linking to specific documents via URL hash.

Code Reference

Source Location

  • Repository: OpenContext
  • File: src/ui/src/hooks/useDocLoader.js
  • File (data integrity):** src/ui/src/hooks/useDataIntegrity.js

Signature

export function useDocLoader({
  api,
  foldersLoaded,
  expandedFolders,
  setExpandedFolders,
  folderDocs,
  refreshFolder,
  hasPendingChanges,
  beforeLoadDoc,
  onAfterLoadDoc,
  setError,
  onAlert,
}) {
  const [state, dispatch] = useReducer(reducer, initialState);
  // ... implementation ...

  return {
    selectedDoc,
    docContent,
    isLoadingContent,
    diffGate,
    spaceNewDocs,
    // ... action functions
  };
}

Import

import { useDocLoader } from './hooks/useDocLoader';

I/O Contract

State (reducer)

Name Type Description
selectedDoc null Currently selected document metadata
docContent string Current document content (markdown)
isLoadingContent boolean Whether content is loading
diffGate null Remote update detection state
spaceNewDocs null New documents hint

Reducer Actions

Action Description
LOAD_DOC_START Set isLoadingContent = true
LOAD_DOC_SUCCESS Set selectedDoc, docContent, isLoadingContent = false
LOAD_DOC_FINISH Set isLoadingContent = false
SET_SELECTED_DOC Set selectedDoc
SET_DOC_CONTENT Set docContent
SET_DIFF_GATE Set diffGate
CLEAR_SELECTION Reset selectedDoc and docContent

Usage Examples

Basic Usage

import { useDocLoader } from './hooks/useDocLoader';

function DocumentEditor({ api, foldersLoaded, expandedFolders, ... }) {
  const {
    selectedDoc,
    docContent,
    isLoadingContent,
    diffGate,
  } = useDocLoader({
    api,
    foldersLoaded,
    expandedFolders,
    setExpandedFolders,
    folderDocs,
    refreshFolder,
    hasPendingChanges,
    setError,
    onAlert,
  });

  if (isLoadingContent) return <LoadingSpinner />;
  if (!selectedDoc) return <EmptyState />;

  return (
    <TiptapMarkdown
      content={docContent}
      onChange={(newContent) => { /* save logic */ }}
    />
  );
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment