CoastalCommitsPastes/client/components/view-document/index.tsx

130 lines
4.7 KiB
TypeScript
Raw Normal View History

import { memo, useRef, useState } from "react"
2022-03-06 19:46:59 -05:00
import styles from './document.module.css'
2022-03-21 18:55:21 -04:00
import Download from '@geist-ui/icons/download'
import ExternalLink from '@geist-ui/icons/externalLink'
2022-03-09 20:11:37 -05:00
import Skeleton from "react-loading-skeleton"
import { Button, ButtonGroup, Card, Input, Spacer, Tabs, Textarea, Tooltip } from "@geist-ui/core"
import HtmlPreview from "@components/preview"
// import Link from "next/link"
2022-03-06 19:46:59 -05:00
type Props = {
title: string
2022-03-07 01:16:08 -05:00
initialTab?: "edit" | "preview"
2022-03-09 20:11:37 -05:00
skeleton?: boolean
id: string
content: string
2022-03-06 19:46:59 -05:00
}
const DownloadButton = ({ rawLink }: { rawLink?: string }) => {
return (<div className={styles.actionWrapper}>
<ButtonGroup className={styles.actions}>
<Tooltip text="Download">
<a href={`${rawLink}?download=true`} target="_blank" rel="noopener noreferrer">
<Button
scale={2 / 3} px={0.6}
icon={<Download />}
auto
aria-label="Download"
/>
</a>
</Tooltip>
<Tooltip text="Open raw in new tab">
<a href={rawLink} target="_blank" rel="noopener noreferrer">
<Button
scale={2 / 3} px={0.6}
icon={<ExternalLink />}
auto
aria-label="Open raw file in new tab"
/>
</a>
</Tooltip>
</ButtonGroup>
</div>)
}
const Document = ({ content, title, initialTab = 'edit', skeleton, id }: Props) => {
2022-03-06 19:46:59 -05:00
const codeEditorRef = useRef<HTMLTextAreaElement>(null)
const [tab, setTab] = useState(initialTab)
// const height = editable ? "500px" : '100%'
const height = "100%";
const handleTabChange = (newTab: string) => {
if (newTab === 'edit') {
codeEditorRef.current?.focus()
}
setTab(newTab as 'edit' | 'preview')
}
2022-03-06 19:46:59 -05:00
const rawLink = () => {
if (id) {
return `/file/raw/${id}`
}
}
2022-03-09 20:11:37 -05:00
if (skeleton) {
return <>
<Spacer height={1} />
<Card marginBottom={'var(--gap)'} marginTop={'var(--gap)'} style={{ maxWidth: 980, margin: "0 auto" }}>
<div className={styles.fileNameContainer}>
<Skeleton width={275} height={36} />
</div>
<div className={styles.descriptionContainer}>
<div style={{ flexDirection: 'row', display: 'flex' }}><Skeleton width={125} height={36} /></div>
<Skeleton width={'100%'} height={350} />
</div >
</Card>
</>
}
2022-03-06 19:46:59 -05:00
return (
<>
<Spacer height={1} />
2022-03-23 19:28:39 -04:00
<div className={styles.card}>
<div className={styles.fileNameContainer}>
<Input
value={title}
readOnly
marginTop="var(--gap-double)"
size={1.2}
font={1.2}
label="Filename"
width={"100%"}
2022-03-20 23:45:37 -04:00
id={title}
/>
</div>
<div className={styles.descriptionContainer}>
<DownloadButton rawLink={rawLink()} />
<Tabs onChange={handleTabChange} initialValue={initialTab} hideDivider leftSpace={0}>
<Tabs.Item label={"Raw"} value="edit">
{/* <textarea className={styles.lineCounter} wrap='off' readOnly ref={lineNumberRef}>1.</textarea> */}
<div style={{ marginTop: 'var(--gap)', display: 'flex', flexDirection: 'column' }}>
<Textarea
readOnly
ref={codeEditorRef}
value={content}
width="100%"
// TODO: Textarea should grow to fill parent if height == 100%
style={{ flex: 1, minHeight: 350 }}
resize="vertical"
className={styles.textarea}
/>
</div>
</Tabs.Item>
<Tabs.Item label="Preview" value="preview">
<HtmlPreview height={height} fileId={id} />
</Tabs.Item>
</Tabs>
2022-03-08 03:37:18 -05:00
</div >
2022-03-23 19:28:39 -04:00
</div >
</>
2022-03-06 19:46:59 -05:00
)
}
export default memo(Document)