File size: 2,560 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { capitalize } from './strings'

interface CodesandboxSandbox {
  title: string
  tags: string[]
  screenshot_url: string
  description: string
  id: string
}

type CodesandboxSandboxResponse = {
  data: CodesandboxSandbox
}

export interface CodesandboxSandboxFetched
  extends Omit<CodesandboxSandbox, 'screenshot_url'> {
  screenshotUrl: string
  urlTitle: string
}

const hookRegex = new RegExp(/^use/)

export const fetchSandbox = async (
  id: string
): Promise<CodesandboxSandboxFetched> => {
  const sandbox = await fetch(`https://codesandbox.io/api/v1/sandboxes/${id}`, {
    cache: 'force-cache',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then(res => {
      if (res.status === 200) {
        return res.json()
      } else {
        throw new Error('Bad response from codesandbox')
      }
    })
    .then((res: CodesandboxSandboxResponse) => {
      const { data } = res

      const { id, title, tags, screenshot_url, description } = data ?? {}

      return {
        id,
        urlTitle: title,
        title,
        tags: tags.map(tag => {
          if (hookRegex.test(tag)) {
            const stringSplit = tag.split('use')

            if (tag === 'usespringvalue') {
              return `useSpringValue`
            } else if (tag === 'usespringref') {
              return `useSpringRef`
            }

            return `use${capitalize(stringSplit[1])}`
          } else if (tag === 'parallax') {
            return capitalize(tag)
          }
          return tag
        }),
        screenshotUrl: screenshot_url,
        description,
      }
    })

  return sandbox
}

interface SelectData {
  value: string
  label: string
}

type TagsAndComponents = [tags: SelectData[], components: SelectData[]]

const components = [
  'useSpring',
  'useSprings',
  'useTrail',
  'useChain',
  'useTransition',
  'Parallax',
  'useSpringValue',
  'useSpringRef',
]

export const getTagsAndComponents = (
  sandboxes: CodesandboxSandboxFetched[]
): TagsAndComponents =>
  sandboxes
    .reduce<[tags: string[], components: string[]]>(
      (acc, sandbox) => {
        sandbox.tags.forEach(tag => {
          if (components.includes(tag)) {
            acc[1].push(tag)
          } else {
            acc[0].push(tag)
          }
        })

        return acc
      },
      [[], []]
    )
    .map(arr =>
      arr
        .filter((val, ind, self) => self.indexOf(val) === ind)
        .sort()
        .map(tag => ({
          value: tag,
          label: tag,
        }))
    ) as TagsAndComponents