@inertiajs/react 2.0.13 → 2.0.15
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/LICENSE +0 -0
- package/dist/index.esm.js +910 -1
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +934 -1
- package/dist/index.js.map +3 -3
- package/dist/server.esm.js +5 -1
- package/dist/server.esm.js.map +1 -1
- package/dist/server.js +35 -1
- package/dist/server.js.map +2 -2
- package/package.json +11 -10
- package/readme.md +0 -0
- package/types/App.d.ts +1 -0
- package/types/HeadContext.d.ts +1 -0
- package/types/Link.d.ts +3 -2
- package/types/PageContext.d.ts +1 -0
- package/types/useForm.d.ts +2 -1
package/dist/index.js
CHANGED
|
@@ -1,2 +1,935 @@
|
|
|
1
|
-
var $e=Object.create;var te=Object.defineProperty;var We=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var qe=Object.getPrototypeOf,Je=Object.prototype.hasOwnProperty;var ze=(t,r)=>{for(var o in r)te(t,o,{get:r[o],enumerable:!0})},be=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of _e(r))!Je.call(t,n)&&n!==o&&te(t,n,{get:()=>r[n],enumerable:!(a=We(r,n))||a.enumerable});return t};var Qe=(t,r,o)=>(o=t!=null?$e(qe(t)):{},be(r||!t||!t.__esModule?te(o,"default",{value:t,enumerable:!0}):o,t)),Ye=t=>be(te({},"__esModule",{value:!0}),t);var Ze={};ze(Ze,{Deferred:()=>Me,Head:()=>Le,Link:()=>Ke,WhenVisible:()=>Ne,createInertiaApp:()=>ge,router:()=>Pe,useForm:()=>Fe,usePage:()=>Y,usePoll:()=>Se,usePrefetch:()=>Te,useRemember:()=>U});module.exports=Ye(Ze);var je=require("@inertiajs/core");var oe=require("@inertiajs/core"),Ee=require("react");var Q=require("@inertiajs/core"),C=require("react");var Ce=require("react"),De=(0,Ce.createContext)(void 0);De.displayName="InertiaHeadContext";var J=De;var Re=require("react"),xe=(0,Re.createContext)(void 0);xe.displayName="InertiaPageContext";var z=xe;var me=!0,Ie=!1,Ae=async()=>{me=!1};function re({children:t,initialPage:r,initialComponent:o,resolveComponent:a,titleCallback:n,onHeadUpdate:m}){let[d,g]=(0,C.useState)({component:o||null,page:r,key:null}),c=(0,C.useMemo)(()=>(0,Q.createHeadManager)(typeof window>"u",n||(l=>l),m||(()=>{})),[]);if(Ie||(Q.router.init({initialPage:r,resolveComponent:a,swapComponent:async l=>Ae(l)}),Ie=!0),(0,C.useEffect)(()=>{Ae=async({component:l,page:e,preserveState:f})=>{if(me){me=!1;return}g(s=>({component:l,page:e,key:f?s.key:Date.now()}))},Q.router.on("navigate",()=>c.forceUpdate())},[]),!d.component)return(0,C.createElement)(J.Provider,{value:c},(0,C.createElement)(z.Provider,{value:d.page},null));let P=t||(({Component:l,props:e,key:f})=>{let s=(0,C.createElement)(l,{key:f,...e});return typeof l.layout=="function"?l.layout(s):Array.isArray(l.layout)?l.layout.concat(s).reverse().reduce((F,S)=>(0,C.createElement)(S,{children:F,...e})):s});return(0,C.createElement)(J.Provider,{value:c},(0,C.createElement)(z.Provider,{value:d.page},P({Component:d.component,key:d.key,props:d.page.props})))}re.displayName="Inertia";async function ge({id:t="app",resolve:r,setup:o,title:a,progress:n={},page:m,render:d}){let g=typeof window>"u",c=g?null:document.getElementById(t),P=m||JSON.parse(c.dataset.page),l=s=>Promise.resolve(r(s)).then(F=>F.default||F),e=[],f=await Promise.all([l(P.component),oe.router.decryptHistory().catch(()=>{})]).then(([s])=>o({el:c,App:re,props:{initialPage:P,initialComponent:s,resolveComponent:l,titleCallback:a,onHeadUpdate:g?F=>e=F:null}}));if(!g&&n&&(0,oe.setupProgress)(n),g){let s=await d((0,Ee.createElement)("div",{id:t,"data-page":JSON.stringify(P)},f));return{head:e,body:s}}}var H=require("react");var ke=require("react");function Y(){let t=(0,ke.useContext)(z);if(!t)throw new Error("usePage must be used within the Inertia component");return t}var we=t=>(t=new URL(t.href),t.hash="",t),Ge=(t,r)=>we(t).href===we(r).href,Oe=({children:t,data:r,fallback:o})=>{if(!r)throw new Error("`<Deferred>` requires a `data` prop to be a string or array of strings");let[a,n]=(0,H.useState)(!1),m=Y().props,d=(0,H.useMemo)(()=>Array.isArray(r)?r:[r],[r]);return(0,H.useEffect)(()=>{let g=Pe.on("start",c=>{let P=c.detail.visit.only.length>0||c.detail.visit.except.length>0,l=c.detail.visit.only.find(e=>d.includes(e));Ge(c.detail.visit.url,window.location)&&(!P||l)&&n(!1)});return()=>{g()}},[]),(0,H.useEffect)(()=>{n(d.every(g=>m[g]!==void 0))},[m,d]),a?t:o};Oe.displayName="InertiaDeferred";var Me=Oe;var E=Qe(require("react"),1);var Xe=function({children:t,title:r}){let o=(0,E.useContext)(J),a=(0,E.useMemo)(()=>o.createProvider(),[o]);(0,E.useEffect)(()=>(a.reconnect(),a.update(l(t)),()=>{a.disconnect()}),[a,t,r]);function n(e){return["area","base","br","col","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"].indexOf(e.type)>-1}function m(e){let f=Object.keys(e.props).reduce((s,F)=>{if(["head-key","children","dangerouslySetInnerHTML"].includes(F))return s;let S=e.props[F];return S===""?s+` ${F}`:s+` ${F}="${S}"`},"");return`<${e.type}${f}>`}function d(e){return typeof e.props.children=="string"?e.props.children:e.props.children.reduce((f,s)=>f+g(s),"")}function g(e){let f=m(e);return e.props.children&&(f+=d(e)),e.props.dangerouslySetInnerHTML&&(f+=e.props.dangerouslySetInnerHTML.__html),n(e)||(f+=`</${e.type}>`),f}function c(e){return E.default.cloneElement(e,{inertia:e.props["head-key"]!==void 0?e.props["head-key"]:""})}function P(e){return g(c(e))}function l(e){let f=E.default.Children.toArray(e).filter(s=>s).map(s=>P(s));return r&&!f.find(s=>s.startsWith("<title"))&&f.push(`<title inertia>${r}</title>`),f}return null},Le=Xe;var x=require("@inertiajs/core"),v=require("react"),k=()=>{},He=(0,v.forwardRef)(({children:t,as:r="a",data:o={},href:a,method:n="get",preserveScroll:m=!1,preserveState:d=null,replace:g=!1,only:c=[],except:P=[],headers:l={},queryStringArrayFormat:e="brackets",async:f=!1,onClick:s=k,onCancelToken:F=k,onBefore:S=k,onStart:M=k,onProgress:L=k,onFinish:ae=k,onCancel:G=k,onSuccess:ie=k,onError:$=k,prefetch:b=!1,cacheFor:B=0,...I},pe)=>{let[X,W]=(0,v.useState)(0),_=(0,v.useRef)(null);r=r.toLowerCase(),n=typeof a=="object"?a.method:n.toLowerCase();let[ue,ce]=(0,x.mergeDataIntoQueryString)(n,typeof a=="object"?a.url:a||"",o,e),V=ue;o=ce;let w={data:o,method:n,preserveScroll:m,preserveState:d??n!=="get",replace:g,only:c,except:P,headers:l,async:f},Z={...w,onCancelToken:F,onBefore:S,onStart(u){W(y=>y+1),M(u)},onProgress:L,onFinish(u){W(y=>y-1),ae(u)},onCancel:G,onSuccess:ie,onError:$},q=()=>{x.router.prefetch(V,w,{cacheFor:le})},A=(0,v.useMemo)(()=>b===!0?["hover"]:b===!1?[]:Array.isArray(b)?b:[b],Array.isArray(b)?b:[b]),le=(0,v.useMemo)(()=>B!==0?B:A.length===1&&A[0]==="click"?0:3e4,[B,A]);(0,v.useEffect)(()=>()=>{clearTimeout(_.current)},[]),(0,v.useEffect)(()=>{A.includes("mount")&&setTimeout(()=>q())},A);let ee={onClick:u=>{s(u),(0,x.shouldIntercept)(u)&&(u.preventDefault(),x.router.visit(V,Z))}},de={onMouseEnter:()=>{_.current=window.setTimeout(()=>{q()},75)},onMouseLeave:()=>{clearTimeout(_.current)},onClick:ee.onClick},fe={onMouseDown:u=>{(0,x.shouldIntercept)(u)&&(u.preventDefault(),q())},onMouseUp:u=>{u.preventDefault(),x.router.visit(V,Z)},onClick:u=>{s(u),(0,x.shouldIntercept)(u)&&u.preventDefault()}};return n!=="get"&&(r="button"),(0,v.createElement)(r,{...I,...{a:{href:V},button:{type:"button"}}[r]||{},ref:pe,...A.includes("hover")?de:A.includes("click")?fe:ee,"data-loading":X>0?"":void 0},t)});He.displayName="InertiaLink";var Ke=He;var he=require("@inertiajs/core"),K=require("es-toolkit"),O=require("es-toolkit/compat"),p=require("react");var ye=require("@inertiajs/core"),ne=require("react");function U(t,r){let[o,a]=(0,ne.useState)(()=>{let n=ye.router.restore(r);return n!==void 0?n:t});return(0,ne.useEffect)(()=>{ye.router.remember(o,r)},[o,r]),[o,a]}function Fe(t,r){let o=(0,p.useRef)(null),a=typeof t=="string"?t:null,[n,m]=(0,p.useState)((typeof t=="string"?r:t)||{}),d=(0,p.useRef)(null),g=(0,p.useRef)(null),[c,P]=a?U(n,`${a}:data`):(0,p.useState)(n),[l,e]=a?U({},`${a}:errors`):(0,p.useState)({}),[f,s]=(0,p.useState)(!1),[F,S]=(0,p.useState)(!1),[M,L]=(0,p.useState)(null),[ae,G]=(0,p.useState)(!1),[ie,$]=(0,p.useState)(!1),b=(0,p.useRef)(i=>i),B=(0,p.useMemo)(()=>!(0,K.isEqual)(c,n),[c,n]);(0,p.useEffect)(()=>(o.current=!0,()=>{o.current=!1}),[]);let I=(0,p.useCallback)((...i)=>{let u=typeof i[0]=="object",y=u?i[0].method:i[0],R=u?i[0].url:i[1],h=(u?i[1]:i[2])??{},ve={...h,onCancelToken:T=>{if(d.current=T,h.onCancelToken)return h.onCancelToken(T)},onBefore:T=>{if(G(!1),$(!1),clearTimeout(g.current),h.onBefore)return h.onBefore(T)},onStart:T=>{if(S(!0),h.onStart)return h.onStart(T)},onProgress:T=>{if(L(T),h.onProgress)return h.onProgress(T)},onSuccess:T=>{if(o.current&&(S(!1),L(null),e({}),s(!1),G(!0),$(!0),m((0,K.cloneDeep)(c)),g.current=setTimeout(()=>{o.current&&$(!1)},2e3)),h.onSuccess)return h.onSuccess(T)},onError:T=>{if(o.current&&(S(!1),L(null),e(T),s(!0)),h.onError)return h.onError(T)},onCancel:()=>{if(o.current&&(S(!1),L(null)),h.onCancel)return h.onCancel()},onFinish:T=>{if(o.current&&(S(!1),L(null)),d.current=null,h.onFinish)return h.onFinish(T)}};y==="delete"?he.router.delete(R,{...ve,data:b.current(c)}):he.router[y](R,b.current(c),ve)},[c,e,b]),pe=(0,p.useCallback)((i,u)=>{P(typeof i=="string"?y=>(0,O.set)((0,K.cloneDeep)(y),i,u):typeof i=="function"?y=>i(y):i)},[P]),[X,W]=(0,p.useState)(!1),_=(0,p.useCallback)((i,u)=>{typeof i>"u"?(m(c),W(!0)):m(y=>typeof i=="string"?(0,O.set)((0,K.cloneDeep)(y),i,u):Object.assign((0,K.cloneDeep)(y),i))},[c,m]);(0,p.useLayoutEffect)(()=>{X&&(B&&m(c),W(!1))},[X]);let ue=(0,p.useCallback)((...i)=>{i.length===0?P(n):P(u=>i.filter(y=>(0,O.has)(n,y)).reduce((y,R)=>(0,O.set)(y,R,(0,O.get)(n,R)),{...u}))},[P,n]),ce=(0,p.useCallback)((i,u)=>{e(y=>{let R={...y,...typeof i=="string"?{[i]:u}:i};return s(Object.keys(R).length>0),R})},[e,s]),V=(0,p.useCallback)((...i)=>{e(u=>{let y=Object.keys(u).reduce((R,h)=>({...R,...i.length>0&&!i.includes(h)?{[h]:u[h]}:{}}),{});return s(Object.keys(y).length>0),y})},[e,s]),w=i=>(u,y)=>{I(i,u,y)},Z=(0,p.useCallback)(w("get"),[I]),q=(0,p.useCallback)(w("post"),[I]),A=(0,p.useCallback)(w("put"),[I]),le=(0,p.useCallback)(w("patch"),[I]),ee=(0,p.useCallback)(w("delete"),[I]),de=(0,p.useCallback)(()=>{d.current&&d.current.cancel()},[]),fe=(0,p.useCallback)(i=>{b.current=i},[]);return{data:c,setData:pe,isDirty:B,errors:l,hasErrors:f,processing:F,progress:M,wasSuccessful:ae,recentlySuccessful:ie,transform:fe,setDefaults:_,reset:ue,setError:ce,clearErrors:V,submit:I,get:Z,post:q,put:A,patch:le,delete:ee,cancel:de}}var Be=require("@inertiajs/core"),se=require("react");function Se(t,r={},o={keepAlive:!1,autoStart:!0}){let a=(0,se.useRef)(Be.router.poll(t,r,{...o,autoStart:!1}));return(0,se.useEffect)(()=>((o.autoStart??!0)&&a.current.start(),()=>a.current.stop()),[]),{stop:a.current.stop,start:a.current.start}}var N=require("@inertiajs/core"),j=require("react");function Te(t={}){let r=typeof window>"u"?null:N.router.getCached(window.location.pathname,t),o=typeof window>"u"?null:N.router.getPrefetching(window.location.pathname,t),[a,n]=(0,j.useState)(r?.staleTimestamp||null),[m,d]=(0,j.useState)(o!==null),[g,c]=(0,j.useState)(r!==null);return(0,j.useEffect)(()=>{let P=N.router.on("prefetching",e=>{e.detail.visit.url.pathname===window.location.pathname&&d(!0)}),l=N.router.on("prefetched",e=>{e.detail.visit.url.pathname===window.location.pathname&&(d(!1),c(!0),n(e.detail.fetchedAt))});return()=>{l(),P()}},[]),{lastUpdatedAt:a,isPrefetching:m,isPrefetched:g,flush:()=>N.router.flush(window.location.pathname,t)}}var Ve=require("@inertiajs/core"),D=require("react"),Ue=({children:t,data:r,params:o,buffer:a,as:n,always:m,fallback:d})=>{m=m??!1,n=n??"div",d=d??null;let[g,c]=(0,D.useState)(!1),P=(0,D.useRef)(!1),l=(0,D.useRef)(!1),e=(0,D.useRef)(null),f=(0,D.useCallback)(()=>{if(r)return{only:Array.isArray(r)?r:[r]};if(!o)throw new Error("You must provide either a `data` or `params` prop.");return o},[o,r]);return(0,D.useEffect)(()=>{if(!e.current)return;let s=new IntersectionObserver(F=>{if(!F[0].isIntersecting||(!m&&P.current&&s.disconnect(),l.current))return;P.current=!0,l.current=!0;let S=f();Ve.router.reload({...S,onStart:M=>{l.current=!0,S.onStart?.(M)},onFinish:M=>{c(!0),l.current=!1,S.onFinish?.(M),m||s.disconnect()}})},{rootMargin:`${a||0}px`});return s.observe(e.current),()=>{s.disconnect()}},[e,f,a]),m||!g?(0,D.createElement)(n,{props:null,ref:e},g?t:d):g?t:null};Ue.displayName="InertiaWhenVisible";var Ne=Ue;var Pe=je.router;
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/index.ts
|
|
30
|
+
var index_exports = {};
|
|
31
|
+
__export(index_exports, {
|
|
32
|
+
Deferred: () => Deferred_default,
|
|
33
|
+
Head: () => Head_default,
|
|
34
|
+
Link: () => Link_default,
|
|
35
|
+
WhenVisible: () => WhenVisible_default,
|
|
36
|
+
createInertiaApp: () => createInertiaApp,
|
|
37
|
+
router: () => router3,
|
|
38
|
+
useForm: () => useForm,
|
|
39
|
+
usePage: () => usePage,
|
|
40
|
+
usePoll: () => usePoll,
|
|
41
|
+
usePrefetch: () => usePrefetch,
|
|
42
|
+
useRemember: () => useRemember
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(index_exports);
|
|
45
|
+
var import_core9 = require("@inertiajs/core");
|
|
46
|
+
|
|
47
|
+
// src/createInertiaApp.ts
|
|
48
|
+
var import_core2 = require("@inertiajs/core");
|
|
49
|
+
var import_react4 = require("react");
|
|
50
|
+
|
|
51
|
+
// src/App.ts
|
|
52
|
+
var import_core = require("@inertiajs/core");
|
|
53
|
+
var import_react3 = require("react");
|
|
54
|
+
|
|
55
|
+
// src/HeadContext.ts
|
|
56
|
+
var import_react = require("react");
|
|
57
|
+
var headContext = (0, import_react.createContext)(void 0);
|
|
58
|
+
headContext.displayName = "InertiaHeadContext";
|
|
59
|
+
var HeadContext_default = headContext;
|
|
60
|
+
|
|
61
|
+
// src/PageContext.ts
|
|
62
|
+
var import_react2 = require("react");
|
|
63
|
+
var pageContext = (0, import_react2.createContext)(void 0);
|
|
64
|
+
pageContext.displayName = "InertiaPageContext";
|
|
65
|
+
var PageContext_default = pageContext;
|
|
66
|
+
|
|
67
|
+
// src/App.ts
|
|
68
|
+
var currentIsInitialPage = true;
|
|
69
|
+
var routerIsInitialized = false;
|
|
70
|
+
var swapComponent = async () => {
|
|
71
|
+
currentIsInitialPage = false;
|
|
72
|
+
};
|
|
73
|
+
function App({
|
|
74
|
+
children,
|
|
75
|
+
initialPage,
|
|
76
|
+
initialComponent,
|
|
77
|
+
resolveComponent,
|
|
78
|
+
titleCallback,
|
|
79
|
+
onHeadUpdate
|
|
80
|
+
}) {
|
|
81
|
+
const [current, setCurrent] = (0, import_react3.useState)({
|
|
82
|
+
component: initialComponent || null,
|
|
83
|
+
page: initialPage,
|
|
84
|
+
key: null
|
|
85
|
+
});
|
|
86
|
+
const headManager = (0, import_react3.useMemo)(() => {
|
|
87
|
+
return (0, import_core.createHeadManager)(
|
|
88
|
+
typeof window === "undefined",
|
|
89
|
+
titleCallback || ((title) => title),
|
|
90
|
+
onHeadUpdate || (() => {
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
}, []);
|
|
94
|
+
if (!routerIsInitialized) {
|
|
95
|
+
import_core.router.init({
|
|
96
|
+
initialPage,
|
|
97
|
+
resolveComponent,
|
|
98
|
+
swapComponent: async (args) => swapComponent(args)
|
|
99
|
+
});
|
|
100
|
+
routerIsInitialized = true;
|
|
101
|
+
}
|
|
102
|
+
(0, import_react3.useEffect)(() => {
|
|
103
|
+
swapComponent = async ({ component, page, preserveState }) => {
|
|
104
|
+
if (currentIsInitialPage) {
|
|
105
|
+
currentIsInitialPage = false;
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
setCurrent((current2) => ({
|
|
109
|
+
component,
|
|
110
|
+
page,
|
|
111
|
+
key: preserveState ? current2.key : Date.now()
|
|
112
|
+
}));
|
|
113
|
+
};
|
|
114
|
+
import_core.router.on("navigate", () => headManager.forceUpdate());
|
|
115
|
+
}, []);
|
|
116
|
+
if (!current.component) {
|
|
117
|
+
return (0, import_react3.createElement)(
|
|
118
|
+
HeadContext_default.Provider,
|
|
119
|
+
{ value: headManager },
|
|
120
|
+
(0, import_react3.createElement)(PageContext_default.Provider, { value: current.page }, null)
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
const renderChildren = children || (({ Component, props, key }) => {
|
|
124
|
+
const child = (0, import_react3.createElement)(Component, { key, ...props });
|
|
125
|
+
if (typeof Component.layout === "function") {
|
|
126
|
+
return Component.layout(child);
|
|
127
|
+
}
|
|
128
|
+
if (Array.isArray(Component.layout)) {
|
|
129
|
+
return Component.layout.concat(child).reverse().reduce((children2, Layout) => (0, import_react3.createElement)(Layout, { children: children2, ...props }));
|
|
130
|
+
}
|
|
131
|
+
return child;
|
|
132
|
+
});
|
|
133
|
+
return (0, import_react3.createElement)(
|
|
134
|
+
HeadContext_default.Provider,
|
|
135
|
+
{ value: headManager },
|
|
136
|
+
(0, import_react3.createElement)(
|
|
137
|
+
PageContext_default.Provider,
|
|
138
|
+
{ value: current.page },
|
|
139
|
+
renderChildren({
|
|
140
|
+
Component: current.component,
|
|
141
|
+
key: current.key,
|
|
142
|
+
props: current.page.props
|
|
143
|
+
})
|
|
144
|
+
)
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
App.displayName = "Inertia";
|
|
148
|
+
|
|
149
|
+
// src/createInertiaApp.ts
|
|
150
|
+
async function createInertiaApp({
|
|
151
|
+
id = "app",
|
|
152
|
+
resolve,
|
|
153
|
+
setup,
|
|
154
|
+
title,
|
|
155
|
+
progress = {},
|
|
156
|
+
page,
|
|
157
|
+
render
|
|
158
|
+
}) {
|
|
159
|
+
const isServer = typeof window === "undefined";
|
|
160
|
+
const el = isServer ? null : document.getElementById(id);
|
|
161
|
+
const initialPage = page || JSON.parse(el.dataset.page);
|
|
162
|
+
const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module2) => module2.default || module2);
|
|
163
|
+
let head = [];
|
|
164
|
+
const reactApp = await Promise.all([
|
|
165
|
+
resolveComponent(initialPage.component),
|
|
166
|
+
import_core2.router.decryptHistory().catch(() => {
|
|
167
|
+
})
|
|
168
|
+
]).then(([initialComponent]) => {
|
|
169
|
+
return setup({
|
|
170
|
+
// @ts-expect-error
|
|
171
|
+
el,
|
|
172
|
+
App,
|
|
173
|
+
props: {
|
|
174
|
+
initialPage,
|
|
175
|
+
initialComponent,
|
|
176
|
+
resolveComponent,
|
|
177
|
+
titleCallback: title,
|
|
178
|
+
onHeadUpdate: isServer ? (elements) => head = elements : null
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
if (!isServer && progress) {
|
|
183
|
+
(0, import_core2.setupProgress)(progress);
|
|
184
|
+
}
|
|
185
|
+
if (isServer) {
|
|
186
|
+
const body = await render(
|
|
187
|
+
(0, import_react4.createElement)(
|
|
188
|
+
"div",
|
|
189
|
+
{
|
|
190
|
+
id,
|
|
191
|
+
"data-page": JSON.stringify(initialPage)
|
|
192
|
+
},
|
|
193
|
+
// @ts-expect-error
|
|
194
|
+
reactApp
|
|
195
|
+
)
|
|
196
|
+
);
|
|
197
|
+
return { head, body };
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/Deferred.ts
|
|
202
|
+
var import_react6 = require("react");
|
|
203
|
+
|
|
204
|
+
// src/usePage.ts
|
|
205
|
+
var import_react5 = require("react");
|
|
206
|
+
function usePage() {
|
|
207
|
+
const page = (0, import_react5.useContext)(PageContext_default);
|
|
208
|
+
if (!page) {
|
|
209
|
+
throw new Error("usePage must be used within the Inertia component");
|
|
210
|
+
}
|
|
211
|
+
return page;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// src/Deferred.ts
|
|
215
|
+
var urlWithoutHash = (url) => {
|
|
216
|
+
url = new URL(url.href);
|
|
217
|
+
url.hash = "";
|
|
218
|
+
return url;
|
|
219
|
+
};
|
|
220
|
+
var isSameUrlWithoutHash = (url1, url2) => {
|
|
221
|
+
return urlWithoutHash(url1).href === urlWithoutHash(url2).href;
|
|
222
|
+
};
|
|
223
|
+
var Deferred = ({ children, data, fallback }) => {
|
|
224
|
+
if (!data) {
|
|
225
|
+
throw new Error("`<Deferred>` requires a `data` prop to be a string or array of strings");
|
|
226
|
+
}
|
|
227
|
+
const [loaded, setLoaded] = (0, import_react6.useState)(false);
|
|
228
|
+
const pageProps = usePage().props;
|
|
229
|
+
const keys = (0, import_react6.useMemo)(() => Array.isArray(data) ? data : [data], [data]);
|
|
230
|
+
(0, import_react6.useEffect)(() => {
|
|
231
|
+
const removeListener = router3.on("start", (e) => {
|
|
232
|
+
const isPartialVisit = e.detail.visit.only.length > 0 || e.detail.visit.except.length > 0;
|
|
233
|
+
const isReloadingKey = e.detail.visit.only.find((key) => keys.includes(key));
|
|
234
|
+
if (isSameUrlWithoutHash(e.detail.visit.url, window.location) && (!isPartialVisit || isReloadingKey)) {
|
|
235
|
+
setLoaded(false);
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
return () => {
|
|
239
|
+
removeListener();
|
|
240
|
+
};
|
|
241
|
+
}, []);
|
|
242
|
+
(0, import_react6.useEffect)(() => {
|
|
243
|
+
setLoaded(keys.every((key) => pageProps[key] !== void 0));
|
|
244
|
+
}, [pageProps, keys]);
|
|
245
|
+
return loaded ? children : fallback;
|
|
246
|
+
};
|
|
247
|
+
Deferred.displayName = "InertiaDeferred";
|
|
248
|
+
var Deferred_default = Deferred;
|
|
249
|
+
|
|
250
|
+
// src/Head.ts
|
|
251
|
+
var import_es_toolkit = require("es-toolkit");
|
|
252
|
+
var import_react7 = __toESM(require("react"), 1);
|
|
253
|
+
var Head = function({ children, title }) {
|
|
254
|
+
const headManager = (0, import_react7.useContext)(HeadContext_default);
|
|
255
|
+
const provider = (0, import_react7.useMemo)(() => headManager.createProvider(), [headManager]);
|
|
256
|
+
const isServer = typeof window === "undefined";
|
|
257
|
+
(0, import_react7.useEffect)(() => {
|
|
258
|
+
provider.reconnect();
|
|
259
|
+
provider.update(renderNodes(children));
|
|
260
|
+
return () => {
|
|
261
|
+
provider.disconnect();
|
|
262
|
+
};
|
|
263
|
+
}, [provider, children, title]);
|
|
264
|
+
function isUnaryTag(node) {
|
|
265
|
+
return [
|
|
266
|
+
"area",
|
|
267
|
+
"base",
|
|
268
|
+
"br",
|
|
269
|
+
"col",
|
|
270
|
+
"embed",
|
|
271
|
+
"hr",
|
|
272
|
+
"img",
|
|
273
|
+
"input",
|
|
274
|
+
"keygen",
|
|
275
|
+
"link",
|
|
276
|
+
"meta",
|
|
277
|
+
"param",
|
|
278
|
+
"source",
|
|
279
|
+
"track",
|
|
280
|
+
"wbr"
|
|
281
|
+
].indexOf(node.type) > -1;
|
|
282
|
+
}
|
|
283
|
+
function renderTagStart(node) {
|
|
284
|
+
const attrs = Object.keys(node.props).reduce((carry, name) => {
|
|
285
|
+
if (["head-key", "children", "dangerouslySetInnerHTML"].includes(name)) {
|
|
286
|
+
return carry;
|
|
287
|
+
}
|
|
288
|
+
const value = node.props[name];
|
|
289
|
+
if (value === "") {
|
|
290
|
+
return carry + ` ${name}`;
|
|
291
|
+
} else {
|
|
292
|
+
return carry + ` ${name}="${(0, import_es_toolkit.escape)(value)}"`;
|
|
293
|
+
}
|
|
294
|
+
}, "");
|
|
295
|
+
return `<${node.type}${attrs}>`;
|
|
296
|
+
}
|
|
297
|
+
function renderTagChildren(node) {
|
|
298
|
+
return typeof node.props.children === "string" ? node.props.children : node.props.children.reduce((html, child) => html + renderTag(child), "");
|
|
299
|
+
}
|
|
300
|
+
function renderTag(node) {
|
|
301
|
+
let html = renderTagStart(node);
|
|
302
|
+
if (node.props.children) {
|
|
303
|
+
html += renderTagChildren(node);
|
|
304
|
+
}
|
|
305
|
+
if (node.props.dangerouslySetInnerHTML) {
|
|
306
|
+
html += node.props.dangerouslySetInnerHTML.__html;
|
|
307
|
+
}
|
|
308
|
+
if (!isUnaryTag(node)) {
|
|
309
|
+
html += `</${node.type}>`;
|
|
310
|
+
}
|
|
311
|
+
return html;
|
|
312
|
+
}
|
|
313
|
+
function ensureNodeHasInertiaProp(node) {
|
|
314
|
+
return import_react7.default.cloneElement(node, {
|
|
315
|
+
inertia: node.props["head-key"] !== void 0 ? node.props["head-key"] : ""
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
function renderNode(node) {
|
|
319
|
+
return renderTag(ensureNodeHasInertiaProp(node));
|
|
320
|
+
}
|
|
321
|
+
function renderNodes(nodes) {
|
|
322
|
+
const computed = import_react7.default.Children.toArray(nodes).filter((node) => node).map((node) => renderNode(node));
|
|
323
|
+
if (title && !computed.find((tag) => tag.startsWith("<title"))) {
|
|
324
|
+
computed.push(`<title inertia>${title}</title>`);
|
|
325
|
+
}
|
|
326
|
+
return computed;
|
|
327
|
+
}
|
|
328
|
+
if (isServer) {
|
|
329
|
+
provider.update(renderNodes(children));
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
};
|
|
333
|
+
var Head_default = Head;
|
|
334
|
+
|
|
335
|
+
// src/Link.ts
|
|
336
|
+
var import_core3 = require("@inertiajs/core");
|
|
337
|
+
var import_react8 = require("react");
|
|
338
|
+
var noop = () => void 0;
|
|
339
|
+
var Link = (0, import_react8.forwardRef)(
|
|
340
|
+
({
|
|
341
|
+
children,
|
|
342
|
+
as = "a",
|
|
343
|
+
data = {},
|
|
344
|
+
href,
|
|
345
|
+
method = "get",
|
|
346
|
+
preserveScroll = false,
|
|
347
|
+
preserveState = null,
|
|
348
|
+
replace = false,
|
|
349
|
+
only = [],
|
|
350
|
+
except = [],
|
|
351
|
+
headers = {},
|
|
352
|
+
queryStringArrayFormat = "brackets",
|
|
353
|
+
async = false,
|
|
354
|
+
onClick = noop,
|
|
355
|
+
onCancelToken = noop,
|
|
356
|
+
onBefore = noop,
|
|
357
|
+
onStart = noop,
|
|
358
|
+
onProgress = noop,
|
|
359
|
+
onFinish = noop,
|
|
360
|
+
onCancel = noop,
|
|
361
|
+
onSuccess = noop,
|
|
362
|
+
onError = noop,
|
|
363
|
+
prefetch = false,
|
|
364
|
+
cacheFor = 0,
|
|
365
|
+
...props
|
|
366
|
+
}, ref) => {
|
|
367
|
+
const [inFlightCount, setInFlightCount] = (0, import_react8.useState)(0);
|
|
368
|
+
const hoverTimeout = (0, import_react8.useRef)(null);
|
|
369
|
+
const _method = (0, import_react8.useMemo)(() => {
|
|
370
|
+
return typeof href === "object" ? href.method : method.toLowerCase();
|
|
371
|
+
}, [href, method]);
|
|
372
|
+
const _as = (0, import_react8.useMemo)(() => {
|
|
373
|
+
as = as.toLowerCase();
|
|
374
|
+
return _method !== "get" ? "button" : as;
|
|
375
|
+
}, [as, _method]);
|
|
376
|
+
const mergeDataArray = (0, import_react8.useMemo)(
|
|
377
|
+
() => (0, import_core3.mergeDataIntoQueryString)(
|
|
378
|
+
_method,
|
|
379
|
+
typeof href === "object" ? href.url : href || "",
|
|
380
|
+
data,
|
|
381
|
+
queryStringArrayFormat
|
|
382
|
+
),
|
|
383
|
+
[href, _method, data, queryStringArrayFormat]
|
|
384
|
+
);
|
|
385
|
+
const url = (0, import_react8.useMemo)(() => mergeDataArray[0], [mergeDataArray]);
|
|
386
|
+
const _data = (0, import_react8.useMemo)(() => mergeDataArray[1], [mergeDataArray]);
|
|
387
|
+
const baseParams = (0, import_react8.useMemo)(
|
|
388
|
+
() => ({
|
|
389
|
+
data: _data,
|
|
390
|
+
method: _method,
|
|
391
|
+
preserveScroll,
|
|
392
|
+
preserveState: preserveState ?? _method !== "get",
|
|
393
|
+
replace,
|
|
394
|
+
only,
|
|
395
|
+
except,
|
|
396
|
+
headers,
|
|
397
|
+
async
|
|
398
|
+
}),
|
|
399
|
+
[_data, _method, preserveScroll, preserveState, replace, only, except, headers, async]
|
|
400
|
+
);
|
|
401
|
+
const visitParams = (0, import_react8.useMemo)(
|
|
402
|
+
() => ({
|
|
403
|
+
...baseParams,
|
|
404
|
+
onCancelToken,
|
|
405
|
+
onBefore,
|
|
406
|
+
onStart(event) {
|
|
407
|
+
setInFlightCount((count) => count + 1);
|
|
408
|
+
onStart(event);
|
|
409
|
+
},
|
|
410
|
+
onProgress,
|
|
411
|
+
onFinish(event) {
|
|
412
|
+
setInFlightCount((count) => count - 1);
|
|
413
|
+
onFinish(event);
|
|
414
|
+
},
|
|
415
|
+
onCancel,
|
|
416
|
+
onSuccess,
|
|
417
|
+
onError
|
|
418
|
+
}),
|
|
419
|
+
[baseParams, onCancelToken, onBefore, onStart, onProgress, onFinish, onCancel, onSuccess, onError]
|
|
420
|
+
);
|
|
421
|
+
const doPrefetch = () => {
|
|
422
|
+
import_core3.router.prefetch(url, baseParams, { cacheFor: cacheForValue });
|
|
423
|
+
};
|
|
424
|
+
const prefetchModes = (0, import_react8.useMemo)(
|
|
425
|
+
() => {
|
|
426
|
+
if (prefetch === true) {
|
|
427
|
+
return ["hover"];
|
|
428
|
+
}
|
|
429
|
+
if (prefetch === false) {
|
|
430
|
+
return [];
|
|
431
|
+
}
|
|
432
|
+
if (Array.isArray(prefetch)) {
|
|
433
|
+
return prefetch;
|
|
434
|
+
}
|
|
435
|
+
return [prefetch];
|
|
436
|
+
},
|
|
437
|
+
Array.isArray(prefetch) ? prefetch : [prefetch]
|
|
438
|
+
);
|
|
439
|
+
const cacheForValue = (0, import_react8.useMemo)(() => {
|
|
440
|
+
if (cacheFor !== 0) {
|
|
441
|
+
return cacheFor;
|
|
442
|
+
}
|
|
443
|
+
if (prefetchModes.length === 1 && prefetchModes[0] === "click") {
|
|
444
|
+
return 0;
|
|
445
|
+
}
|
|
446
|
+
return 3e4;
|
|
447
|
+
}, [cacheFor, prefetchModes]);
|
|
448
|
+
(0, import_react8.useEffect)(() => {
|
|
449
|
+
return () => {
|
|
450
|
+
clearTimeout(hoverTimeout.current);
|
|
451
|
+
};
|
|
452
|
+
}, []);
|
|
453
|
+
(0, import_react8.useEffect)(() => {
|
|
454
|
+
if (prefetchModes.includes("mount")) {
|
|
455
|
+
setTimeout(() => doPrefetch());
|
|
456
|
+
}
|
|
457
|
+
}, prefetchModes);
|
|
458
|
+
const regularEvents = {
|
|
459
|
+
onClick: (event) => {
|
|
460
|
+
onClick(event);
|
|
461
|
+
if ((0, import_core3.shouldIntercept)(event)) {
|
|
462
|
+
event.preventDefault();
|
|
463
|
+
import_core3.router.visit(url, visitParams);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
const prefetchHoverEvents = {
|
|
468
|
+
onMouseEnter: () => {
|
|
469
|
+
hoverTimeout.current = window.setTimeout(() => {
|
|
470
|
+
doPrefetch();
|
|
471
|
+
}, 75);
|
|
472
|
+
},
|
|
473
|
+
onMouseLeave: () => {
|
|
474
|
+
clearTimeout(hoverTimeout.current);
|
|
475
|
+
},
|
|
476
|
+
onClick: regularEvents.onClick
|
|
477
|
+
};
|
|
478
|
+
const prefetchClickEvents = {
|
|
479
|
+
onMouseDown: (event) => {
|
|
480
|
+
if ((0, import_core3.shouldIntercept)(event)) {
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
doPrefetch();
|
|
483
|
+
}
|
|
484
|
+
},
|
|
485
|
+
onMouseUp: (event) => {
|
|
486
|
+
event.preventDefault();
|
|
487
|
+
import_core3.router.visit(url, visitParams);
|
|
488
|
+
},
|
|
489
|
+
onClick: (event) => {
|
|
490
|
+
onClick(event);
|
|
491
|
+
if ((0, import_core3.shouldIntercept)(event)) {
|
|
492
|
+
event.preventDefault();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
const elProps = (0, import_react8.useMemo)(
|
|
497
|
+
() => ({
|
|
498
|
+
a: { href: url },
|
|
499
|
+
button: { type: "button" }
|
|
500
|
+
}),
|
|
501
|
+
[url]
|
|
502
|
+
);
|
|
503
|
+
return (0, import_react8.createElement)(
|
|
504
|
+
_as,
|
|
505
|
+
{
|
|
506
|
+
...props,
|
|
507
|
+
...elProps[_as] || {},
|
|
508
|
+
ref,
|
|
509
|
+
...(() => {
|
|
510
|
+
if (prefetchModes.includes("hover")) {
|
|
511
|
+
return prefetchHoverEvents;
|
|
512
|
+
}
|
|
513
|
+
if (prefetchModes.includes("click")) {
|
|
514
|
+
return prefetchClickEvents;
|
|
515
|
+
}
|
|
516
|
+
return regularEvents;
|
|
517
|
+
})(),
|
|
518
|
+
"data-loading": inFlightCount > 0 ? "" : void 0
|
|
519
|
+
},
|
|
520
|
+
children
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
);
|
|
524
|
+
Link.displayName = "InertiaLink";
|
|
525
|
+
var Link_default = Link;
|
|
526
|
+
|
|
527
|
+
// src/useForm.ts
|
|
528
|
+
var import_core5 = require("@inertiajs/core");
|
|
529
|
+
var import_es_toolkit2 = require("es-toolkit");
|
|
530
|
+
var import_compat = require("es-toolkit/compat");
|
|
531
|
+
var import_react10 = require("react");
|
|
532
|
+
|
|
533
|
+
// src/useRemember.ts
|
|
534
|
+
var import_core4 = require("@inertiajs/core");
|
|
535
|
+
var import_react9 = require("react");
|
|
536
|
+
function useRemember(initialState, key) {
|
|
537
|
+
const [state, setState] = (0, import_react9.useState)(() => {
|
|
538
|
+
const restored = import_core4.router.restore(key);
|
|
539
|
+
return restored !== void 0 ? restored : initialState;
|
|
540
|
+
});
|
|
541
|
+
(0, import_react9.useEffect)(() => {
|
|
542
|
+
import_core4.router.remember(state, key);
|
|
543
|
+
}, [state, key]);
|
|
544
|
+
return [state, setState];
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/useForm.ts
|
|
548
|
+
function useForm(rememberKeyOrInitialValues, maybeInitialValues) {
|
|
549
|
+
const isMounted = (0, import_react10.useRef)(null);
|
|
550
|
+
const rememberKey = typeof rememberKeyOrInitialValues === "string" ? rememberKeyOrInitialValues : null;
|
|
551
|
+
const [defaults, setDefaults] = (0, import_react10.useState)(
|
|
552
|
+
(typeof rememberKeyOrInitialValues === "string" ? maybeInitialValues : rememberKeyOrInitialValues) || {}
|
|
553
|
+
);
|
|
554
|
+
const cancelToken = (0, import_react10.useRef)(null);
|
|
555
|
+
const recentlySuccessfulTimeoutId = (0, import_react10.useRef)(null);
|
|
556
|
+
const [data, setData] = rememberKey ? useRemember(defaults, `${rememberKey}:data`) : (0, import_react10.useState)(defaults);
|
|
557
|
+
const [errors, setErrors] = rememberKey ? useRemember({}, `${rememberKey}:errors`) : (0, import_react10.useState)({});
|
|
558
|
+
const [hasErrors, setHasErrors] = (0, import_react10.useState)(false);
|
|
559
|
+
const [processing, setProcessing] = (0, import_react10.useState)(false);
|
|
560
|
+
const [progress, setProgress] = (0, import_react10.useState)(null);
|
|
561
|
+
const [wasSuccessful, setWasSuccessful] = (0, import_react10.useState)(false);
|
|
562
|
+
const [recentlySuccessful, setRecentlySuccessful] = (0, import_react10.useState)(false);
|
|
563
|
+
const transform = (0, import_react10.useRef)((data2) => data2);
|
|
564
|
+
const isDirty = (0, import_react10.useMemo)(() => !(0, import_es_toolkit2.isEqual)(data, defaults), [data, defaults]);
|
|
565
|
+
(0, import_react10.useEffect)(() => {
|
|
566
|
+
isMounted.current = true;
|
|
567
|
+
return () => {
|
|
568
|
+
isMounted.current = false;
|
|
569
|
+
};
|
|
570
|
+
}, []);
|
|
571
|
+
const submit = (0, import_react10.useCallback)(
|
|
572
|
+
(...args) => {
|
|
573
|
+
const objectPassed = typeof args[0] === "object";
|
|
574
|
+
const method = objectPassed ? args[0].method : args[0];
|
|
575
|
+
const url = objectPassed ? args[0].url : args[1];
|
|
576
|
+
const options = (objectPassed ? args[1] : args[2]) ?? {};
|
|
577
|
+
const _options = {
|
|
578
|
+
...options,
|
|
579
|
+
onCancelToken: (token) => {
|
|
580
|
+
cancelToken.current = token;
|
|
581
|
+
if (options.onCancelToken) {
|
|
582
|
+
return options.onCancelToken(token);
|
|
583
|
+
}
|
|
584
|
+
},
|
|
585
|
+
onBefore: (visit) => {
|
|
586
|
+
setWasSuccessful(false);
|
|
587
|
+
setRecentlySuccessful(false);
|
|
588
|
+
clearTimeout(recentlySuccessfulTimeoutId.current);
|
|
589
|
+
if (options.onBefore) {
|
|
590
|
+
return options.onBefore(visit);
|
|
591
|
+
}
|
|
592
|
+
},
|
|
593
|
+
onStart: (visit) => {
|
|
594
|
+
setProcessing(true);
|
|
595
|
+
if (options.onStart) {
|
|
596
|
+
return options.onStart(visit);
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
onProgress: (event) => {
|
|
600
|
+
setProgress(event);
|
|
601
|
+
if (options.onProgress) {
|
|
602
|
+
return options.onProgress(event);
|
|
603
|
+
}
|
|
604
|
+
},
|
|
605
|
+
onSuccess: (page) => {
|
|
606
|
+
if (isMounted.current) {
|
|
607
|
+
setProcessing(false);
|
|
608
|
+
setProgress(null);
|
|
609
|
+
setErrors({});
|
|
610
|
+
setHasErrors(false);
|
|
611
|
+
setWasSuccessful(true);
|
|
612
|
+
setRecentlySuccessful(true);
|
|
613
|
+
setDefaults((0, import_es_toolkit2.cloneDeep)(data));
|
|
614
|
+
recentlySuccessfulTimeoutId.current = setTimeout(() => {
|
|
615
|
+
if (isMounted.current) {
|
|
616
|
+
setRecentlySuccessful(false);
|
|
617
|
+
}
|
|
618
|
+
}, 2e3);
|
|
619
|
+
}
|
|
620
|
+
if (options.onSuccess) {
|
|
621
|
+
return options.onSuccess(page);
|
|
622
|
+
}
|
|
623
|
+
},
|
|
624
|
+
onError: (errors2) => {
|
|
625
|
+
if (isMounted.current) {
|
|
626
|
+
setProcessing(false);
|
|
627
|
+
setProgress(null);
|
|
628
|
+
setErrors(errors2);
|
|
629
|
+
setHasErrors(true);
|
|
630
|
+
}
|
|
631
|
+
if (options.onError) {
|
|
632
|
+
return options.onError(errors2);
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
onCancel: () => {
|
|
636
|
+
if (isMounted.current) {
|
|
637
|
+
setProcessing(false);
|
|
638
|
+
setProgress(null);
|
|
639
|
+
}
|
|
640
|
+
if (options.onCancel) {
|
|
641
|
+
return options.onCancel();
|
|
642
|
+
}
|
|
643
|
+
},
|
|
644
|
+
onFinish: (visit) => {
|
|
645
|
+
if (isMounted.current) {
|
|
646
|
+
setProcessing(false);
|
|
647
|
+
setProgress(null);
|
|
648
|
+
}
|
|
649
|
+
cancelToken.current = null;
|
|
650
|
+
if (options.onFinish) {
|
|
651
|
+
return options.onFinish(visit);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
if (method === "delete") {
|
|
656
|
+
import_core5.router.delete(url, { ..._options, data: transform.current(data) });
|
|
657
|
+
} else {
|
|
658
|
+
import_core5.router[method](url, transform.current(data), _options);
|
|
659
|
+
}
|
|
660
|
+
},
|
|
661
|
+
[data, setErrors, transform]
|
|
662
|
+
);
|
|
663
|
+
const setDataFunction = (0, import_react10.useCallback)(
|
|
664
|
+
(keyOrData, maybeValue) => {
|
|
665
|
+
if (typeof keyOrData === "string") {
|
|
666
|
+
setData((data2) => (0, import_compat.set)((0, import_es_toolkit2.cloneDeep)(data2), keyOrData, maybeValue));
|
|
667
|
+
} else if (typeof keyOrData === "function") {
|
|
668
|
+
setData((data2) => keyOrData(data2));
|
|
669
|
+
} else {
|
|
670
|
+
setData(keyOrData);
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
[setData]
|
|
674
|
+
);
|
|
675
|
+
const [dataAsDefaults, setDataAsDefaults] = (0, import_react10.useState)(false);
|
|
676
|
+
const setDefaultsFunction = (0, import_react10.useCallback)(
|
|
677
|
+
(fieldOrFields, maybeValue) => {
|
|
678
|
+
if (typeof fieldOrFields === "undefined") {
|
|
679
|
+
setDefaults(data);
|
|
680
|
+
setDataAsDefaults(true);
|
|
681
|
+
} else {
|
|
682
|
+
setDefaults((defaults2) => {
|
|
683
|
+
return typeof fieldOrFields === "string" ? (0, import_compat.set)((0, import_es_toolkit2.cloneDeep)(defaults2), fieldOrFields, maybeValue) : Object.assign((0, import_es_toolkit2.cloneDeep)(defaults2), fieldOrFields);
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
},
|
|
687
|
+
[data, setDefaults]
|
|
688
|
+
);
|
|
689
|
+
(0, import_react10.useLayoutEffect)(() => {
|
|
690
|
+
if (!dataAsDefaults) {
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (isDirty) {
|
|
694
|
+
setDefaults(data);
|
|
695
|
+
}
|
|
696
|
+
setDataAsDefaults(false);
|
|
697
|
+
}, [dataAsDefaults]);
|
|
698
|
+
const reset = (0, import_react10.useCallback)(
|
|
699
|
+
(...fields) => {
|
|
700
|
+
if (fields.length === 0) {
|
|
701
|
+
setData(defaults);
|
|
702
|
+
} else {
|
|
703
|
+
setData(
|
|
704
|
+
(data2) => fields.filter((key) => (0, import_compat.has)(defaults, key)).reduce(
|
|
705
|
+
(carry, key) => {
|
|
706
|
+
return (0, import_compat.set)(carry, key, (0, import_compat.get)(defaults, key));
|
|
707
|
+
},
|
|
708
|
+
{ ...data2 }
|
|
709
|
+
)
|
|
710
|
+
);
|
|
711
|
+
}
|
|
712
|
+
},
|
|
713
|
+
[setData, defaults]
|
|
714
|
+
);
|
|
715
|
+
const setError = (0, import_react10.useCallback)(
|
|
716
|
+
(fieldOrFields, maybeValue) => {
|
|
717
|
+
setErrors((errors2) => {
|
|
718
|
+
const newErrors = {
|
|
719
|
+
...errors2,
|
|
720
|
+
...typeof fieldOrFields === "string" ? { [fieldOrFields]: maybeValue } : fieldOrFields
|
|
721
|
+
};
|
|
722
|
+
setHasErrors(Object.keys(newErrors).length > 0);
|
|
723
|
+
return newErrors;
|
|
724
|
+
});
|
|
725
|
+
},
|
|
726
|
+
[setErrors, setHasErrors]
|
|
727
|
+
);
|
|
728
|
+
const clearErrors = (0, import_react10.useCallback)(
|
|
729
|
+
(...fields) => {
|
|
730
|
+
setErrors((errors2) => {
|
|
731
|
+
const newErrors = Object.keys(errors2).reduce(
|
|
732
|
+
(carry, field) => ({
|
|
733
|
+
...carry,
|
|
734
|
+
...fields.length > 0 && !fields.includes(field) ? { [field]: errors2[field] } : {}
|
|
735
|
+
}),
|
|
736
|
+
{}
|
|
737
|
+
);
|
|
738
|
+
setHasErrors(Object.keys(newErrors).length > 0);
|
|
739
|
+
return newErrors;
|
|
740
|
+
});
|
|
741
|
+
},
|
|
742
|
+
[setErrors, setHasErrors]
|
|
743
|
+
);
|
|
744
|
+
const resetAndClearErrors = (0, import_react10.useCallback)(
|
|
745
|
+
(...fields) => {
|
|
746
|
+
reset(...fields);
|
|
747
|
+
clearErrors(...fields);
|
|
748
|
+
},
|
|
749
|
+
[reset, clearErrors]
|
|
750
|
+
);
|
|
751
|
+
const createSubmitMethod = (method) => (url, options) => {
|
|
752
|
+
submit(method, url, options);
|
|
753
|
+
};
|
|
754
|
+
const getMethod = (0, import_react10.useCallback)(createSubmitMethod("get"), [submit]);
|
|
755
|
+
const post = (0, import_react10.useCallback)(createSubmitMethod("post"), [submit]);
|
|
756
|
+
const put = (0, import_react10.useCallback)(createSubmitMethod("put"), [submit]);
|
|
757
|
+
const patch = (0, import_react10.useCallback)(createSubmitMethod("patch"), [submit]);
|
|
758
|
+
const deleteMethod = (0, import_react10.useCallback)(createSubmitMethod("delete"), [submit]);
|
|
759
|
+
const cancel = (0, import_react10.useCallback)(() => {
|
|
760
|
+
if (cancelToken.current) {
|
|
761
|
+
cancelToken.current.cancel();
|
|
762
|
+
}
|
|
763
|
+
}, []);
|
|
764
|
+
const transformFunction = (0, import_react10.useCallback)((callback) => {
|
|
765
|
+
transform.current = callback;
|
|
766
|
+
}, []);
|
|
767
|
+
return {
|
|
768
|
+
data,
|
|
769
|
+
setData: setDataFunction,
|
|
770
|
+
isDirty,
|
|
771
|
+
errors,
|
|
772
|
+
hasErrors,
|
|
773
|
+
processing,
|
|
774
|
+
progress,
|
|
775
|
+
wasSuccessful,
|
|
776
|
+
recentlySuccessful,
|
|
777
|
+
transform: transformFunction,
|
|
778
|
+
setDefaults: setDefaultsFunction,
|
|
779
|
+
reset,
|
|
780
|
+
setError,
|
|
781
|
+
clearErrors,
|
|
782
|
+
resetAndClearErrors,
|
|
783
|
+
submit,
|
|
784
|
+
get: getMethod,
|
|
785
|
+
post,
|
|
786
|
+
put,
|
|
787
|
+
patch,
|
|
788
|
+
delete: deleteMethod,
|
|
789
|
+
cancel
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
// src/usePoll.ts
|
|
794
|
+
var import_core6 = require("@inertiajs/core");
|
|
795
|
+
var import_react11 = require("react");
|
|
796
|
+
function usePoll(interval, requestOptions = {}, options = {
|
|
797
|
+
keepAlive: false,
|
|
798
|
+
autoStart: true
|
|
799
|
+
}) {
|
|
800
|
+
const pollRef = (0, import_react11.useRef)(
|
|
801
|
+
import_core6.router.poll(interval, requestOptions, {
|
|
802
|
+
...options,
|
|
803
|
+
autoStart: false
|
|
804
|
+
})
|
|
805
|
+
);
|
|
806
|
+
(0, import_react11.useEffect)(() => {
|
|
807
|
+
if (options.autoStart ?? true) {
|
|
808
|
+
pollRef.current.start();
|
|
809
|
+
}
|
|
810
|
+
return () => pollRef.current.stop();
|
|
811
|
+
}, []);
|
|
812
|
+
return {
|
|
813
|
+
stop: pollRef.current.stop,
|
|
814
|
+
start: pollRef.current.start
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
// src/usePrefetch.ts
|
|
819
|
+
var import_core7 = require("@inertiajs/core");
|
|
820
|
+
var import_react12 = require("react");
|
|
821
|
+
function usePrefetch(options = {}) {
|
|
822
|
+
const cached = typeof window === "undefined" ? null : import_core7.router.getCached(window.location.pathname, options);
|
|
823
|
+
const inFlight = typeof window === "undefined" ? null : import_core7.router.getPrefetching(window.location.pathname, options);
|
|
824
|
+
const [lastUpdatedAt, setLastUpdatedAt] = (0, import_react12.useState)(cached?.staleTimestamp || null);
|
|
825
|
+
const [isPrefetching, setIsPrefetching] = (0, import_react12.useState)(inFlight !== null);
|
|
826
|
+
const [isPrefetched, setIsPrefetched] = (0, import_react12.useState)(cached !== null);
|
|
827
|
+
(0, import_react12.useEffect)(() => {
|
|
828
|
+
const onPrefetchingListener = import_core7.router.on("prefetching", (e) => {
|
|
829
|
+
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
830
|
+
setIsPrefetching(true);
|
|
831
|
+
}
|
|
832
|
+
});
|
|
833
|
+
const onPrefetchedListener = import_core7.router.on("prefetched", (e) => {
|
|
834
|
+
if (e.detail.visit.url.pathname === window.location.pathname) {
|
|
835
|
+
setIsPrefetching(false);
|
|
836
|
+
setIsPrefetched(true);
|
|
837
|
+
setLastUpdatedAt(e.detail.fetchedAt);
|
|
838
|
+
}
|
|
839
|
+
});
|
|
840
|
+
return () => {
|
|
841
|
+
onPrefetchedListener();
|
|
842
|
+
onPrefetchingListener();
|
|
843
|
+
};
|
|
844
|
+
}, []);
|
|
845
|
+
return {
|
|
846
|
+
lastUpdatedAt,
|
|
847
|
+
isPrefetching,
|
|
848
|
+
isPrefetched,
|
|
849
|
+
flush: () => import_core7.router.flush(window.location.pathname, options)
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
// src/WhenVisible.ts
|
|
854
|
+
var import_core8 = require("@inertiajs/core");
|
|
855
|
+
var import_react13 = require("react");
|
|
856
|
+
var WhenVisible = ({ children, data, params, buffer, as, always, fallback }) => {
|
|
857
|
+
always = always ?? false;
|
|
858
|
+
as = as ?? "div";
|
|
859
|
+
fallback = fallback ?? null;
|
|
860
|
+
const [loaded, setLoaded] = (0, import_react13.useState)(false);
|
|
861
|
+
const hasFetched = (0, import_react13.useRef)(false);
|
|
862
|
+
const fetching = (0, import_react13.useRef)(false);
|
|
863
|
+
const ref = (0, import_react13.useRef)(null);
|
|
864
|
+
const getReloadParams = (0, import_react13.useCallback)(() => {
|
|
865
|
+
if (data) {
|
|
866
|
+
return {
|
|
867
|
+
only: Array.isArray(data) ? data : [data]
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
if (!params) {
|
|
871
|
+
throw new Error("You must provide either a `data` or `params` prop.");
|
|
872
|
+
}
|
|
873
|
+
return params;
|
|
874
|
+
}, [params, data]);
|
|
875
|
+
(0, import_react13.useEffect)(() => {
|
|
876
|
+
if (!ref.current) {
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
const observer = new IntersectionObserver(
|
|
880
|
+
(entries) => {
|
|
881
|
+
if (!entries[0].isIntersecting) {
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
if (!always && hasFetched.current) {
|
|
885
|
+
observer.disconnect();
|
|
886
|
+
}
|
|
887
|
+
if (fetching.current) {
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
hasFetched.current = true;
|
|
891
|
+
fetching.current = true;
|
|
892
|
+
const reloadParams = getReloadParams();
|
|
893
|
+
import_core8.router.reload({
|
|
894
|
+
...reloadParams,
|
|
895
|
+
onStart: (e) => {
|
|
896
|
+
fetching.current = true;
|
|
897
|
+
reloadParams.onStart?.(e);
|
|
898
|
+
},
|
|
899
|
+
onFinish: (e) => {
|
|
900
|
+
setLoaded(true);
|
|
901
|
+
fetching.current = false;
|
|
902
|
+
reloadParams.onFinish?.(e);
|
|
903
|
+
if (!always) {
|
|
904
|
+
observer.disconnect();
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
});
|
|
908
|
+
},
|
|
909
|
+
{
|
|
910
|
+
rootMargin: `${buffer || 0}px`
|
|
911
|
+
}
|
|
912
|
+
);
|
|
913
|
+
observer.observe(ref.current);
|
|
914
|
+
return () => {
|
|
915
|
+
observer.disconnect();
|
|
916
|
+
};
|
|
917
|
+
}, [ref, getReloadParams, buffer]);
|
|
918
|
+
if (always || !loaded) {
|
|
919
|
+
return (0, import_react13.createElement)(
|
|
920
|
+
as,
|
|
921
|
+
{
|
|
922
|
+
props: null,
|
|
923
|
+
ref
|
|
924
|
+
},
|
|
925
|
+
loaded ? children : fallback
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
return loaded ? children : null;
|
|
929
|
+
};
|
|
930
|
+
WhenVisible.displayName = "InertiaWhenVisible";
|
|
931
|
+
var WhenVisible_default = WhenVisible;
|
|
932
|
+
|
|
933
|
+
// src/index.ts
|
|
934
|
+
var router3 = import_core9.router;
|
|
2
935
|
//# sourceMappingURL=index.js.map
|