Skip to content

Commit

Permalink
docs: refactor site demo to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
otakustay committed Feb 24, 2023
1 parent 3685f87 commit 964094d
Show file tree
Hide file tree
Showing 32 changed files with 552 additions and 271 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@ecomfe/eslint-config": "^7.4.0",
"@emotion/react": "^11.10.6",
"@emotion/styled": "^11.10.6",
"@reskript/cli": "5.7.4",
"@reskript/cli-build": "5.7.4",
"@reskript/cli-dev": "5.7.4",
Expand All @@ -56,6 +58,7 @@
"@types/lodash": "^4.14.191",
"@types/react-test-renderer": "^18.0.0",
"@types/refractor": "^2.8.0",
"@types/sha1": "^1.1.3",
"@types/warning": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
Expand Down Expand Up @@ -106,7 +109,6 @@
"sha1": "^1.1.1",
"standard-version": "^9.5.0",
"style-loader": "^3.3.1",
"styled-components": "^5.3.6",
"typescript": "^4.9.5",
"unidiff": "^1.0.2",
"vitest": "^0.28.5",
Expand Down
4 changes: 4 additions & 0 deletions site/components/App/app.global.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
--background-color-pure: #fff;
--border-color: #e8e8e8;
--heading-color: #252525;
--link-text-color: #1890ff;
--link-text-hover-color: #40a9ff;
--link-text-active-color: #096dd9;
--disabled-text-color: #d9d9d9;
}

html,
Expand Down
15 changes: 9 additions & 6 deletions site/components/App/index.js → site/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import './app.global.less';

const fakeIndex = () => sha(uniqueId()).slice(0, 9);

