all repos — snow-editor @ 13d855e25e8510a57608b0ce77f62cbeed372d0e

small and cozy markdown, and orgmode editor

src/hooks/useEditLock.js (view raw)

 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
 152
 153
 154
 155
 156
import { useCallback, useEffect, useRef, useState } from 'react';
import {
  acquireEditLock,
  ApiError,
  refreshEditLock,
  releaseEditLock,
} from '../lib/api.js';
import { getOrCreateClientId } from '../lib/clientId.js';

const REFRESH_INTERVAL_MS = 30 * 1000;

export function useEditLock(editToken, enabled = true) {
  const [lockState, setLockState] = useState({
    status: 'idle',
    lockToken: null,
    lockExpiresAt: null,
    blockedExpiresAt: null,
  });
  const clientIdRef = useRef(getOrCreateClientId());
  const lockTokenRef = useRef(null);

  const acquire = useCallback(async () => {
    if (!editToken || !enabled) return;
    setLockState((s) => ({ ...s, status: 'acquiring' }));

    try {
      const result = await acquireEditLock(editToken, clientIdRef.current);
      lockTokenRef.current = result.lockToken;
      setLockState({
        status: 'held',
        lockToken: result.lockToken,
        lockExpiresAt: result.expiresAt,
        blockedExpiresAt: null,
      });
      return { locked: true, lockToken: result.lockToken };
    } catch (error) {
      if (error instanceof ApiError && error.status === 423) {
        setLockState({
          status: 'blocked',
          lockToken: null,
          lockExpiresAt: null,
          blockedExpiresAt: error.payload?.lockExpiresAt ?? null,
        });
        return { locked: false, lockExpiresAt: error.payload?.lockExpiresAt };
      }
      setLockState({
        status: 'error',
        lockToken: null,
        lockExpiresAt: null,
        blockedExpiresAt: null,
      });
      throw error;
    }
  }, [editToken, enabled]);

  const refresh = useCallback(async () => {
    if (!editToken || !lockTokenRef.current) return false;
    try {
      const result = await refreshEditLock(
        editToken,
        clientIdRef.current,
        lockTokenRef.current,
      );
      lockTokenRef.current = result.lockToken;
      setLockState((s) => ({
        ...s,
        status: 'held',
        lockExpiresAt: result.expiresAt,
      }));
      return true;
    } catch {
      lockTokenRef.current = null;
      setLockState({
        status: 'lost',
        lockToken: null,
        lockExpiresAt: null,
        blockedExpiresAt: null,
      });
      return false;
    }
  }, [editToken]);

  const release = useCallback(async () => {
    if (!editToken || !lockTokenRef.current) return;
    const token = lockTokenRef.current;
    lockTokenRef.current = null;
    try {
      await releaseEditLock(editToken, clientIdRef.current, token);
    } catch {
      /* best effort */
    }
    setLockState({
      status: 'released',
      lockToken: null,
      lockExpiresAt: null,
      blockedExpiresAt: null,
    });
  }, [editToken]);

  useEffect(() => {
    if (!enabled || lockState.status !== 'held' || !editToken) return;

    const interval = window.setInterval(() => {
      refresh();
    }, REFRESH_INTERVAL_MS);

    return () => window.clearInterval(interval);
  }, [enabled, editToken, lockState.status, refresh]);

  useEffect(() => {
    if (!enabled || !editToken) return;

    // pagehide is far more reliable than beforeunload (which mobile browsers
    // often skip and which disables the back/forward cache).
    const onPageHide = () => {
      if (!lockTokenRef.current) return;
      const body = JSON.stringify({
        clientId: clientIdRef.current,
        lockToken: lockTokenRef.current,
      });
      const url = `${import.meta.env.VITE_API_BASE ?? ''}/api/documents/edit/${encodeURIComponent(editToken)}/lock`;
      fetch(url, {
        method: 'DELETE',
        body,
        keepalive: true,
        headers: { 'Content-Type': 'application/json' },
      }).catch(() => {});
      lockTokenRef.current = null;
    };

    // Restored from the back/forward cache: the lock was released on the way
    // out, so grab it again to keep editing seamless.
    const onPageShow = (event) => {
      if (event.persisted && !lockTokenRef.current) {
        acquire().catch(() => {});
      }
    };

    window.addEventListener('pagehide', onPageHide);
    window.addEventListener('pageshow', onPageShow);
    return () => {
      window.removeEventListener('pagehide', onPageHide);
      window.removeEventListener('pageshow', onPageShow);
    };
  }, [enabled, editToken, acquire]);

  return {
    clientId: clientIdRef.current,
    lockState,
    acquire,
    refresh,
    release,
    hasLock: lockState.status === 'held' && !!lockTokenRef.current,
    lockToken: lockTokenRef.current,
  };
}