@legit-sdk/react 0.1.11 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +16 -8
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
|
-
import {
|
|
3
|
+
import { openLegitFs, HistoryItem } from '@legit-sdk/core';
|
|
4
4
|
|
|
5
5
|
interface LegitContextValue {
|
|
6
|
-
legitFs: Awaited<ReturnType<typeof
|
|
6
|
+
legitFs: Awaited<ReturnType<typeof openLegitFs>> | null;
|
|
7
7
|
loading: boolean;
|
|
8
8
|
head: string | null;
|
|
9
|
+
rollback: (commitHash: string) => Promise<void>;
|
|
9
10
|
error?: Error;
|
|
10
11
|
}
|
|
11
12
|
declare const useLegitContext: () => LegitContextValue;
|
|
12
13
|
interface LegitProviderProps {
|
|
13
14
|
children: ReactNode;
|
|
15
|
+
config?: LegitConfig;
|
|
14
16
|
}
|
|
15
|
-
|
|
17
|
+
type LegitConfig = {
|
|
18
|
+
gitRoot: string;
|
|
19
|
+
initialBranch?: string;
|
|
20
|
+
serverUrl?: string;
|
|
21
|
+
publicKey?: string;
|
|
22
|
+
};
|
|
23
|
+
declare const LegitProvider: ({ children, config, }: LegitProviderProps) => react_jsx_runtime.JSX.Element;
|
|
16
24
|
|
|
17
25
|
interface UseLegitFileOptions {
|
|
18
|
-
|
|
26
|
+
initialData?: string;
|
|
19
27
|
}
|
|
20
28
|
type UseLegitFileReturn = {
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
data: string | null;
|
|
30
|
+
setData: (newText: string) => Promise<void>;
|
|
23
31
|
history: HistoryItem[];
|
|
24
32
|
getPastState: (commitHash: string) => Promise<string>;
|
|
25
33
|
loading: boolean;
|
|
26
34
|
error?: Error;
|
|
27
|
-
legitFs: Awaited<ReturnType<typeof
|
|
35
|
+
legitFs: Awaited<ReturnType<typeof openLegitFs>> | null;
|
|
28
36
|
};
|
|
29
37
|
declare function useLegitFile(path: string, options?: UseLegitFileOptions): UseLegitFileReturn;
|
|
30
38
|
|
|
31
39
|
export { LegitProvider, useLegitContext, useLegitFile };
|
|
32
|
-
export type { LegitContextValue, LegitProviderProps, UseLegitFileOptions, UseLegitFileReturn };
|
|
40
|
+
export type { LegitConfig, LegitContextValue, LegitProviderProps, UseLegitFileOptions, UseLegitFileReturn };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// @legit-sdk/react bundle (ESM)
|
|
2
2
|
// Generated by esbuild
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var O=Object.defineProperty;var s=(r,o)=>O(r,"name",{value:o,configurable:!0});import{createContext as S,useContext as z,useEffect as H,useState as L,useRef as A}from"react";import{openLegitFs as T}from"@legit-sdk/core";import V from"memfs";import{jsx as M}from"react/jsx-runtime";var I=S({legitFs:null,loading:!0,head:null,rollback:s(async()=>{},"rollback")}),x=s(()=>z(I),"useLegitContext"),K={gitRoot:"/"},_=100,J=s(({children:r,config:o=K})=>{let[t,F]=L(!0),[i,f]=L(null),u=A(null),[y,d]=L(),[p,h]=L(null);H(()=>{if(!t)return;s(async()=>{try{let e=await T({storageFs:V,gitRoot:o.gitRoot,serverUrl:o.serverUrl,publicKey:o.publicKey});e&&f(e)}catch(e){d(e)}},"init")(),F(!1)},[]),H(()=>{if(!i)return;window.legitFs=i;let l=setInterval(async()=>{let e=await i.getCurrentBranch();try{let c=await i.promises.readFile(`/.legit/branches/${e}/.legit/head`,"utf8");c!==u.current&&(u.current=c,h(c))}catch{}},_);return()=>{clearInterval(l)}},[i]);let w=s(async l=>{if(!i){console.error("No legitFs instance available");return}try{let e=await i.getCurrentBranch();await i.promises.writeFile(`/.legit/branches/${e}/.legit/head`,l,"utf8")}catch(e){d(e)}},"rollback");return M(I.Provider,{value:{legitFs:i,loading:i===null,head:p,rollback:w,error:y},children:r})},"LegitProvider");import{useEffect as U,useState as m,useRef as P,useCallback as $,useMemo as W}from"react";function q(r,o){let{legitFs:t,error:F,head:i}=x(),[f,u]=m(null),[y,d]=m([]),[p,h]=m(!0),[w,l]=m(),e=P(null),c=P(!1),C=P(!1);U(()=>{if(!t)return;let n=!1;return s(async()=>{h(!0);try{let g=await t.getCurrentBranch(),D=`/.legit/branches/${g}${r}`,N=`/.legit/branches/${g}/.legit/history`,v=await t.promises.readFile(D,"utf8"),k=await t.promises.readFile(N,"utf8").catch(()=>"");if(n)return;let E=[],b=k;b&&(E=JSON.parse(b)),e.current===v&&(e.current=null),u(v),d(E),l(void 0)}catch(g){g?.code!=="ENOENT"?l(g):(u(null),d([]))}finally{n||h(!1)}},"load")(),()=>{n=!0}},[t,r,i]);let R=$(async n=>{if(t)try{e.current=n;let a=await t.getCurrentBranch();await t.promises.writeFile(`/.legit/branches/${a}${r}`,n,"utf8"),u(n)}catch(a){throw e.current=null,l(a),a}},[t,r]);U(()=>{c.current||C.current||!p&&f===null&&t&&o?.initialData&&(C.current=!0,c.current=!0,s(async()=>{try{await R(o.initialData)}catch(a){console.error("Failed to initialize file:",a),c.current=!1}finally{C.current=!1}},"initialize")())},[p,f,t,o?.initialData,R]);let B=$(async n=>{if(!t)return"";try{let a=r.startsWith("/")?r.slice(1):r;return await t.promises.readFile(`/.legit/commits/${n.slice(0,2)}/${n.slice(2)}/${a}`,"utf8")}catch{return""}},[t,r]);return{data:f,setData:R,history:W(()=>y,[y]),getPastState:B,loading:p,error:w??F,legitFs:t}}s(q,"useLegitFile");export{J as LegitProvider,x as useLegitContext,q as useLegitFile};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@legit-sdk/react",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "React wrapper for @legit-sdk/core",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"vitest": "^3.1.1"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@legit-sdk/core": "0.1
|
|
38
|
+
"@legit-sdk/core": "0.2.1"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public"
|