miolo-react 3.0.0-beta.21 → 3.0.0-beta.210

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo-react",
3
- "version": "3.0.0-beta.21",
3
+ "version": "3.0.0-beta.210",
4
4
  "description": "React utils for miolo",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -17,28 +17,25 @@
17
17
  "license": "MIT",
18
18
  "type": "module",
19
19
  "exports": {
20
- ".": {
21
- "development": "./src/index.mjs",
22
- "import": "./dist/miolo-react.mjs",
23
- "default": "./dist/miolo-react.umd.js"
24
- }
20
+ ".": "./src/index.mjs"
25
21
  },
26
22
  "files": [
27
- "dist",
28
- "src"
23
+ "src",
24
+ "package.json"
29
25
  ],
30
26
  "scripts": {
31
- "reset": "rm -fr package-lock.json npm-lock.yaml dist/* && npm i",
32
- "clean": "rm -fr ./dist/* ",
33
- "bundle": "npx xeira bundle --target=browser --source_index=./src/index.mjs --bundle_folder=./dist --bundle_name=miolo-react --bundle_node_polyfill=true --bundle_extension=umd,mjs",
34
- "dist": "npm run clean && npm run bundle"
27
+ "reset": "rm -fr package-lock.json npm-lock.yaml node_modules && npm i",
28
+ "lint": "biome check ./src --reporter=github",
29
+ "lint:fix": "biome check --write ./src --reporter=github",
30
+ "prepublishOnly": "npm run lint"
35
31
  },
36
32
  "dependencies": {
33
+ "idb-keyval": "^6.2.5",
37
34
  "miolo-cli": "../miolo-cli"
38
- },
35
+ },
39
36
  "peerDependencies": {
40
- "react": "^18.3.1",
41
- "react-dom": "^18.3.1"
37
+ "react": "^19.2.4",
38
+ "react-dom": "^19.2.4"
42
39
  },
43
40
  "peerDependenciesMeta": {
44
41
  "react": {
@@ -48,12 +45,7 @@
48
45
  "optional": true
49
46
  }
50
47
  },
51
- "devDependencies": {
52
- "xeira": "^1.2.3"
53
- },
54
- "eslintConfig": {
55
- "extends": [
56
- "../../node_modules/xeira/configs/eslint.react.mjs"
57
- ]
48
+ "engines": {
49
+ "node": ">=24"
58
50
  }
59
- }
51
+ }
@@ -1,14 +1,9 @@
1
- import React from 'react'
2
- import MioloContextProvider from './context/MioloContextProvider.jsx'
1
+ import MioloContextProvider from "./context/MioloContextProvider.jsx"
3
2
 
4
3
  const AppBrowser = ({ children }) => {
5
- const context = typeof window !== 'undefined' && window.__CONTEXT ? window.__CONTEXT : {};
4
+ const context = typeof window !== "undefined" && window.__CONTEXT ? window.__CONTEXT : {}
6
5
 
7
- return (
8
- <MioloContextProvider context={context}>
9
- {children}
10
- </MioloContextProvider>
11
- )
6
+ return <MioloContextProvider context={context}>{children}</MioloContextProvider>
12
7
  }
13
8
 
14
9
  export default AppBrowser
package/src/AppServer.jsx CHANGED
@@ -1,17 +1,7 @@
1
+ import MioloContextProvider from "./context/MioloContextProvider.jsx"
1
2
 
