Open History to see the last commit
1import { useCallback, useMemo, useRef } from 'react'
2import { invariant } from '../invariant'
3import { useSuspenseQuery } from '@tanstack/react-query'
4import { boardQueries, useUpdateBoardMutation } from '../queries.js'
5import { NewColumn } from './NewColumn.js'
6import { Column as ColumnComponent } from './Column.js'
7import type { Column } from '../db/schema.js'
8import { EditableText } from '~/components/EditableText.js'
9β
10export function Board({ boardId }: { boardId: string }) {
11 const newColumnAddedRef = useRef(false)
12 const updateBoardMutation = useUpdateBoardMutation()
13 const { data: board } = useSuspenseQuery(boardQueries.detail(boardId))
14β
15 // scroll right when new columns are added
16 const scrollContainerRef = useRef<HTMLDivElement>(null)
17 const columnRef = useCallback((_node: HTMLElement | null) => {
18 if (scrollContainerRef.current && newColumnAddedRef.current) {
19 newColumnAddedRef.current = false
20 scrollContainerRef.current.scrollLeft =
21 scrollContainerRef.current.scrollWidth
22 }
23 }, [])
24β
25 const itemsById = useMemo(
26 () => new Map(board.items.map((item) => [item.id, item])),
27 [board.items],
28 )
29β
30 type ColumnWithItems = Column & { items: typeof board.items }
31β
32 const columns = useMemo(() => {
33 const columnsMap = new Map<string, ColumnWithItems>()
34β
35 for (const column of [...board.columns]) {
36 columnsMap.set(column.id, { ...column, items: [] })
37 }
38β
39 // add items to their columns
40 for (const item of itemsById.values()) {
41 const columnId = item.columnId
42 const column = columnsMap.get(columnId)
43 invariant(column, 'missing column')
44 column.items.push(item)
45 }
46β
47 return [...columnsMap.values()].sort((a, b) => a.order - b.order)
48 }, [board.columns, itemsById])
49β
50 return (
51 <div
52 className="grow min-h-0 flex flex-col overflow-x-scroll"
53 ref={scrollContainerRef}
54 style={{ backgroundColor: board.color }}
55 >
56 <h1>
57 <EditableText
58 value={
59 // optimistic update
60 updateBoardMutation.isPending &&
61 updateBoardMutation.variables.data.name
62 ? updateBoardMutation.variables.data.name
63 : board.name
64 }
65 fieldName="name"
66 inputClassName="mx-8 my-4 text-2xl font-medium border border-slate-400 rounded-lg py-1 px-2 text-black"
67 buttonClassName="mx-8 my-4 text-2xl font-medium block rounded-lg text-left border border-transparent py-1 px-2 text-slate-800"
68 buttonLabel={`Edit board "${board.name}" name`}
69 inputLabel="Edit board name"
70 onChange={(value) => {
71 updateBoardMutation.mutate({
72 data: {
73 id: board.id,
74 name: value,
75 },
76 })
77 }}
78 />
79 </h1>
80β
81 <div className="flex grow min-h-0 h-full items-start px-8 pb-4 w-fit">
82 {columns.map((col, index) => {
83 return (
84 <ColumnComponent
85 ref={columnRef}
86 key={col.id}
87 name={col.name}
88 columnId={col.id}
89 boardId={board.id}
90 items={col.items}
91 order={col.order}
92 previousOrder={columns[index - 1] ? columns[index - 1].order : 0}
93 nextOrder={
94 columns[index + 1] ? columns[index + 1].order : col.order + 1
95 }
96 />
97 )
98 })}
99 <NewColumn
100 boardId={board.id}
101 editInitially={board.columns.length === 0}
102 onNewColumnAdded={() => {
103 newColumnAddedRef.current = true
104 }}
105 />
106 </div>
107β
108 {/* trolling you to add some extra margin to the right of the container with a whole dang div */}
109 <div data-lol className="w-8 h-1 shrink-0" />
110 </div>
111 )
112}