~bigbes/lethe

ref: 859d3fd879fffd81f7f72b5511e9fec061e3288d lethe/web/src/features/home/useHomeCursor.ts -rw-r--r-- 820 bytes
859d3fd8 — Eugene Blikh auth: lift oidc test stub into internal/testutil/oidcstub a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useState, useCallback } from 'react'

export interface HomeCursor {
  cursor: number
  move(d: 1 | -1): void
  activate(): void
  jumpTo(i: number): void
}

export function useHomeCursor(
  sessionCount: number,
  onActivate: (idx: number) => void,
): HomeCursor {
  const [cursor, setCursor] = useState(0)

  const move = useCallback(
    (d: 1 | -1) => {
      if (sessionCount === 0) return
      setCursor(c => Math.max(0, Math.min(sessionCount - 1, c + d)))
    },
    [sessionCount],
  )

  const activate = useCallback(() => {
    onActivate(cursor)
  }, [cursor, onActivate])

  const jumpTo = useCallback(
    (i: number) => {
      if (sessionCount === 0) return
      setCursor(Math.max(0, Math.min(sessionCount - 1, i)))
    },
    [sessionCount],
  )

  return { cursor, move, activate, jumpTo }
}