2
- import React from 'react'
3
- import MioloContextProvider from './context/MioloContextProvider.jsx'
4
-
5
-
6
- const AppServer = ({context, children}) => {
7
-
8
- return (
9
- <MioloContextProvider
10
- context= {context || {}}>
11
- {children}
12
- </MioloContextProvider>
13
- )
3
+ const AppServer = ({ context, children }) => {
4
+ return <MioloContextProvider context={context || {}}>{children}</MioloContextProvider>
14
5
  }
15
6
 
16
7
  export default AppServer
17
-
@@ -1,5 +1,5 @@
1
- import React from 'react'
1
+ import React from "react"
2
2
 
3
3
  const MioloContext = React.createContext()
4
4
 
5
- export default MioloContext
5
+ export default MioloContext
@@ -1,60 +1,71 @@
1
- import React , {useState, useEffect, useCallback} from 'react'
2
- import Context from './MioloContext.mjs'
3
- import { miolo_client } from 'miolo-cli'
4
- import { useSsrDataOrReload } from '../ssr/useSsrDataOrReload.mjs'
1
+ import { miolo_client } from "miolo-cli"
2
+ import { useCallback, useEffect, useState } from "react"
3
+ import { useSsrDataOrReload } from "../ssr/useSsrDataOrReload.mjs"
4
+ import Context from "./MioloContext.mjs"
5
5
 
6
+ const MioloContextProvider = ({ context, children }) => {
7
+ const [innerContext, setInnerContext] = useState(context)
8
+ const [mioloObj, setMioloObj] = useState(miolo_client(context))
6
9
 
7
- const MioloContextProvider = ({context, children}) => {
8
- const [innerContext, setInnerContext]= useState(context)
9
- const [mioloObj, setMioloObj]= useState(miolo_client(context))
10
-
11
10
  useEffect(() => {
12
11
  setInnerContext(context)
13
12
  setMioloObj(miolo_client(context))
14
13
  }, [context])
15
-
16
- const login = useCallback(async (credentials) => {
17
- const { fetcher } = mioloObj
18
- const { config } = innerContext
19
14
 
20
- const url = config.login_url || '/login'
21
- const resp = await fetcher.login(url, credentials)
22
-
23
- if (resp?.data) {
24
- if (resp?.data?.authenticated) {
25
- setInnerContext(current => {
26
- return {
27
- ...current,
28
- ...resp?.data,
29
- }
30
- })
15
+ const localLogin = useCallback(
16
+ async (params) => {
17
+ const { fetcher } = mioloObj
18
+ const { config } = innerContext
19
+
20
+ const url = config.login_url || "/login"
21
+ const resp = await fetcher.post(url, params)
22
+
23
+ if (resp?.data) {
24
+ if (resp?.data?.authenticated) {
25
+ setInnerContext((current) => {
26
+ return {
27
+ ...current,
28
+ ...resp.data,
29
+ config: {
30
+ ...current.config,
31
+ ...(resp.data?.config || {})
32
+ }
33
+ }
34
+ })
35
+ }
36
+
37
+ return resp?.data
31
38
  }
32
39
 
33
- return resp?.data
34
- }
40
+ return {}
41
+ },
42
+ [innerContext, mioloObj]
43
+ )
35
44
 
36
- return {}
37
- }, [innerContext, mioloObj])
45
+ const googleLogin = useCallback((redirect = "/") => {
46
+ const url = `/auth/google?redirect=${encodeURIComponent(redirect)}`
47
+ window.location.href = url
48
+ }, [])
38
49
 
39
50
  const logout = useCallback(async () => {
40
51
  const { fetcher } = mioloObj
41
52
  const { config } = innerContext
42
53
 
43
- const url = config.logout_url || '/logout'
44
- const _resp = await fetcher.logout(url)
54
+ const url = config.logout_url || "/logout"
55
+ const _resp = await fetcher.post(url)
45
56
  // resp.redirected= true
46
57
 
47
- setInnerContext(current => {
58
+ setInnerContext((current) => {
48
59
  return {
49
60
  ...current,
50
61
  user: undefined,
51
- authenticated: false,
62
+ authenticated: false
52
63
  }
53
64
  })
54
65
 
55
66
  return {
56
67
  user: undefined,
57
- authenticated: false,
68
+ authenticated: false
58
69
  }
59
70
  }, [innerContext, mioloObj])
60
71
 
@@ -62,34 +73,37 @@ const MioloContextProvider = ({context, children}) => {
62
73
  setInnerContext((current) => {
63
74
  return {
64
75
  ...current,
65
- user,
76
+ user
66
77
  }
67
78
  })
68
79
  }, [])
69
80
 
70
- const useSsrData = (name, defval, loader, modifier) => {
71
- return useSsrDataOrReload(innerContext, mioloObj, name, defval, loader, modifier)
72
- }
73
-
81
+ const useSsrData = (name, options) => {
82
+ return useSsrDataOrReload(innerContext, mioloObj, name, options)
83
+ }
84
+
74
85
  return (
75
- <Context.Provider
86
+ <Context.Provider
76
87
  value={{
77
- //context: innerContext,
88
+ //context: innerContext,
78
89
  //setContext: setInnerContext,
79
90
  //miolo: mioloObj,
80
91
  user: innerContext.user,
81
92
  updateUser,
82
93
  authenticated: innerContext.authenticated,
83
94
  fetcher: mioloObj.fetcher,
84
- //socket: mioloObj.socket,
85
- login,
95
+ socket: mioloObj.socket,
96
+ logger: mioloObj.logger,
97
+ googleLogin,
98
+ localLogin,
86
99
  logout,
87
- useSsrData
88
- }}>
100
+ useSsrData,
101
+ authMethod: innerContext?.config?.auth_method
102
+ }}
103
+ >
89
104
  {children}
90
105
  </Context.Provider>
91
106
  )
92
107
  }
93
108
 
94
-
95
109
  export default MioloContextProvider
@@ -1,5 +1,5 @@
1
- import {useContext} from 'react'
2
- import MioloContext from './MioloContext.mjs'
1
+ import { useContext } from "react"
2
+ import MioloContext from "./MioloContext.mjs"
3
3
 
4
4
  const useMioloContext = () => useContext(MioloContext)
5
5
 
@@ -1,15 +1,10 @@
1
- import React, {useContext} from 'react'
2
- import MioloContext from './MioloContext.mjs'
1
+ import { useContext } from "react"
2
+ import MioloContext from "./MioloContext.mjs"
3
3
 
4
- /* eslint react/display-name:0 */
5
4
  const withMioloContext = (BaseComponent) => (props) => {
6
5
  const context = useContext(MioloContext)
7
6
 
8
- return (
9
- <BaseComponent {...props}
10
- {...context}/>
11
- );
7
+ return <BaseComponent {...props} {...context} />
12
8
  }
13
9
 
14
-
15
- export default withMioloContext
10
+ export default withMioloContext
package/src/index.mjs CHANGED
@@ -1,7 +1,6 @@
1
- import withMioloContext from './context/withMioloContext.jsx'
2
- import useMioloContext from './context/useMioloContext.jsx'
1
+ import AppBrowser from "./AppBrowser.jsx"
2
+ import AppServer from "./AppServer.jsx"
3
+ import useMioloContext from "./context/useMioloContext.jsx"
4
+ import withMioloContext from "./context/withMioloContext.jsx"
3
5
 
4
- import AppBrowser from './AppBrowser.jsx'
5
- import AppServer from './AppServer.jsx'
6
-
7
- export {withMioloContext, useMioloContext, AppBrowser, AppServer}
6
+ export { AppBrowser, AppServer, useMioloContext, withMioloContext }
@@ -1,32 +1,31 @@
1
-
2
1
  const _getDataFromWindow = (name) => {
3
2
  try {
4
- if (window != undefined) {
5
- const ssr_data= window.__CONTEXT.ssr_data
6
-
7
- if (ssr_data!=undefined) {
8
- if (ssr_data[name]!=undefined) {
3
+ if (window !== undefined) {
4
+ const ssr_data = window.__CONTEXT.ssr_data
5
+
6
+ if (ssr_data !== undefined) {
7
+ if (ssr_data[name] !== undefined) {
9
8
  return ssr_data[name]
10
9
  }
11
10
  }
12
11
  }
13
- } catch(e) {}
14
-
12
+ } catch (_) {}
13
+
15
14
  return undefined
16
15
  }
17
16
 
18
17
  const getSsrDataFromContext = (context, name) => {
19
- let data= undefined
18
+ let data
20
19
 
21
- if (context?.ssr_data != undefined && context?.ssr_data[name]!=undefined) {
22
- data= context.ssr_data[name]
20
+ if (context?.ssr_data !== undefined && context?.ssr_data[name] !== undefined) {
21
+ data = context.ssr_data[name]
23
22
  } else {
24
- const wdata= _getDataFromWindow(name)
25
- if (wdata != undefined) {
26
- data= wdata
23
+ const wdata = _getDataFromWindow(name)
24
+ if (wdata !== undefined) {
25
+ data = wdata
27
26
  }
28
27
  }
29
-
28
+
30
29
  return data
31
30
  }
32
31
 
@@ -0,0 +1,69 @@
1
+ import { useEffect, useRef } from "react"
2
+
3
+ export default function usePropsCheck(loader, effect, modifier, desc) {
4
+ const prevLoader = useRef(loader)
5
+ const prevEffect = useRef(effect)
6
+ const prevModifier = useRef(modifier)
7
+ const loaderChangeCount = useRef(0)
8
+ const effectChangeCount = useRef(0)
9
+ const modifierChangeCount = useRef(0)
10
+ const loaderChangeTime = useRef(0)
11
+ const effectChangeTime = useRef(0)
12
+ const modifierChangeTime = useRef(0)
13
+
14
+ useEffect(() => {
15
+ if (prevLoader.current !== undefined && prevLoader.current !== loader) {
16
+ const now = Date.now()
17
+ // Si el cambio ocurrió rápido (<100ms desde el último cambio), sumamos al contador continuo
18
+ if (now - loaderChangeTime.current < 100) {
19
+ loaderChangeCount.current += 1
20
+ if (loaderChangeCount.current >= 4) {
21
+ console.warn(
22
+ `🚨 [miolo][useSsrDataOrReload]: 'options.loader' varies too frequently. Wrap it in a useCallback! ${desc}`
23
+ )
24
+ }
25
+ } else {
26
+ // Ha pasado suficiente tiempo como para ser un cambio humano/aislado
27
+ loaderChangeCount.current = 1
28
+ }
29
+ loaderChangeTime.current = now
30
+ }
31
+ prevLoader.current = loader
32
+ }) // Evalúa en cada render
33
+
34
+ useEffect(() => {
35
+ if (prevEffect.current !== undefined && prevEffect.current !== effect) {
36
+ const now = Date.now()
37
+ if (now - effectChangeTime.current < 100) {
38
+ effectChangeCount.current += 1
39
+ if (effectChangeCount.current >= 4) {
40
+ console.warn(
41
+ `🚨 [miolo][useSsrDataOrReload]: 'options.effect' varies too frequently. Wrap it in a useMemo or useCallback! ${desc}`
42
+ )
43
+ }
44
+ } else {
45
+ effectChangeCount.current = 1
46
+ }
47
+ effectChangeTime.current = now
48
+ }
49
+ prevEffect.current = effect
50
+ }) // Evalúa en cada render
51
+
52
+ useEffect(() => {
53
+ if (prevModifier.current !== undefined && prevModifier.current !== modifier) {
54
+ const now = Date.now()
55
+ if (now - modifierChangeTime.current < 100) {
56
+ modifierChangeCount.current += 1
57
+ if (modifierChangeCount.current >= 4) {
58
+ console.warn(
59
+ `🚨 [miolo][useSsrDataOrReload]: 'options.modifier' varies too frequently. Wrap it in a useCallback! ${desc}`
60
+ )
61
+ }
62
+ } else {
63
+ modifierChangeCount.current = 1
64
+ }
65
+ modifierChangeTime.current = now
66
+ }
67
+ prevModifier.current = modifier
68
+ }) // Evalúa en cada render
69
+ }