|
| 1 | +import { |
| 2 | + moveItemIsReady, |
| 3 | + renderBenchApp, |
| 4 | + useBenchState, |
| 5 | +} from '@shared/benchHarness'; |
| 6 | +import { |
| 7 | + DOUBLE_LIST_STYLE, |
| 8 | + IssueRow, |
| 9 | + PINNED_STRIP_STYLE, |
| 10 | + PinnedCardView, |
| 11 | + PlainIssueList, |
| 12 | +} from '@shared/components'; |
| 13 | +import { |
| 14 | + FIXTURE_USERS, |
| 15 | + FIXTURE_USERS_BY_LOGIN, |
| 16 | + FIXTURE_ISSUES_BY_NUMBER, |
| 17 | + sortByTitle, |
| 18 | +} from '@shared/data'; |
| 19 | +import { setCurrentIssues } from '@shared/refStability'; |
| 20 | +import { |
| 21 | + fetchIssue, |
| 22 | + fetchIssueList, |
| 23 | + updateIssue, |
| 24 | + updateUser as serverUpdateUser, |
| 25 | + createIssue, |
| 26 | + deleteIssue, |
| 27 | +} from '@shared/server'; |
| 28 | +import type { Issue } from '@shared/types'; |
| 29 | +import React, { useCallback, useEffect, useMemo, useState } from 'react'; |
| 30 | + |
| 31 | +function SortedListView({ |
| 32 | + limit, |
| 33 | + refetchKey, |
| 34 | +}: { |
| 35 | + limit?: number; |
| 36 | + refetchKey: number; |
| 37 | +}) { |
| 38 | + const [issues, setIssues] = useState<Issue[] | null>(null); |
| 39 | + useEffect(() => { |
| 40 | + fetchIssueList().then(setIssues); |
| 41 | + }, [refetchKey]); |
| 42 | + const sorted = useMemo(() => (issues ? sortByTitle(issues) : []), [issues]); |
| 43 | + if (!sorted.length) return null; |
| 44 | + return ( |
| 45 | + <div data-sorted-list> |
| 46 | + <PlainIssueList issues={sorted} limit={limit} /> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | + |
| 51 | +function DetailView({ |
| 52 | + number, |
| 53 | + refetchKey, |
| 54 | +}: { |
| 55 | + number: number; |
| 56 | + refetchKey: number; |
| 57 | +}) { |
| 58 | + const [issue, setIssue] = useState<Issue | null>(null); |
| 59 | + useEffect(() => { |
| 60 | + fetchIssue({ number }).then(setIssue); |
| 61 | + }, [number, refetchKey]); |
| 62 | + if (!issue) return null; |
| 63 | + return ( |
| 64 | + <div data-detail-view data-issue-number={number}> |
| 65 | + <IssueRow issue={issue} /> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +function PinnedCard({ |
| 71 | + number, |
| 72 | + refetchKey, |
| 73 | +}: { |
| 74 | + number: number; |
| 75 | + refetchKey: number; |
| 76 | +}) { |
| 77 | + const [issue, setIssue] = useState<Issue | null>(null); |
| 78 | + useEffect(() => { |
| 79 | + fetchIssue({ number }).then(setIssue); |
| 80 | + }, [number, refetchKey]); |
| 81 | + if (!issue) return null; |
| 82 | + return <PinnedCardView issue={issue} />; |
| 83 | +} |
| 84 | + |
| 85 | +function PinnedStrip({ |
| 86 | + numbers, |
| 87 | + refetchKey, |
| 88 | +}: { |
| 89 | + numbers: number[]; |
| 90 | + refetchKey: number; |
| 91 | +}) { |
| 92 | + return ( |
| 93 | + <div data-pinned-strip style={PINNED_STRIP_STYLE}> |
| 94 | + {numbers.map(n => ( |
| 95 | + <PinnedCard key={n} number={n} refetchKey={refetchKey} /> |
| 96 | + ))} |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
| 101 | +function ListView({ |
| 102 | + count, |
| 103 | + limit, |
| 104 | + refetchKey, |
| 105 | +}: { |
| 106 | + count: number; |
| 107 | + limit?: number; |
| 108 | + refetchKey: number; |
| 109 | +}) { |
| 110 | + const [issues, setIssues] = useState<Issue[] | null>(null); |
| 111 | + useEffect(() => { |
| 112 | + fetchIssueList({ count }).then(setIssues); |
| 113 | + }, [count, refetchKey]); |
| 114 | + if (!issues) return null; |
| 115 | + setCurrentIssues(issues); |
| 116 | + return <PlainIssueList issues={issues} limit={limit} />; |
| 117 | +} |
| 118 | + |
| 119 | +function StateListView({ |
| 120 | + state, |
| 121 | + count, |
| 122 | + limit, |
| 123 | + refetchKey, |
| 124 | +}: { |
| 125 | + state: string; |
| 126 | + count: number; |
| 127 | + limit?: number; |
| 128 | + refetchKey: number; |
| 129 | +}) { |
| 130 | + const [issues, setIssues] = useState<Issue[] | null>(null); |
| 131 | + useEffect(() => { |
| 132 | + fetchIssueList({ state, count }).then(setIssues); |
| 133 | + }, [state, count, refetchKey]); |
| 134 | + if (!issues) return null; |
| 135 | + return ( |
| 136 | + <div data-state-list={state}> |
| 137 | + <span data-state-count>{issues.length}</span> |
| 138 | + <PlainIssueList issues={issues} limit={limit} /> |
| 139 | + </div> |
| 140 | + ); |
| 141 | +} |
| 142 | + |
| 143 | +function DoubleListView({ |
| 144 | + count, |
| 145 | + limit, |
| 146 | + refetchKey, |
| 147 | +}: { |
| 148 | + count: number; |
| 149 | + limit?: number; |
| 150 | + refetchKey: number; |
| 151 | +}) { |
| 152 | + return ( |
| 153 | + <div style={DOUBLE_LIST_STYLE}> |
| 154 | + <StateListView |
| 155 | + state="open" |
| 156 | + count={count} |
| 157 | + limit={limit} |
| 158 | + refetchKey={refetchKey} |
| 159 | + /> |
| 160 | + <StateListView |
| 161 | + state="closed" |
| 162 | + count={count} |
| 163 | + limit={limit} |
| 164 | + refetchKey={refetchKey} |
| 165 | + /> |
| 166 | + </div> |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function BenchmarkHarness() { |
| 171 | + const [refetchKey, setRefetchKey] = useState(0); |
| 172 | + const triggerRefetch = useCallback(() => setRefetchKey(k => k + 1), []); |
| 173 | + |
| 174 | + const { |
| 175 | + listViewCount, |
| 176 | + showSortedView, |
| 177 | + showDoubleList, |
| 178 | + doubleListCount, |
| 179 | + detailIssueNumber, |
| 180 | + pinnedNumbers, |
| 181 | + renderLimit, |
| 182 | + containerRef, |
| 183 | + measureUpdate, |
| 184 | + registerAPI, |
| 185 | + } = useBenchState(); |
| 186 | + |
| 187 | + const updateEntity = useCallback( |
| 188 | + (number: number) => { |
| 189 | + const issue = FIXTURE_ISSUES_BY_NUMBER.get(number); |
| 190 | + if (!issue) return; |
| 191 | + measureUpdate(() => |
| 192 | + updateIssue({ |
| 193 | + number, |
| 194 | + title: `${issue.title} (updated)`, |
| 195 | + }).then(triggerRefetch), |
| 196 | + ); |
| 197 | + }, |
| 198 | + [measureUpdate, triggerRefetch], |
| 199 | + ); |
| 200 | + |
| 201 | + const updateUser = useCallback( |
| 202 | + (login: string) => { |
| 203 | + const user = FIXTURE_USERS_BY_LOGIN.get(login); |
| 204 | + if (!user) return; |
| 205 | + measureUpdate(() => |
| 206 | + serverUpdateUser({ |
| 207 | + login, |
| 208 | + name: `${user.name} (updated)`, |
| 209 | + }).then(triggerRefetch), |
| 210 | + ); |
| 211 | + }, |
| 212 | + [measureUpdate, triggerRefetch], |
| 213 | + ); |
| 214 | + |
| 215 | + const unshiftItem = useCallback(() => { |
| 216 | + const user = FIXTURE_USERS[0]; |
| 217 | + measureUpdate(() => |
| 218 | + createIssue({ title: 'New Issue', user }).then(triggerRefetch), |
| 219 | + ); |
| 220 | + }, [measureUpdate, triggerRefetch]); |
| 221 | + |
| 222 | + const deleteEntity = useCallback( |
| 223 | + (number: number) => { |
| 224 | + measureUpdate(() => deleteIssue({ number }).then(triggerRefetch)); |
| 225 | + }, |
| 226 | + [measureUpdate, triggerRefetch], |
| 227 | + ); |
| 228 | + |
| 229 | + const moveItem = useCallback( |
| 230 | + (number: number) => { |
| 231 | + measureUpdate( |
| 232 | + () => updateIssue({ number, state: 'closed' }).then(triggerRefetch), |
| 233 | + () => moveItemIsReady(containerRef, number), |
| 234 | + ); |
| 235 | + }, |
| 236 | + [measureUpdate, triggerRefetch, containerRef], |
| 237 | + ); |
| 238 | + |
| 239 | + const updateEntityMultiView = useCallback( |
| 240 | + (number: number) => { |
| 241 | + const issue = FIXTURE_ISSUES_BY_NUMBER.get(number); |
| 242 | + if (!issue) return; |
| 243 | + const expected = `${issue.title} (updated)`; |
| 244 | + measureUpdate( |
| 245 | + () => updateIssue({ number, title: expected }).then(triggerRefetch), |
| 246 | + () => { |
| 247 | + const container = containerRef.current!; |
| 248 | + const listTitle = container.querySelector( |
| 249 | + `[data-issue-number="${number}"] [data-title]`, |
| 250 | + ); |
| 251 | + const detailTitle = container.querySelector( |
| 252 | + '[data-detail-view] [data-title]', |
| 253 | + ); |
| 254 | + const pinnedTitle = container.querySelector( |
| 255 | + `[data-pinned-number="${number}"] [data-title]`, |
| 256 | + ); |
| 257 | + return [listTitle, detailTitle, pinnedTitle].every( |
| 258 | + el => el?.textContent === expected, |
| 259 | + ); |
| 260 | + }, |
| 261 | + ); |
| 262 | + }, |
| 263 | + [measureUpdate, triggerRefetch, containerRef], |
| 264 | + ); |
| 265 | + |
| 266 | + registerAPI({ |
| 267 | + updateEntity, |
| 268 | + updateUser, |
| 269 | + updateEntityMultiView, |
| 270 | + unshiftItem, |
| 271 | + deleteEntity, |
| 272 | + moveItem, |
| 273 | + }); |
| 274 | + |
| 275 | + return ( |
| 276 | + <div ref={containerRef} data-bench-harness> |
| 277 | + {listViewCount != null && ( |
| 278 | + <ListView |
| 279 | + count={listViewCount} |
| 280 | + limit={renderLimit} |
| 281 | + refetchKey={refetchKey} |
| 282 | + /> |
| 283 | + )} |
| 284 | + {showSortedView && ( |
| 285 | + <SortedListView limit={renderLimit} refetchKey={refetchKey} /> |
| 286 | + )} |
| 287 | + {showDoubleList && doubleListCount != null && ( |
| 288 | + <DoubleListView |
| 289 | + count={doubleListCount} |
| 290 | + limit={renderLimit} |
| 291 | + refetchKey={refetchKey} |
| 292 | + /> |
| 293 | + )} |
| 294 | + {detailIssueNumber != null && ( |
| 295 | + <DetailView number={detailIssueNumber} refetchKey={refetchKey} /> |
| 296 | + )} |
| 297 | + {pinnedNumbers.length > 0 && ( |
| 298 | + <PinnedStrip numbers={pinnedNumbers} refetchKey={refetchKey} /> |
| 299 | + )} |
| 300 | + </div> |
| 301 | + ); |
| 302 | +} |
| 303 | + |
| 304 | +renderBenchApp(BenchmarkHarness); |
0 commit comments