Socialify

Folder ..

Viewing config.tsx
248 lines (225 loc) • 7.1 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
 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React, { useContext, useEffect } from 'react'
import { useRouter } from 'next/router'

import { RepoQueryResponse } from '../../../common/github/repoQuery'
import ConfigContext from '../../contexts/ConfigContext'

import ConfigType, {
  Theme,
  Pattern,
  Font,
  RequiredConfigsKeys
} from '../../../common/types/configType'

import { getOptionalConfig } from '../../../common/configHelper'

import SelectWrapper from './selectWrapper'
import CheckBoxWrapper from './checkBoxWrapper'
import InputWrapper from './inputWrapper'
import TextAreaWrapper from './textAreaWrapper'

type ConfigProp = {
  repository: RepoQueryResponse['repository']
}

const Config = ({ repository }: ConfigProp) => {
  const router = useRouter()

  const { config, setConfig } = useContext(ConfigContext)

  const handleChanges = (changes: { value: any; key: keyof ConfigType }[]) => {
    let newConfig: ConfigType = { ...config }
    const urlParams = router.query
    // Remove extraneous params from route
    delete urlParams._owner
    delete urlParams._name
    changes.forEach(({ value, key }) => {
      const currentValue = newConfig[key] ? newConfig[key] : {}
      if (value.required === true) {
        newConfig = { ...newConfig, [key]: value.val }
      } else {
        newConfig = { ...newConfig, [key]: { ...currentValue, ...value } }
      }

      if (value && value.state === true && value.editable) {
        urlParams[key] = '1'
        urlParams[`${key}Editable`] = value.value
      } else if (value && value.state === true) {
        urlParams[key] = '1'
      } else if (value && value.required === true) {
        urlParams[key] = value.val
      } else {
        urlParams[key] = '0'
      }

      if (!urlParams[key] || urlParams[key] === '0') {
        delete urlParams[key]
        if (`${key}Editable` in urlParams) {
          delete urlParams[`${key}Editable`]
        }
      }
    })

    router.push(
      `${window.location.pathname}?${Object.entries(urlParams)
        .sort()
        .map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`)
        .join('&')}`,
      undefined,
      { shallow: true }
    )
  }

  const handleChange = (value: any, key: keyof ConfigType) => {
    handleChanges([{ value, key }])
  }

  useEffect(() => {
    const handleRouteChange = (asPath: string) => {
      if (repository) {
        const newConfig = getOptionalConfig(repository)
        if (newConfig) {
          const params = new URLSearchParams(asPath.split('?')[1])

          Array.from(params.keys()).forEach((stringKey) => {
            const key = stringKey as keyof ConfigType
            if (key in newConfig) {
              const query = params.get(key)
              const currentConfig = newConfig[key as keyof typeof newConfig]
              const newChange = {
                state: query === '1'
              }
              if (currentConfig?.editable) {
                const editableValue = params.get(`${key}Editable`)
                if (editableValue != null) {
                  Object.assign(newChange, {
                    value: editableValue
                  })
                }
              }

              Object.assign(
                newConfig[key as keyof typeof newConfig] ?? {},
                newChange
              )
            } else if (key in RequiredConfigsKeys) {
              const query = params.get(key)
              if (query != null) {
                const newChange = {
                  [key]: query
                }

                Object.assign(newConfig, newChange)
              }
            }
          })
          setConfig({ ...config, ...newConfig })
        }
      }
    }

    router.events.on('routeChangeComplete', handleRouteChange)
    handleRouteChange(router.asPath)

    return () => {
      router.events.off('routeChangeComplete', handleRouteChange)
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])

  if (!repository) {
    return null
  }

  return (
    <div className="card w-96 max-w-[90vw] bg-base-200 text-primary-content shadow-xl">
      <div className="card-body">
        <SelectWrapper
          title="Theme"
          keyName="theme"
          map={Object.keys(Theme).map((key) => ({
            key,
            label: (Theme as any)[key]
          }))}
          value={config.theme}
          handleChange={handleChange}
        />
        <SelectWrapper
          title="Font"
          keyName="font"
          map={Object.keys(Font).map((key) => ({
            key,
            label: (Font as any)[key]
          }))}
          value={config.font}
          handleChange={handleChange}
        />
        <SelectWrapper
          title="Background Pattern"
          keyName="pattern"
          map={Object.keys(Pattern).map((key) => ({
            key,
            label: (Pattern as any)[key]
          }))}
          value={config.pattern}
          handleChange={handleChange}
        />
        <InputWrapper
          title="Logo"
          alt="Image url or data uri"
          keyName="logo"
          placeholder="Optional"
          value={config.logo}
          handleChange={handleChange}
        />

        <div className="columns-2">
          <CheckBoxWrapper
            title="Name"
            keyName="name"
            checked={config.name?.state}
            handleChange={handleChange}
            disabled
          />
          <CheckBoxWrapper
            title="Owner"
            keyName="owner"
            checked={config.owner?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Language"
            keyName="language"
            checked={config.language?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="2nd Language"
            keyName="language2"
            checked={config.language2?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Stars"
            keyName="stargazers"
            checked={config.stargazers?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Forks"
            keyName="forks"
            checked={config.forks?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Issues"
            keyName="issues"
            checked={config.issues?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Pull Requests"
            keyName="pulls"
            checked={config.pulls?.state}
            handleChange={handleChange}
          />
          <CheckBoxWrapper
            title="Description"
            keyName="description"
            checked={config.description?.state}
            handleChange={handleChange}
          />
        </div>

        {config.description?.state && (
          <TextAreaWrapper
            title="Description"
            keyName="description"
            value={config.description?.value}
            handleChange={handleChange}
            disabled={!config.description?.state}
          />
        )}
      </div>
    </div>
  )
}

export default Config