87 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-07-21 01:13:02 +00:00
import React from "react"
2023-07-21 13:33:47 +00:00
import { Flex } from "@chakra-ui/react"
import { useLocation, useMatches } from "@remix-run/react"
2023-07-21 01:13:02 +00:00
2023-07-21 13:33:47 +00:00
import { Thingtime } from "./Thingtime"
import { useThingtime } from "./useThingtime"
2023-07-21 01:13:02 +00:00
2023-07-21 13:33:47 +00:00
export const ThingtimeURL = (props) => {
2023-07-21 01:13:02 +00:00
const { getThingtime } = useThingtime()
const { pathname } = useLocation()
2023-07-21 01:13:02 +00:00
const matches = useMatches()
const location = React.useMemo(() => {
return matches[matches.length - 1]
}, [matches])
const path = React.useMemo(() => {
2023-08-15 23:10:36 +00:00
console.log("ThingtimeURL location", location)
// const sanitisation = ["/things", "/edit", "/editor", "/code", "/coder"]
// // strip the leading /path1/path2 path1 section from the path
// let pathPartOne = location?.pathname?.split("/")[2]
// // remove all sanitsation strings from path
// sanitisation.forEach((sanitisationString) => {
// pathPartOne = pathPartOne?.replace(sanitisationString, "")
// })
// strip the leading /path1/path2 path1 section from the path
const pathPartOne = location?.pathname?.split("/")[2]
2023-07-21 13:33:47 +00:00
const path = pathPartOne?.replace(/\//g, ".")
2023-07-21 01:13:02 +00:00
return path || "thingtime"
2023-07-21 13:33:47 +00:00
}, [location])
2023-07-21 01:13:02 +00:00
const thing = React.useMemo(() => {
// remove /things/ from path
const ret = getThingtime(path)
return ret
}, [path, getThingtime])
const inEditorMode = React.useMemo(() => {
if (pathname.slice(0, 7) === "/editor") {
return true
}
return false
}, [pathname])
const inEditMode = React.useMemo(() => {
if (pathname.slice(0, 5) === "/edit") {
return true
}
return false
}, [pathname])
2023-07-21 01:13:02 +00:00
return (
<Flex
alignItems={inEditorMode ? "flex-start" : "center"}
2023-07-21 01:13:02 +00:00
justifyContent="center"
flexDirection={inEditorMode ? "row" : "column"}
2023-07-21 01:13:02 +00:00
maxWidth="100%"
>
{inEditorMode && (
<Thingtime
path={path}
thing={thing}
render
chakras={{ marginY: 200 }}
width="600px"
></Thingtime>
)}
2023-07-21 01:13:02 +00:00
<Thingtime
edit={inEditMode}
2023-07-21 01:13:02 +00:00
path={path}
thing={thing}
chakras={{ marginY: 200 }}
width="600px"
></Thingtime>
</Flex>
)
}