const App = () => {
const [{diff, source}, setData] = useState({diff: '', source: ''});
interface DiffData {
diff: string;
source: string | null;
}

export default function App() {
const [{diff, source}, setData] = useState<DiffData>({diff: '', source: ''});
const file = useMemo(
() => {
if (!diff) {
Expand All @@ -39,7 +44,7 @@ const App = () => {
<>
<Configuration />
<DiffView
key={sha(diff) + (source ? sha(source) : '')}
key={`${sha(diff)}${source ? sha(source) : ''}`}
type={file.type}
hunks={file.hunks}
oldSource={source}
Expand All @@ -50,6 +55,4 @@ const App = () => {
</div>
</ConfigurationProvider>
);
};

export default App;
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,46 @@
import {ReactNode} from 'react';
import {Modal, Radio, Switch} from 'antd';
import styles from './OptionsModal.less';
import {MarkEditsType, ViewType} from 'react-diff-view';

/* eslint-disable react/jsx-no-bind */

const {Group: RadioGroup, Button: RadioButton} = Radio;

const Row = ({title, tooltip, children}) => (
<div className={styles.row}>
<div className={styles.field}>
<span className={styles.rowTitle}>
{title}
</span>
{children}
</div>
<div className={styles.tooltip}>
{tooltip}
interface RowProps {
title: string;
tooltip: string;
children: ReactNode;
}

function Row({title, tooltip, children}: RowProps) {
return (
<div className={styles.row}>
<div className={styles.field}>
<span className={styles.rowTitle}>
{title}
</span>
{children}
</div>
<div className={styles.tooltip}>
{tooltip}
</div>
</div>
</div>
);
);
}

interface Props {
visible: boolean;
viewType: ViewType;
editsType: MarkEditsType;
showGutter: boolean;
onSwitchViewType: (value: ViewType) => void;
onSwitchEditsType: (value: MarkEditsType) => void;
onSwitchGutterVisibility: (value: boolean) => void;
onClose: () => void;
}

const OptionsModal = props => {
export default function OptionsModal(props: Props) {
const {
visible,
viewType,
Expand Down Expand Up @@ -51,6 +72,4 @@ const OptionsModal = props => {
</Row>
</Modal>
);
};

export default OptionsModal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import styles from './index.less';

const {Option} = Select;

const useBoolean = initialValue => {
function useBoolean(initialValue: boolean) {
const [value, setValue] = useState(initialValue);
const on = useCallback(() => setValue(true), []);
const off = useCallback(() => setValue(false), []);
return [value, on, off];
};
return [value, on, off] as const;
}

const Configuration = () => {
export default function Configuration() {
const [isModalVisible, openModal, closeModal] = useBoolean(false);
const configuration = useConfiguration();
const switchViewType = useSwitchViewType();
Expand Down Expand Up @@ -54,6 +54,4 @@ const Configuration = () => {
/>
</div>
);
};

export default Configuration;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useCallback} from 'react';
import styled from 'styled-components';
import styled from '@emotion/styled';
import InteractiveLabel from '../../InteractiveLabel';

const Text = styled.div`
white-space: pre;
Expand All @@ -14,7 +15,15 @@ const Footer = styled.footer`
margin-top: 12px;
`;

export default function CommentDisplay({commentId, content, time, onEdit, onDelete}) {
interface Props {
commentId: string;
content: string;
time: Date;
onEdit: (id: string) => void;
onDelete: (id: string) => void;
}

export default function CommentDisplay({commentId, content, time, onEdit, onDelete}: Props) {
const edit = useCallback(
() => onEdit(commentId),
[commentId, onEdit]
Expand All @@ -31,8 +40,8 @@ export default function CommentDisplay({commentId, content, time, onEdit, onDele
</Text>
<Footer>
<time>{time.toLocaleDateString()} {time.toLocaleTimeString()}</time>
<a onClick={edit}>edit</a>
<a onClick={remove}>delete</a>
<InteractiveLabel onClick={edit}>edit</InteractiveLabel>
<InteractiveLabel onClick={remove}>delete</InteractiveLabel>
</Footer>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useCallback, useState} from 'react';
import styled from 'styled-components';
import {ChangeEvent, useCallback, useState} from 'react';
import styled from '@emotion/styled';
import {Button, Input} from 'antd';

const Layout = styled.div`
Expand All @@ -15,10 +15,19 @@ const Footer = styled.footer`
justify-content: flex-end;
`;

export default function CommentEditor({commentId, type, defaultContent, onSave, onCancel, onDelete}) {
interface Props {
commentId: string;
type: 'edit' | 'create';
defaultContent: string;
onSave: (id: string, value: string) => void;
onCancel: (id: string) => void;
onDelete: (id: string) => void;
}

export default function CommentEditor({commentId, type, defaultContent, onSave, onCancel, onDelete}: Props) {
const [value, setValue] = useState(defaultContent);
const updateValue = useCallback(
e => setValue(e.target.value),
(e: ChangeEvent<HTMLTextAreaElement>) => setValue(e.target.value),
[]
);
const save = useCallback(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from 'styled-components';
import styled from '@emotion/styled';
import Editor from './Editor';
import Display from './Display';

Expand All @@ -7,7 +7,18 @@ const Layout = styled.div`
background-color: var(--background-color-secondary);
`;

export default function Comment({id, content, state, time, onSave, onEdit, onCancel, onDelete}) {
interface Props {
id: string;
content: string;
state: 'create' | 'edit' | 'display';
time: Date;
onSave: (id: string, content: string) => void;
onEdit: (id: string) => void;
onCancel: (id: string) => void;
onDelete: (id: string) => void;
}

export default function Comment({id, content, state, time, onSave, onEdit, onCancel, onDelete}: Props) {
return (
<Layout>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useCallback} from 'react';
import styled from 'styled-components';
import {getChangeKey} from 'react-diff-view';
import styled from '@emotion/styled';
import {ChangeData, getChangeKey} from 'react-diff-view';

const Trigger = styled.span`
display: flex;
Expand All @@ -14,18 +14,23 @@ const Trigger = styled.span`
height: 22px;
font-size: 16px;
font-weight: bold;
background-color: #fff;
background-color: var(--background-color-pure);
box-shadow: 0 1px 4px rgba(27, 31, 35, .15);
color: #999;
color: var(--diff-decoration-content-color);
:hover {
transition: all .2s linear;
background-color: #fafafa;
background-color: var(--background-color-secondary);
color: #333;
}
`;

export default function CommentTrigger({change, onClick}) {
interface Props {
change: ChangeData;
onClick: (value: string) => void;
}

export default function CommentTrigger({change, onClick}: Props) {
const click = useCallback(
() => onClick(getChangeKey(change)),
[change, onClick]
Expand Down
10 changes: 0 additions & 10 deletions site/components/DiffView/HunkInfo.js

This file was deleted.

14 changes: 14 additions & 0 deletions site/components/DiffView/HunkInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {Decoration, DecorationProps, HunkData} from 'react-diff-view';

interface Props extends Omit<DecorationProps, 'children'> {
hunk: HunkData;
}

export default function HunkInfo({hunk, ...props}: Props) {
return (
<Decoration {...props}>
{null}
{hunk.content}
</Decoration>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ self.addEventListener(
catch (ex) {
const payload = {
success: false,
reason: ex.message,
reason: ex instanceof Error ? ex.message : `${ex}`,
};
self.postMessage({id, payload});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createElement, useCallback} from 'react';
import {CaretUpOutlined, CaretDownOutlined, PlusCircleOutlined} from '@ant-design/icons';
import {Decoration} from 'react-diff-view';
import {Decoration, DecorationProps} from 'react-diff-view';
import styles from './Unfold.less';

const ICON_TYPE_MAPPING = {
Expand All @@ -9,7 +9,14 @@ const ICON_TYPE_MAPPING = {
none: PlusCircleOutlined,
};

const Unfold = ({start, end, direction, onExpand, ...props}) => {
interface Props extends Omit<DecorationProps, 'children'> {
start: number;
end: number;
direction: 'up' | 'down' | 'none';
onExpand: (start: number, end: number) => void;
}

export default function Unfold({start, end, direction, onExpand, ...props}: Props) {
const expand = useCallback(
() => onExpand(start, end),
[onExpand, start, end]
Expand All @@ -26,6 +33,4 @@ const Unfold = ({start, end, direction, onExpand, ...props}) => {
</div>
</Decoration>
);
};

export default Unfold;
}
Loading

0 comments on commit 964094d

Please sign in to comment.