react-use#createMemo TypeScript Examples

The following examples show how to use react-use#createMemo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DistributionBar.tsx    From mStable-apps with GNU Lesser General Public License v3.0 6 votes vote down vote up
useScaledDialVotes = createMemo((dialVotes?: EpochDialVotes): [{ [dialId: number]: number }] => {
  if (!dialVotes) return [{}]

  const totalVotes = Object.values(dialVotes).reduce((prev, dialVote) => prev + dialVote.votes, 0)

  // TODO improve; >100 total breaks the chart
  const weightMultiplier = 99.9 / totalVotes

  const entries = Object.entries(dialVotes).map(([dialId, dialVote]) => [dialId, dialVote.votes * weightMultiplier])

  return [Object.fromEntries(entries)]
})
Example #2
Source File: useScaledInputs.ts    From mStable-apps with GNU Lesser General Public License v3.0 6 votes vote down vote up
useScaledInputs = createMemo((inputValues: BigDecimalInputValues, inputRatios: InputRatios): ScaledInputs => {
  const touched = Object.values(inputValues).filter(value => value.touched && value.amount.exact.gt(0))

  const highAmounts = touched.map(value => value.amount)
  const highTotal = BigDecimal.sum(...highAmounts)

  const scaledHighAmounts = touched.map(({ amount, address }) =>
    // Only scale with a ratio; massets are not scaled
    inputRatios[address] ? amount.mulRatioTruncate(inputRatios[address]).setDecimals(18) : amount,
  )
  const scaledHighTotal = BigDecimal.sum(...scaledHighAmounts)

  return touched.reduce<ScaledInputs>(
    (prev, { amount: high, address }, idx) => {
      const scaledHigh = scaledHighAmounts[idx]

      const low = high.divPrecisely(highTotal)
      const lowTotal = prev.lowTotal.add(low)

      const scaledLow = scaledHigh.divPrecisely(highTotal)
      const scaledLowTotal = prev.scaledLowTotal.add(scaledLow)

      return {
        ...prev,
        lowTotal,
        scaledLowTotal,
        values: { ...prev.values, [address]: { low, high, scaledLow, scaledHigh } },
      } as ScaledInputs
    },
    { highTotal, scaledHighTotal, scaledLowTotal: BigDecimal.ZERO, lowTotal: BigDecimal.ZERO, values: {} } as ScaledInputs,
  )
})
Example #3
Source File: DialTable.tsx    From mStable-apps with GNU Lesser General Public License v3.0 5 votes vote down vote up
useScaleUserDialPreferences = createMemo(
  ({
    current,
    changes,
  }: {
    current: UserDialPreferences
    changes: UserDialPreferences
  }): { scaled: UserDialPreferences; pending: UserDialPreferences } => {
    // Combine changes and current dial preferences
    const pending = Object.fromEntries(
      ALL_POSSIBLE_DIAL_IDS.filter(dialId => changes[dialId] ?? current[dialId]).map(dialId => [
        dialId,
        changes[dialId] ?? current[dialId] ?? 0,
      ]),
    )

    // Since weights might not total 100, get a multiplier to scale them
    const totalWeight = Object.values(pending).reduce((a, b) => a + b, 0)
    const weightMultiplier = totalWeight > 0 ? 100 / totalWeight : 1

    const dialIds = Object.keys(pending)
    const scaledWeights = dialIds.map(dialId => pending[dialId] * weightMultiplier)

    let scaled = Object.fromEntries(dialIds.map((dialId, idx) => [dialId, scaledWeights[idx]]))

    if (totalWeight === 0) return { pending, scaled }

    // The remainder gets added to the highest value
    const remainder = 100 - scaledWeights.reduce((prev, weight) => prev + weight, 0)
    if (remainder > 0) {
      const maxValue = Math.max(...scaledWeights)

      scaled = Object.fromEntries(
        dialIds.map((dialId, idx) => {
          const weight = scaledWeights[idx]
          return [dialId, weight === maxValue ? weight + remainder : weight]
        }),
      )
    }

    return { scaled, pending }
  },
)