|
| 1 | +import styles from './Tasks.module.scss' |
| 2 | +import React, { useState } from 'react' |
| 3 | +import { ReactSortable } from 'react-sortablejs' |
| 4 | +import dayjs from 'dayjs' |
| 5 | +import relativeTime from 'dayjs/plugin/relativeTime' |
| 6 | +import { Checkbox } from '../../common/Checkbox' |
| 7 | +import { Textarea } from '../../common/Textarea' |
| 8 | +import { use_periodic_re_render } from '../../../../hooks/use-periodic-re-render' |
| 9 | +import cn from 'classnames' |
| 10 | + |
| 11 | +dayjs.extend(relativeTime) |
| 12 | + |
| 13 | +export type Task = { |
| 14 | + id: string |
| 15 | + text: string |
| 16 | + is_checked: boolean |
| 17 | + is_starred: boolean |
| 18 | + created_at: number |
| 19 | +} |
| 20 | + |
| 21 | +type Props = { |
| 22 | + tasks: Task[] |
| 23 | + on_reorder?: (tasks: Task[]) => void |
| 24 | + on_change?: (task: Task) => void |
| 25 | +} |
| 26 | + |
| 27 | +export const Tasks: React.FC<Props> = ({ tasks, on_reorder, on_change }) => { |
| 28 | + use_periodic_re_render(60 * 1000) |
| 29 | + const [editing_id, set_editing_id] = useState<string | null>(null) |
| 30 | + |
| 31 | + const handle_check_change = (task: Task, checked: boolean) => { |
| 32 | + on_change?.({ ...task, is_checked: checked }) |
| 33 | + } |
| 34 | + |
| 35 | + const handle_star_toggle = (task: Task) => { |
| 36 | + on_change?.({ ...task, is_starred: !task.is_starred }) |
| 37 | + } |
| 38 | + |
| 39 | + const render_item = (task: Task) => { |
| 40 | + const is_editing = editing_id === task.id |
| 41 | + |
| 42 | + return ( |
| 43 | + <div key={task.id} className={styles.item}> |
| 44 | + <div className={styles.item__left}> |
| 45 | + <Checkbox |
| 46 | + checked={task.is_checked} |
| 47 | + on_change={(checked) => handle_check_change(task, checked)} |
| 48 | + /> |
| 49 | + <div className={styles.item__drag_handle}> |
| 50 | + <span className="codicon codicon-gripper" /> |
| 51 | + </div> |
| 52 | + </div> |
| 53 | + <div className={styles.item__right}> |
| 54 | + <div className={styles.item__header}> |
| 55 | + <span>{dayjs(task.created_at).fromNow()}</span> |
| 56 | + <span |
| 57 | + className={cn( |
| 58 | + 'codicon', |
| 59 | + task.is_starred ? 'codicon-star-full' : 'codicon-star-empty', |
| 60 | + styles.item__star, |
| 61 | + { |
| 62 | + [styles['item__star--active']]: task.is_starred |
| 63 | + } |
| 64 | + )} |
| 65 | + onClick={() => handle_star_toggle(task)} |
| 66 | + title={task.is_starred ? 'Unstar' : 'Star'} |
| 67 | + /> |
| 68 | + </div> |
| 69 | + {is_editing ? ( |
| 70 | + <Textarea |
| 71 | + value={task.text} |
| 72 | + on_change={(value) => on_change?.({ ...task, text: value })} |
| 73 | + autofocus |
| 74 | + on_blur={() => set_editing_id(null)} |
| 75 | + on_key_down={(e) => { |
| 76 | + if (e.key === 'Enter' && !e.shiftKey) { |
| 77 | + e.preventDefault() |
| 78 | + set_editing_id(null) |
| 79 | + } |
| 80 | + }} |
| 81 | + /> |
| 82 | + ) : ( |
| 83 | + <div |
| 84 | + className={cn(styles.item__text, { |
| 85 | + [styles['item__text--checked']]: task.is_checked, |
| 86 | + [styles['item__text--placeholder']]: !task.text |
| 87 | + })} |
| 88 | + onDoubleClick={() => set_editing_id(task.id)} |
| 89 | + > |
| 90 | + {task.text || 'Double click to add text...'} |
| 91 | + </div> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + if (on_reorder) { |
| 99 | + return ( |
| 100 | + <ReactSortable |
| 101 | + list={tasks} |
| 102 | + setList={on_reorder} |
| 103 | + handle={`.${styles.item__drag_handle}`} |
| 104 | + className={styles.tasks} |
| 105 | + animation={150} |
| 106 | + > |
| 107 | + {tasks.map(render_item)} |
| 108 | + </ReactSortable> |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + return <div className={styles.tasks}>{tasks.map(render_item)}</div> |
| 113 | +} |
0 commit comments