File size: 2,008 Bytes
d69879c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { create } from 'zustand'
import type { ClosestLandmark } from './useFaceLandmarkDetection'
import type { ImageModificationParams } from '@/lib/facePoke'

interface ImageState {
  error: string
  imageFile: File | null
  originalImage: string
  previewImage: string
  originalImageHash: string
  minLatency: number
  averageLatency: number
  maxLatency: number
  activeLandmark?: ClosestLandmark
  params: Partial<ImageModificationParams>
  setError: (error?: string) => void
  setImageFile: (file: File | null) => void
  setOriginalImage: (url: string) => void
  setOriginalImageHash: (hash: string) => void
  setPreviewImage: (url: string) => void
  resetImage: () => void
  setAverageLatency: (averageLatency: number) => void
  setActiveLandmark: (activeLandmark?: ClosestLandmark) => void
  setParams: (params: Partial<ImageModificationParams>) => void
}

export const useMainStore = create<ImageState>((set, get) => ({
  error: '',
  imageFile: null,
  originalImage: '',
  originalImageHash: '',
  previewImage: '',
  minLatency: 20, // min time between requests
  averageLatency: 190, // this should be the average for most people
  maxLatency: 4000, // max time between requests
  activeLandmark: undefined,
  params: {},
  setError: (error: string = '') => set({ error }),
  setImageFile: (file) => set({ imageFile: file }),
  setOriginalImage: (url) => set({ originalImage: url }),
  setOriginalImageHash: (originalImageHash) => set({ originalImageHash }),
  setPreviewImage: (url) => set({ previewImage: url }),
  resetImage: () => {
    const { originalImage } = get()
    if (originalImage) {
      set({ previewImage: originalImage })
    }
  },
  setAverageLatency: (averageLatency: number) => set({ averageLatency }),
  setActiveLandmark: (activeLandmark?: ClosestLandmark) => set({ activeLandmark }),
  setParams: (params: Partial<ImageModificationParams>) => {
    const {params: previousParams } = get()
    set({ params: {
      ...previousParams,
      ...params
    }})
  },
}))