๐Ÿ“ฆ payloadcms / plugin-seo

๐Ÿ“„ LengthIndicator.tsx ยท 132 lines
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'use client'

/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-use-before-define */
import React, { Fragment, useEffect, useState } from 'react'

import { Pill } from './Pill'

export const LengthIndicator: React.FC<{
  text?: string
  minLength?: number
  maxLength?: number
}> = props => {
  const { text, minLength = 0, maxLength = 0 } = props

  const [labelStyle, setLabelStyle] = useState({
    color: '',
    backgroundColor: '',
  })

  const [label, setLabel] = useState('')
  const [barWidth, setBarWidth] = useState<number>(0)

  useEffect(() => {
    const textLength = text?.length || 0

    if (textLength === 0) {
      setLabel('Missing')
      setLabelStyle({
        backgroundColor: 'red',
        color: 'white',
      })
      setBarWidth(0)
    } else {
      const progress = (textLength - minLength) / (maxLength - minLength)

      if (progress < 0) {
        const ratioUntilMin = textLength / minLength

        if (ratioUntilMin > 0.9) {
          setLabel('Almost there')
          setLabelStyle({
            backgroundColor: 'orange',
            color: 'white',
          })
        } else {
          setLabel('Too short')
          setLabelStyle({
            backgroundColor: 'orangered',
            color: 'white',
          })
        }

        setBarWidth(ratioUntilMin)
      }

      if (progress >= 0 && progress <= 1) {
        setLabel('Good')
        setLabelStyle({
          backgroundColor: 'green',
          color: 'white',
        })
        setBarWidth(progress)
      }

      if (progress > 1) {
        setLabel('Too long')
        setLabelStyle({
          backgroundColor: 'red',
          color: 'white',
        })
        setBarWidth(1)
      }
    }
  }, [minLength, maxLength, text])

  const textLength = text?.length || 0

  const charsUntilMax = maxLength - textLength
  const charsUntilMin = minLength - textLength

  return (
    <div
      style={{
        width: '100%',
        display: 'flex',
        alignItems: 'center',
      }}
    >
      <Pill label={label} color={labelStyle.color} backgroundColor={labelStyle.backgroundColor} />
      <div
        style={{
          marginRight: '10px',
          whiteSpace: 'nowrap',
          flexShrink: 0,
          lineHeight: 1,
        }}
      >
        <small>
          {`${text?.length || 0}/${minLength}-${maxLength} chars, `}
          {(textLength === 0 || charsUntilMin > 0) && (
            <Fragment>{`${charsUntilMin} to go`}</Fragment>
          )}
          {charsUntilMin <= 0 && charsUntilMax >= 0 && (
            <Fragment>{`${charsUntilMax} left over`}</Fragment>
          )}
          {charsUntilMax < 0 && <Fragment>{`${charsUntilMax * -1} too many`}</Fragment>}
        </small>
      </div>
      <div
        style={{
          height: '2px',
          width: '100%',
          backgroundColor: '#F3F3F3',
          position: 'relative',
        }}
      >
        <div
          style={{
            height: '100%',
            width: `${barWidth * 100}%`,
            backgroundColor: labelStyle.backgroundColor,
            position: 'absolute',
            left: 0,
            top: 0,
          }}
        />
      </div>
    </div>
  )
}