123 lines
3.2 KiB
TypeScript
Raw Normal View History

import React from 'react';
2023-08-17 14:21:38 +00:00
// import { Sticky, StickyContainer } from "react-sticky"
import Sticky from 'react-sticky-el';
import { Box, Flex } from '@chakra-ui/react';
import { useLocation, useMatches } from '@remix-run/react';
2023-07-21 01:13:02 +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) => {
const { getThingtime } = useThingtime();
2023-07-21 01:13:02 +00:00
const { pathname } = useLocation();
const matches = useMatches();
2023-07-21 01:13:02 +00:00
const location = React.useMemo(() => {
return matches[matches.length - 1];
}, [matches]);
2023-07-21 01:13:02 +00:00
const path = React.useMemo(() => {
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
console.log('nik path', path);
return path || 'thingtime';
}, [location]);
2023-07-21 01:13:02 +00:00
const thing = React.useMemo(() => {
// remove /things/ from path
const ret = getThingtime(path);
2023-07-21 01:13:02 +00:00
return ret;
}, [path, getThingtime]);
2023-07-21 01:13:02 +00:00
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]);
const containerRef = React.useRef(null);
const editorRef = React.useRef(null);
2023-08-17 14:21:38 +00:00
React.useEffect(() => {
const scrollListener = () => {
if (containerRef?.current?.getBoundingClientRect) {
const { top } = containerRef?.current?.getBoundingClientRect();
2023-08-17 14:21:38 +00:00
editorRef.current.style.top = `${-top}px`;
2023-08-17 14:21:38 +00:00
}
};
2023-08-17 14:21:38 +00:00
window.addEventListener('scroll', scrollListener);
2023-08-17 14:21:38 +00:00
return () => {
window.removeEventListener('scroll', scrollListener);
};
}, []);
2023-08-17 14:21:38 +00:00
2023-07-21 01:13:02 +00:00
return (
<Flex
2023-08-17 14:21:38 +00:00
ref={containerRef}
// position="sticky"
position="relative"
alignItems={inEditorMode ? 'flex-start' : 'center'}
2023-07-21 01:13:02 +00:00
justifyContent="center"
2023-08-17 14:21:38 +00:00
// overflow="scroll"
// height="auto"
flexDirection={inEditorMode ? 'row' : 'column'}
2023-07-21 01:13:02 +00:00
maxWidth="100%"
2023-08-17 14:21:38 +00:00
// maxHeight="100vh"
2023-07-21 01:13:02 +00:00
>
{inEditorMode && (
2023-08-17 14:21:38 +00:00
<Box
ref={editorRef}
position="relative"
// position="sticky"
// top={200}
// alignSelf="flex-start"
overflow="scroll"
width="600px"
2023-08-17 14:21:38 +00:00
// width="100%"
maxHeight="100vh"
// paddingY={2}
2023-08-17 14:21:38 +00:00
>
<Thingtime
path={path}
thing={thing}
render
chakras={{ marginY: '200px' }}
// width="600px"
2023-08-17 14:21:38 +00:00
></Thingtime>
</Box>
)}
<Thingtime edit={inEditMode} path={path} thing={thing} chakras={{ marginY: '200px' }} width="600px"></Thingtime>
2023-07-21 01:13:02 +00:00
</Flex>
);
};