2023-07-04 08:05:08 +00:00
|
|
|
import React, { createContext } from "react"
|
|
|
|
|
|
|
|
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: {
|
|
|
|
commanderActive: true,
|
|
|
|
},
|
|
|
|
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-05 10:05:35 +00:00
|
|
|
commanderActive: true,
|
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-07-05 10:05:35 +00:00
|
|
|
const initialThingtime = smarts.merge(initialValues, force)
|
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
export const ThingtimeProvider = (props: any): JSX.Element => {
|
2023-07-05 10:05:35 +00:00
|
|
|
const [thingtime, set] = React.useState(smarts.merge(initialValues, force))
|
2023-07-01 14:13:27 +00:00
|
|
|
|
|
|
|
const thingtimeRef = React.useRef(thingtime)
|
|
|
|
|
|
|
|
// get thingtime from localstorage
|
|
|
|
React.useEffect(() => {
|
|
|
|
try {
|
2023-07-04 08:05:08 +00:00
|
|
|
const thingtimeFromLocalStorage = window.localStorage.getItem("thingtime")
|
2023-07-01 14:13:27 +00:00
|
|
|
|
|
|
|
if (thingtimeFromLocalStorage) {
|
|
|
|
const parsed = JSON.parse(thingtimeFromLocalStorage)
|
|
|
|
if (parsed) {
|
2023-07-05 10:05:35 +00:00
|
|
|
const localIsValid =
|
|
|
|
!parsed.version || parsed.version >= force.version
|
|
|
|
if (localIsValid) {
|
|
|
|
const newThingtime = smarts.merge(force, parsed)
|
|
|
|
console.log("nik comm newThingtime", newThingtime)
|
|
|
|
set(newThingtime)
|
2023-07-01 14:13:27 +00:00
|
|
|
} else {
|
2023-07-05 10:05:35 +00:00
|
|
|
const withVersionUpdates = smarts.merge(newVersionData, parsed)
|
|
|
|
const newThingtime = smarts.merge(force, withVersionUpdates)
|
2023-07-01 14:13:27 +00:00
|
|
|
set(newThingtime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2023-07-04 08:05:08 +00:00
|
|
|
console.error("There was an error getting thingtime from localStorage")
|
2023-07-01 14:13:27 +00:00
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
thingtimeRef.current = thingtime
|
|
|
|
|
|
|
|
try {
|
2023-07-04 08:05:08 +00:00
|
|
|
window.localStorage.setItem("thingtime", JSON.stringify(thingtime))
|
2023-07-01 14:13:27 +00:00
|
|
|
} catch (err) {
|
2023-07-04 08:05:08 +00:00
|
|
|
console.error("There was an error saving thingtime to localStorage")
|
2023-07-01 14:13:27 +00:00
|
|
|
}
|
|
|
|
}, [thingtime])
|
2023-07-01 09:46:36 +00:00
|
|
|
|
|
|
|
const setThingtime = React.useCallback(
|
|
|
|
(path, value) => {
|
|
|
|
const prevThingtime = thingtime
|
|
|
|
|
|
|
|
const newThingtime = {
|
2023-07-04 08:05:08 +00:00
|
|
|
...prevThingtime,
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if first characters of path starts with thingtime or tt and strip from path
|
|
|
|
|
|
|
|
path = sanitise(path)
|
|
|
|
|
|
|
|
smarts.setsmart(newThingtime, path, value)
|
|
|
|
|
2023-07-01 14:13:27 +00:00
|
|
|
// subtract last path part from dot delimitted path
|
|
|
|
// prop1.prop2.prop3 => prop1.prop2
|
2023-07-04 08:05:08 +00:00
|
|
|
const pathParts = path.split(".")
|
2023-07-01 14:13:27 +00:00
|
|
|
pathParts.pop()
|
2023-07-04 08:05:08 +00:00
|
|
|
const parentPath = pathParts.join(".")
|
2023-07-01 14:13:27 +00:00
|
|
|
|
|
|
|
if (parentPath?.length) {
|
|
|
|
const parent = smarts.getsmart(newThingtime, parentPath)
|
|
|
|
|
|
|
|
const newParent = Array.isArray(parent) ? [...parent] : { ...parent }
|
|
|
|
|
|
|
|
smarts.setsmart(newThingtime, parentPath, newParent)
|
|
|
|
}
|
|
|
|
|
2023-07-01 09:46:36 +00:00
|
|
|
set(newThingtime)
|
|
|
|
},
|
|
|
|
[thingtime]
|
|
|
|
)
|
|
|
|
|
|
|
|
const getThingtime = React.useCallback(
|
|
|
|
(...args) => {
|
2023-07-04 08:05:08 +00:00
|
|
|
const rawPath = args[0]
|
|
|
|
if (
|
|
|
|
rawPath === "thingtime" ||
|
|
|
|
rawPath === "tt" ||
|
|
|
|
rawPath === "." ||
|
|
|
|
!rawPath
|
|
|
|
) {
|
2023-07-01 09:46:36 +00:00
|
|
|
return thingtime
|
|
|
|
}
|
2023-07-04 08:05:08 +00:00
|
|
|
const path = sanitise(rawPath)
|
|
|
|
console.log("Getting thingtime at path", path)
|
2023-07-01 09:46:36 +00:00
|
|
|
return smarts.getsmart(thingtime, path)
|
|
|
|
},
|
|
|
|
[thingtime]
|
|
|
|
)
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}, [setThingtime, thingtime])
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
thingtime,
|
|
|
|
setThingtime,
|
2023-07-01 14:13:27 +00:00
|
|
|
getThingtime,
|
2023-07-04 08:05:08 +00:00
|
|
|
thingtimeRef,
|
2023-07-01 09:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ThingtimeContext.Provider value={value}>
|
|
|
|
{props?.children}
|
|
|
|
</ThingtimeContext.Provider>
|
|
|
|
)
|
|
|
|
}
|