diff options
Diffstat (limited to 'Timeline/ClientApp/src/app/home')
-rw-r--r-- | Timeline/ClientApp/src/app/home/Home.tsx | 150 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/home/TimelineBoard.tsx | 54 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/home/TimelineBoardAreaWithUser.tsx | 36 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/home/TimelineBoardAreaWithoutUser.tsx | 26 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/home/TimelineCreateDialog.tsx | 54 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/home/home.sass | 13 |
6 files changed, 333 insertions, 0 deletions
diff --git a/Timeline/ClientApp/src/app/home/Home.tsx b/Timeline/ClientApp/src/app/home/Home.tsx new file mode 100644 index 00000000..de25d5c1 --- /dev/null +++ b/Timeline/ClientApp/src/app/home/Home.tsx @@ -0,0 +1,150 @@ +import React from 'react';
+import { useHistory } from 'react-router';
+import { Row, Container, Button, Col } from 'reactstrap';
+import { useTranslation } from 'react-i18next';
+
+import { useUser } from '../data/user';
+import { TimelineInfo } from '../data/timeline';
+import { getHttpTimelineClient } from '../http/timeline';
+
+import AppBar from '../common/AppBar';
+import SearchInput from '../common/SearchInput';
+import TimelineBoardAreaWithoutUser from './TimelineBoardAreaWithoutUser';
+import TimelineBoardAreaWithUser from './TimelineBoardAreaWithUser';
+import TimelineCreateDialog from './TimelineCreateDialog';
+
+const Home: React.FC = (_) => {
+ const history = useHistory();
+
+ const { t } = useTranslation();
+
+ const user = useUser();
+
+ const [navText, setNavText] = React.useState<string>('');
+
+ const [publicTimelines, setPublicTimelines] = React.useState<
+ TimelineInfo[] | undefined
+ >(undefined);
+ const [ownTimelines, setOwnTimelines] = React.useState<
+ TimelineInfo[] | undefined
+ >(undefined);
+ const [joinTimelines, setJoinTimelines] = React.useState<
+ TimelineInfo[] | undefined
+ >(undefined);
+
+ React.useEffect(() => {
+ let subscribe = true;
+ if (user == null) {
+ setOwnTimelines(undefined);
+ setJoinTimelines(undefined);
+ void getHttpTimelineClient()
+ .listTimeline({ visibility: 'Public' })
+ .then((timelines) => {
+ if (subscribe) {
+ setPublicTimelines(timelines);
+ }
+ });
+ } else {
+ setPublicTimelines(undefined);
+ void getHttpTimelineClient()
+ .listTimeline({ relate: user.username, relateType: 'own' })
+ .then((timelines) => {
+ if (subscribe) {
+ setOwnTimelines(timelines);
+ }
+ });
+ void getHttpTimelineClient()
+ .listTimeline({ relate: user.username, relateType: 'join' })
+ .then((timelines) => {
+ if (subscribe) {
+ setJoinTimelines(timelines);
+ }
+ });
+ }
+ return () => {
+ subscribe = false;
+ };
+ }, [user]);
+
+ const [dialog, setDialog] = React.useState<'create' | null>(null);
+
+ const goto = React.useCallback((): void => {
+ if (navText === '') {
+ history.push('users/crupest');
+ } else if (navText.startsWith('@')) {
+ history.push(`users/${navText.slice(1)}`);
+ } else {
+ history.push(`timelines/${navText}`);
+ }
+ }, [navText, history]);
+
+ const openCreateDialog = React.useCallback(() => {
+ setDialog('create');
+ }, []);
+
+ const closeDialog = React.useCallback(() => {
+ setDialog(null);
+ }, []);
+
+ return (
+ <>
+ <AppBar />
+ <Container fluid style={{ marginTop: '56px' }}>
+ <Row>
+ <Col>
+ <SearchInput
+ className="justify-content-center"
+ value={navText}
+ onChange={setNavText}
+ onButtonClick={goto}
+ buttonText={t('home.go')}
+ placeholder="@crupest"
+ additionalButton={
+ user != null && (
+ <Button color="success" outline onClick={openCreateDialog}>
+ {t('home.createButton')}
+ </Button>
+ )
+ }
+ />
+ </Col>
+ </Row>
+ {(() => {
+ if (user == null) {
+ return (
+ <TimelineBoardAreaWithoutUser publicTimelines={publicTimelines} />
+ );
+ } else {
+ return (
+ <TimelineBoardAreaWithUser
+ ownTimelines={ownTimelines}
+ joinTimelines={joinTimelines}
+ />
+ );
+ }
+ })()}
+ </Container>
+ <footer className="text-right">
+ <a
+ className="mx-3 text-muted"
+ href="http://beian.miit.gov.cn/"
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <small>鄂ICP备18030913号-1</small>
+ </a>
+ <a
+ className="mx-3 text-muted"
+ href="http://www.beian.gov.cn/"
+ target="_blank"
+ rel="noopener noreferrer"
+ >
+ <small className="white-space-no-wrap">公安备案 42112102000124</small>
+ </a>
+ </footer>
+ {dialog === 'create' && <TimelineCreateDialog open close={closeDialog} />}
+ </>
+ );
+};
+
+export default Home;
diff --git a/Timeline/ClientApp/src/app/home/TimelineBoard.tsx b/Timeline/ClientApp/src/app/home/TimelineBoard.tsx new file mode 100644 index 00000000..2e017bf7 --- /dev/null +++ b/Timeline/ClientApp/src/app/home/TimelineBoard.tsx @@ -0,0 +1,54 @@ +import React from 'react';
+import clsx from 'clsx';
+import { Link } from 'react-router-dom';
+import { Spinner } from 'reactstrap';
+
+import { TimelineInfo } from '../data/timeline';
+
+import TimelineLogo from '../common/TimelineLogo';
+import UserTimelineLogo from '../common/UserTimelineLogo';
+
+export interface TimelineBoardProps {
+ title?: string;
+ timelines?: TimelineInfo[];
+ className?: string;
+}
+
+const TimelineBoard: React.FC<TimelineBoardProps> = props => {
+ const { title, timelines, className } = props;
+
+ return (
+ <div className={clsx('timeline-board', className)}>
+ {title != null && <h3 className="text-center">{title}</h3>}
+ {(() => {
+ if (timelines == null) {
+ return (
+ <div className="d-flex flex-grow-1 justify-content-center align-items-center">
+ <Spinner color="primary" />
+ </div>
+ );
+ } else {
+ return timelines.map(timeline => {
+ const { name } = timeline;
+ const isPersonal = name.startsWith('@');
+ const url = isPersonal
+ ? `/users/${timeline.owner.username}`
+ : `/timelines/${name}`;
+ return (
+ <div key={name} className="timeline-board-item">
+ {isPersonal ? (
+ <UserTimelineLogo className="icon" />
+ ) : (
+ <TimelineLogo className="icon" />
+ )}
+ <Link to={url}>{name}</Link>
+ </div>
+ );
+ });
+ }
+ })()}
+ </div>
+ );
+};
+
+export default TimelineBoard;
diff --git a/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithUser.tsx b/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithUser.tsx new file mode 100644 index 00000000..a8603b9e --- /dev/null +++ b/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithUser.tsx @@ -0,0 +1,36 @@ +import React from 'react';
+import { Row, Col } from 'reactstrap';
+import { useTranslation } from 'react-i18next';
+
+import TimelineBoard from './TimelineBoard';
+import { TimelineInfo } from '../data/timeline';
+
+interface TimelineBoardAreaWithUserProps {
+ ownTimelines?: TimelineInfo[];
+ joinTimelines?: TimelineInfo[];
+}
+
+const TimelineBoardAreaWithUser: React.FC<TimelineBoardAreaWithUserProps> = (
+ props
+) => {
+ const { t } = useTranslation();
+
+ return (
+ <Row className="my-2 justify-content-center">
+ <Col sm="6" lg="5" className="py-2">
+ <TimelineBoard
+ title={t('home.ownTimeline')}
+ timelines={props.ownTimelines}
+ />
+ </Col>
+ <Col sm="6" lg="5" className="py-2">
+ <TimelineBoard
+ title={t('home.joinTimeline')}
+ timelines={props.joinTimelines}
+ />
+ </Col>
+ </Row>
+ );
+};
+
+export default TimelineBoardAreaWithUser;
diff --git a/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithoutUser.tsx b/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithoutUser.tsx new file mode 100644 index 00000000..dc05ff09 --- /dev/null +++ b/Timeline/ClientApp/src/app/home/TimelineBoardAreaWithoutUser.tsx @@ -0,0 +1,26 @@ +import React from 'react';
+import { Row, Col } from 'reactstrap';
+
+import { TimelineInfo } from '../data/timeline';
+
+import TimelineBoard from './TimelineBoard';
+
+interface TimelineBoardAreaWithoutUserProps {
+ publicTimelines?: TimelineInfo[];
+}
+
+const TimelineBoardAreaWithoutUser: React.FC<TimelineBoardAreaWithoutUserProps> = (
+ props
+) => {
+ const { publicTimelines } = props;
+
+ return (
+ <Row className="my-2 justify-content-center">
+ <Col sm="8" lg="6">
+ <TimelineBoard timelines={publicTimelines} />
+ </Col>
+ </Row>
+ );
+};
+
+export default TimelineBoardAreaWithoutUser;
diff --git a/Timeline/ClientApp/src/app/home/TimelineCreateDialog.tsx b/Timeline/ClientApp/src/app/home/TimelineCreateDialog.tsx new file mode 100644 index 00000000..06f908f9 --- /dev/null +++ b/Timeline/ClientApp/src/app/home/TimelineCreateDialog.tsx @@ -0,0 +1,54 @@ +import React from 'react';
+import { useHistory } from 'react-router';
+
+import { validateTimelineName, timelineService } from '../data/timeline';
+
+import OperationDialog from '../common/OperationDialog';
+
+interface TimelineCreateDialogProps {
+ open: boolean;
+ close: () => void;
+}
+
+const TimelineCreateDialog: React.FC<TimelineCreateDialogProps> = (props) => {
+ const history = useHistory();
+
+ let nameSaved: string;
+
+ return (
+ <OperationDialog
+ open={props.open}
+ close={props.close}
+ titleColor="success"
+ title="home.createDialog.title"
+ inputScheme={[
+ {
+ type: 'text',
+ label: 'home.createDialog.name',
+ helperText: 'home.createDialog.nameFormat',
+ validator: (name) => {
+ if (name.length === 0) {
+ return 'home.createDialog.noEmpty';
+ } else if (name.length > 26) {
+ return 'home.createDialog.tooLong';
+ } else if (!validateTimelineName(name)) {
+ return 'home.createDialog.badFormat';
+ } else {
+ return null;
+ }
+ },
+ },
+ ]}
+ onProcess={([name]) => {
+ nameSaved = name as string;
+ return timelineService.createTimeline(nameSaved).toPromise();
+ }}
+ onSuccessAndClose={() => {
+ history.push(`timelines/${nameSaved}`);
+ }}
+ failurePrompt={(e) => `${e as string}`}
+ />
+ );
+};
+
+export default TimelineCreateDialog;
diff --git a/Timeline/ClientApp/src/app/home/home.sass b/Timeline/ClientApp/src/app/home/home.sass new file mode 100644 index 00000000..28a2e5f3 --- /dev/null +++ b/Timeline/ClientApp/src/app/home/home.sass @@ -0,0 +1,13 @@ +.timeline-board-item
+ font-size: 1.1em
+ @extend .my-2
+ .icon
+ height: 1.3em
+ @extend .mr-2
+
+.timeline-board
+ @extend .cru-card
+ @extend .d-flex
+ @extend .flex-column
+ @extend .p-3
+ min-height: 200px
|