import React, { FunctionComponent, MouseEvent, useCallback, useState } from "react"; import style from "./style.module.css"; import { useHistory } from 'react-router'; import { base58UUID } from '../../util/uuid'; import { useStoredProjectList } from "../../hooks/use-stored-project-list"; import { formatDate } from "../../util/date"; import { Direction, useSort } from "../../hooks/useSort"; const Home: FunctionComponent = () => { const [projects] = useStoredProjectList() const [sortingKey, setSortingKey] = useState('updatedAt') const [sortingDirection, setSortingDirection] = useState(Direction.DESC) const sortedProjects = useSort(projects, sortingKey, sortingDirection) const history = useHistory() const openNewProject = () => { const uuid = base58UUID() history.push(`/p/${uuid}`) }; const openProject = useCallback((evt: MouseEvent) => { const projectId = evt.currentTarget.dataset.projectId; history.push(`/p/${projectId}`) }, []) const sortBy = useCallback((evt: MouseEvent) => { const key = evt.currentTarget.dataset.sortKey if (!key) return if (sortingKey !== key) { setSortingKey(key) return } setSortingDirection(sortingDirection => -sortingDirection) }, [sortingKey, sortingDirection]) return (
{ sortedProjects.map(p => ( )) } { projects.length === 0 ? : null }
🗒️ {p.label ? p.label : "Projet sans nom"} {p.updatedAt ? formatDate(p.updatedAt) : "--"}
Aucun project pour l'instant.
); }; interface TableHeaderProps { onClick?: React.MouseEventHandler sortingKey: string isCurrentSortingKey: boolean sortingDirection: Direction label: string } const TableHeader: FunctionComponent = ({ onClick, isCurrentSortingKey, sortingKey, label, sortingDirection }) => { return ( {label}{sortingDirection === Direction.ASC ? '↑' : '↓'} ) } export default Home;