ywana-core8 0.1.47 → 0.1.49

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": "ywana-core8",
3
- "version": "0.1.47",
3
+ "version": "0.1.49",
4
4
  "description": "ywana-core8",
5
5
  "homepage": "https://ywana.github.io/workspace",
6
6
  "author": "Ernesto Roldan Garcia",
package/src/site/site.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { Children, Fragment, useState, useContext, useEffect } from 'react'
1
+ import React, { Children, Fragment, useState, useContext, useEffect, useCallback, useMemo } from 'react'
2
2
  import { Icon } from '../html/icon'
3
3
  import { Tabs, Tab } from '../html/tab'
4
4
  import { Header } from '../html/header'
@@ -21,12 +21,30 @@ export const SiteProvider = ({ children, siteLang, siteDictionary }) => {
21
21
  const [info, setInfo] = useState(null)
22
22
  const [showConsole, setShowConsole] = useState(false)
23
23
  const [consoleLines, setConsoleLines] = useState([])
24
- const [page, setPage] = useState()
25
24
  const [dialog, setDialog] = useState()
26
25
  const [promptDialog, setPromptDialog] = useState()
27
26
  const [preview, setPreview] = useState()
28
27
  const [breadcrumb, setBreadcrumb] = useState()
29
-
28
+ const [history, setHistory] = useState([])
29
+
30
+ // 📌 Extraer la página actual desde la URL (por defecto "home")
31
+ const getCurrentPageFromURL = () => {
32
+ const path = window.location.pathname.replace("/", "")
33
+ return path || "home"
34
+ }
35
+ const [page, setPage] = useState(getCurrentPageFromURL())
36
+
37
+ useEffect(() => {
38
+ // 📌 Detectar cambios en la URL cuando el usuario presiona "Atrás" o "Adelante"
39
+ const handlePopState = (event) => {
40
+ const previousPage = event.state?.page || "home"
41
+ setPage(previousPage)
42
+ }
43
+
44
+ window.addEventListener("popstate", handlePopState)
45
+ return () => window.removeEventListener("popstate", handlePopState)
46
+ }, [])
47
+
30
48
  const value = {
31
49
 
32
50
  lang,
@@ -34,13 +52,16 @@ export const SiteProvider = ({ children, siteLang, siteDictionary }) => {
34
52
 
35
53
  dictionary,
36
54
  setDictionary,
55
+ translate: useCallback((key) => dictionary?.[key]?.[lang] || key, [lang, dictionary]),
56
+ /*
37
57
  translate: (key) => {
38
58
  if (!key) return key
39
59
  if (dictionary === undefined) return key
40
60
  const term = dictionary[key]
41
61
  return term ? term[lang] : key
42
62
  },
43
-
63
+ */
64
+
44
65
  sideNav,
45
66
  setSideNav,
46
67
 
@@ -64,7 +85,21 @@ export const SiteProvider = ({ children, siteLang, siteDictionary }) => {
64
85
  setBreadcrumb,
65
86
 
66
87
  page,
67
- goto: (id) => { setPage(id) },
88
+ goto: (id) => {
89
+ if (page) {
90
+ setHistory(prev => [...prev, page]) // 🔹 Guarda la página actual en el historial antes de cambiar
91
+ }
92
+ setPage(id)
93
+ window.history.pushState({ page: id }, "", `/${id}`) // 🔹 Actualiza la URL
94
+ },
95
+ goBack: () => {
96
+ if (history.length > 0) {
97
+ const lastPage = history[history.length - 1]
98
+ setHistory(prev => prev.slice(0, -1)) // 🔹 Elimina la última entrada del historial
99
+ setPage(lastPage) // 🔹 Vuelve a la página anterior
100
+ window.history.back() // 🔹 Regresa en la navegación del navegador
101
+ }
102
+ },
68
103
 
69
104
  dialog,
70
105
  openDialog: (dialog) => { setDialog(dialog) },
@@ -101,7 +136,7 @@ export const SiteProvider = ({ children, siteLang, siteDictionary }) => {
101
136
 
102
137
  return (
103
138
  <SiteContext.Provider value={value}>
104
- {children}
139
+ {children}
105
140
  </SiteContext.Provider>
106
141
  )
107
142
  }
@@ -204,14 +239,15 @@ const SiteMenu = ({ iconSrc, title, children, min }) => {
204
239
  context.goto(id)
205
240
  }
206
241
 
207
- const sections = children ?
208
- Children.toArray(children).reduce((sections, page) => {
209
- const section = page.props ? page.props.section : '...'
210
- if (!sections[section]) sections[section] = []
211
- const { id, icon, title } = page.props
212
- if (title) sections[section].push({ id, icon, title })
213
- return sections
214
- }, {}) : {}
242
+ const sections = useMemo(() => {
243
+ return children.reduce((acc, child) => {
244
+ const section = child.props.section || "";
245
+ if (!acc[section]) acc[section] = [];
246
+ const { title } = child.props;
247
+ if (title) acc[section].push(child.props);
248
+ return acc;
249
+ }, {});
250
+ }, [children]);
215
251
 
216
252
  const style = sideNav === 'max' ? 'max' : 'min'
217
253
  const toggleIcon = sideNav === 'max' ? 'chevron_left' : 'chevron_right'