2023-07-04 08:05:08 +00:00
|
|
|
import React, { createContext } from "react"
|
2023-07-15 08:08:36 +00:00
|
|
|
import { parse, stringify } from "flatted"
|
2023-07-04 08:05:08 +00:00
|
|
|
|
|
|
|
import { sanitise } from "~/functions/sanitise"
|
|
|
|
import { smarts } from "~/smarts"
|
2023-07-01 09:46:36 +00:00
|
|
|
|
|
|
|
export interface ThingtimeContextInterface {
|
|
|
|
thingtime: any
|
|
|
|
setThingtime: any
|
2023-07-04 08:05:08 +00:00
|
|
|
getThingtime: any
|
|
|
|
thingtimeRef: any
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-04 08:05:08 +00:00
|
|
|
export const ThingtimeContext = createContext<
|
|
|
|
ThingtimeContextInterface[] | null
|
|
|
|
>(null)
|
2023-07-01 09:46:36 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
window.smarts = smarts
|
|
|
|
} catch (err) {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2023-07-05 10:05:35 +00:00
|
|
|
const force = {
|
|
|
|
settings: {
|
2023-07-15 08:08:36 +00:00
|
|
|
// commanderActive: false,
|
2023-07-05 10:05:35 +00:00
|
|
|
},
|
|
|
|
version: 22,
|
|
|
|
}
|
|
|
|
|
|
|
|
const newVersionData = {
|
2023-07-01 14:52:27 +00:00
|
|
|
Content: {
|
2023-07-01 15:05:55 +00:00
|
|
|
hidden1: "Edit this to your heart's desire.",
|
2023-07-04 08:05:08 +00:00
|
|
|
"How?": "Just search for Content and edit the value to whatever you want.",
|
|
|
|
"Example:": `Content = New Content!
|
2023-07-01 15:05:55 +00:00
|
|
|
Content.Nested Content = New Nested Content!
|
2023-07-04 08:05:08 +00:00
|
|
|
`,
|
|
|
|
},
|
2023-07-01 14:22:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 10:05:35 +00:00
|
|
|
const initialValues = {
|
2023-07-01 09:46:36 +00:00
|
|
|
settings: {
|
2023-07-15 08:08:36 +00:00
|
|
|
commanderActive: false,
|
2023-07-01 09:46:36 +00:00
|
|
|
clearCommanderOnToggle: true,
|
2023-07-04 08:05:08 +00:00
|
|
|
clearCommanderContextOnToggle: true,
|
2023-07-01 14:13:27 +00:00
|
|
|
},
|
2023-07-05 10:05:35 +00:00
|
|
|
Content: {
|
|
|
|
hidden1: "Edit this to your heart's desire.",
|
|
|
|
"How?": "Just search for Content and edit the value to whatever you want.",
|
|
|
|
"Example:": `Content = New Content!
|
|
|
|
Content.Nested Content = New Nested Content!
|
|
|
|
`,
|
|
|
|
},
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 22:05:07 +00:00
|
|
|
// const initialThingtime = createInitialThingtime()
|
2023-07-05 10:05:35 +00:00
|
|
|
const initialThingtime = smarts.merge(initialValues, force)
|
|
|
|
|
2023-07-15 08:08:36 +00:00
|
|
|
// TODO: Make localStorage be loaded first before initialValues if local version exists
|
|
|
|
// and is valid
|
|
|
|
// Issue seems to be server id is different to client hydration
|
|
|
|
|
|
|
|
// let thingtimeToUse = initialThingtime
|
|
|
|
|
|
|
|
// try {
|
|
|
|
// const thingtimeFromLocalStorage = window.localStorage.getItem("thingtime")
|
|
|
|
|
|
|
|
// if (thingtimeFromLocalStorage) {
|
|
|
|
// const parsed = parse(thingtimeFromLocalStorage)
|
|
|
|
// if (parsed) {
|
|
|
|
// const localIsValid = !parsed.version || parsed.version >= force.version
|
|
|
|
// if (localIsValid) {
|
|
|
|
// const newThingtime = smarts.merge(force, parsed)
|
|
|
|
// thingtimeToUse = newThingtime
|
|
|
|
// } else {
|
|
|
|
// const withVersionUpdates = smarts.merge(newVersionData, parsed)
|
|
|
|
// const newThingtime = smarts.merge(force, withVersionUpdates)
|
|
|
|
// thingtimeToUse = newThingtime
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// } catch (err) {
|
|
|
|
// console.error("Caught error restoring thingtime from localstorage", err)
|
|
|
|
// }
|
|
|
|
|
2023-08-07 22:05:07 +00:00
|
|
|
// initialise thingtime
|
2023-07-24 01:36:25 +00:00
|
|
|
initialThingtime.thingtime = initialThingtime
|
|
|
|
initialThingtime.tt = initialThingtime
|
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
export const ThingtimeProvider = (props: any): JSX.Element => {
|
2023-08-07 22:05:07 +00:00
|
|
|
const [thingtime, rawSet] = React.useState()
|
2023-07-21 01:13:02 +00:00
|
|
|
|
2023-07-01 14:13:27 +00:00
|
|
|
const thingtimeRef = React.useRef(thingtime)
|
2023-07-15 08:08:36 +00:00
|
|
|
const stateRef = React.useRef({
|
|
|
|
c: 1,
|
|
|
|
})
|
2023-07-01 09:46:36 +00:00
|
|
|
|
2023-08-07 22:05:07 +00:00
|
|
|
const [loading, setLoading] = React.useState(true)
|
|
|
|
|
2023-07-24 01:36:25 +00:00
|
|
|
const set = React.useCallback((newThingtime) => {
|
|
|
|
const thingtimeReference = {
|
|
|
|
...newThingtime,
|
|
|
|
}
|
2023-07-21 13:33:47 +00:00
|
|
|
|
2023-07-24 01:36:25 +00:00
|
|
|
thingtimeReference.tt = thingtimeReference
|
|
|
|
thingtimeReference.thingtime = thingtimeReference
|
|
|
|
rawSet(thingtimeReference)
|
|
|
|
}, [])
|
2023-07-01 09:46:36 +00:00
|
|
|
|
2023-07-24 01:36:25 +00:00
|
|
|
const setThingtime = React.useCallback(
|
|
|
|
(path, value) => {
|
|
|
|
const newThingtime = thingtime
|
|
|
|
|
|
|
|
const paths = smarts.parsePropertyPath(path)
|
|
|
|
|
|
|
|
// find first parent where a path is undefined
|
|
|
|
// paths is array of path parts such as ["path1", "path2", "path3"]
|
|
|
|
// we want to create a new reference at the first object which has an undefined part of the path
|
|
|
|
// and is an object itself
|
|
|
|
// so that react will detect the change and re-render
|
|
|
|
// "path1" = { ...thingtime["path1"] } if path1.path2 undefined
|
|
|
|
// "path1.path2" = { ...thingtime["path1"]["path2"] } if path1.path2.path3 undefined
|
|
|
|
// "path1.path2.path3" = { ...thingtime["path1"]["path2"]["path3"] }
|
|
|
|
// etc
|
|
|
|
let done = false
|
|
|
|
paths.forEach((pathPart, index) => {
|
|
|
|
if (!done) {
|
|
|
|
const pathParts = paths.slice(0, index + 1)
|
|
|
|
const tmpPath = pathParts.join(".")
|
|
|
|
const parentPath = pathParts.slice(0, -1).join(".")
|
|
|
|
|
|
|
|
const valAtPath = smarts.getsmart(newThingtime, tmpPath)
|
|
|
|
|
|
|
|
if (parentPath) {
|
|
|
|
if (typeof valAtPath !== "object" || valAtPath === null) {
|
|
|
|
const parentVal = smarts.getsmart(newThingtime, parentPath)
|
|
|
|
if (typeof parentVal === "object") {
|
|
|
|
const newParent = Array.isArray(parentVal)
|
|
|
|
? [...parentVal]
|
|
|
|
: { ...parentVal }
|
|
|
|
smarts.setsmart(newThingtime, parentPath, newParent)
|
|
|
|
}
|
|
|
|
done = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-07-01 09:46:36 +00:00
|
|
|
|
2023-07-24 01:45:32 +00:00
|
|
|
newThingtime.thingtime = newThingtime
|
|
|
|
newThingtime.tt = newThingtime
|
|
|
|
|
2023-07-24 01:36:25 +00:00
|
|
|
console.log(
|
|
|
|
"nik setting newThingtime value at path",
|
2023-07-24 01:45:32 +00:00
|
|
|
'"' + path + '"',
|
2023-07-24 01:36:25 +00:00
|
|
|
"value: ",
|
|
|
|
value
|
|
|
|
)
|
2023-07-21 13:33:47 +00:00
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
smarts.setsmart(newThingtime, path, value)
|
|
|
|
|
|
|
|
set(newThingtime)
|
|
|
|
},
|
2023-07-21 13:33:47 +00:00
|
|
|
[thingtime, set]
|
2023-07-01 09:46:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const getThingtime = React.useCallback(
|
|
|
|
(...args) => {
|
2023-07-04 08:05:08 +00:00
|
|
|
const rawPath = args[0]
|
2023-07-15 08:08:36 +00:00
|
|
|
const path = rawPath
|
2023-07-24 03:02:27 +00:00
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
return thingtime
|
|
|
|
}
|
|
|
|
|
2023-07-15 08:08:36 +00:00
|
|
|
// do we need to sanitise?
|
|
|
|
// const path = sanitise(rawPath)
|
2023-07-04 08:05:08 +00:00
|
|
|
console.log("Getting thingtime at path", path)
|
2023-07-21 13:33:47 +00:00
|
|
|
// console.trace("Getting thingtime at path", path)
|
2023-07-01 09:46:36 +00:00
|
|
|
return smarts.getsmart(thingtime, path)
|
|
|
|
},
|
|
|
|
[thingtime]
|
|
|
|
)
|
|
|
|
|
2023-07-15 08:08:36 +00:00
|
|
|
const populatePaths = React.useCallback((obj, path, paths, seen = []) => {
|
2023-07-24 01:36:25 +00:00
|
|
|
try {
|
|
|
|
Object.keys(obj).forEach((key) => {
|
|
|
|
const val = obj[key]
|
|
|
|
const newPath = path ? `${path}${path ? "." : ""}${key}` : key
|
|
|
|
if (typeof val === "object") {
|
|
|
|
paths.push(newPath)
|
|
|
|
if (!seen?.includes(val)) {
|
|
|
|
seen.push(val)
|
|
|
|
populatePaths(val, newPath, paths, seen)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
paths.push(newPath)
|
2023-07-15 08:08:36 +00:00
|
|
|
}
|
2023-07-24 01:36:25 +00:00
|
|
|
})
|
|
|
|
} catch {
|
|
|
|
// nothing
|
|
|
|
}
|
2023-07-15 08:08:36 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
const paths = React.useMemo(() => {
|
|
|
|
// const paths = ["tt", "thingtime", "."]
|
|
|
|
const paths = []
|
|
|
|
|
|
|
|
// populatePaths(thingtime, commandPath)
|
|
|
|
populatePaths(thingtime, "", paths)
|
|
|
|
|
|
|
|
return paths
|
|
|
|
}, [populatePaths, thingtime])
|
|
|
|
|
|
|
|
// get thingtime from localstorage
|
|
|
|
React.useEffect(() => {
|
|
|
|
try {
|
|
|
|
const thingtimeFromLocalStorage = window.localStorage.getItem("thingtime")
|
|
|
|
|
|
|
|
if (thingtimeFromLocalStorage) {
|
|
|
|
const parsed = parse(thingtimeFromLocalStorage)
|
|
|
|
if (parsed) {
|
|
|
|
const localIsValid =
|
|
|
|
!parsed.version || parsed.version >= force.version
|
|
|
|
let newThingtime = null
|
|
|
|
if (localIsValid) {
|
|
|
|
newThingtime = smarts.merge(force, parsed)
|
|
|
|
} else {
|
|
|
|
const withVersionUpdates = smarts.merge(newVersionData, parsed)
|
|
|
|
newThingtime = smarts.merge(force, withVersionUpdates)
|
|
|
|
}
|
2023-07-24 01:36:25 +00:00
|
|
|
console.log(
|
|
|
|
"nik setting new thingtime from localStorage",
|
|
|
|
newThingtime
|
|
|
|
)
|
2023-07-15 08:08:36 +00:00
|
|
|
set(newThingtime)
|
|
|
|
}
|
2023-08-07 22:05:07 +00:00
|
|
|
} else {
|
|
|
|
set(initialThingtime)
|
2023-07-15 08:08:36 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error("There was an error getting thingtime from localStorage")
|
|
|
|
}
|
2023-08-07 22:05:07 +00:00
|
|
|
setLoading(false)
|
2023-07-15 08:08:36 +00:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
// thingtime change listener
|
2023-07-01 09:46:36 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
try {
|
|
|
|
window.setThingtime = setThingtime
|
|
|
|
window.thingtime = thingtime
|
2023-07-05 10:05:35 +00:00
|
|
|
window.tt = thingtime
|
2023-07-01 09:46:36 +00:00
|
|
|
} catch {
|
|
|
|
// nothing
|
|
|
|
}
|
|
|
|
|
2023-07-15 08:08:36 +00:00
|
|
|
if (stateRef.current.initialized) {
|
|
|
|
if (thingtime.thingtime !== thingtime || thingtime.tt !== thingtime) {
|
|
|
|
if (!(stateRef?.current?.c >= 10)) {
|
|
|
|
stateRef.current.c++
|
|
|
|
const newThingtime = {
|
|
|
|
...thingtime,
|
|
|
|
}
|
|
|
|
newThingtime.thingtime = newThingtime
|
|
|
|
newThingtime.tt = newThingtime
|
|
|
|
set(newThingtime)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
console.log("Setting thingtime to localStorage", thingtime)
|
2023-07-21 13:33:47 +00:00
|
|
|
// setTimeout(() => {
|
|
|
|
const stringified = stringify(thingtime)
|
|
|
|
window.localStorage.setItem("thingtime", stringified)
|
|
|
|
// }, 600)
|
2023-07-15 08:08:36 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error("There was an error saving thingtime to localStorage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stateRef.current.initialized = true
|
|
|
|
}
|
|
|
|
|
|
|
|
thingtimeRef.current = thingtime
|
|
|
|
|
2023-07-04 08:05:08 +00:00
|
|
|
const keyListener = (e) => {}
|
2023-07-01 09:46:36 +00:00
|
|
|
|
2023-07-04 08:05:08 +00:00
|
|
|
window.addEventListener("keydown", keyListener)
|
2023-07-01 09:46:36 +00:00
|
|
|
|
|
|
|
return () => {
|
2023-07-04 08:05:08 +00:00
|
|
|
window.removeEventListener("keydown", keyListener)
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
2023-07-24 01:36:25 +00:00
|
|
|
}, [setThingtime, thingtime, set])
|
2023-07-01 09:46:36 +00:00
|
|
|
|
|
|
|
const value = {
|
|
|
|
thingtime,
|
|
|
|
setThingtime,
|
2023-07-01 14:13:27 +00:00
|
|
|
getThingtime,
|
2023-07-04 08:05:08 +00:00
|
|
|
thingtimeRef,
|
2023-07-15 08:08:36 +00:00
|
|
|
paths,
|
2023-08-07 22:05:07 +00:00
|
|
|
loading,
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ThingtimeContext.Provider value={value}>
|
|
|
|
{props?.children}
|
|
|
|
</ThingtimeContext.Provider>
|
|
|
|
)
|
|
|
|
}
|