-
Notifications
You must be signed in to change notification settings - Fork 516
Expand file tree
/
Copy pathuseViewMode.ts
More file actions
44 lines (39 loc) · 1.12 KB
/
useViewMode.ts
File metadata and controls
44 lines (39 loc) · 1.12 KB
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
38
39
40
41
42
43
44
import flagsmith from '@flagsmith/flagsmith'
import { useState, useCallback } from 'react'
export type ViewMode =
| 'compact'
| 'default'
| 'release-manager'
| 'executive'
| 'dev'
export function getViewMode() {
const viewMode = flagsmith.getTrait('view_mode')
if (
viewMode === 'compact' ||
viewMode === 'release-manager' ||
viewMode === 'executive' ||
viewMode === 'dev'
) {
return viewMode as ViewMode
}
return 'default'
}
export function setViewMode(viewMode: ViewMode) {
return flagsmith.setTrait('view_mode', viewMode)
}
/**
* Hook for managing view mode with optimistic UI updates.
* Updates state immediately for instant feedback, then persists to Flagsmith in background.
*/
export function useViewMode() {
const [viewMode, setViewModeState] = useState<ViewMode>(getViewMode)
const updateViewMode = useCallback((value: ViewMode) => {
setViewModeState(value) // Optimistic update - instant UI change
setViewMode(value) // Persist to Flagsmith trait in background
}, [])
return {
isCompact: viewMode === 'compact',
setViewMode: updateViewMode,
viewMode,
}
}