Skip to content

Commit

Permalink
allow passed data, widen param type
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Mar 27, 2024
1 parent 1e99747 commit 03a07a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 6 additions & 2 deletions packages/ui/components/data-display/Hours.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createStyles, List, rem, Skeleton, Stack, Table, Text, Title } from '@m
import { Interval } from 'luxon'
import { useTranslation } from 'next-i18next'

import { type ApiOutput } from '@weareinreach/api'
import { HoursDrawer } from '~ui/components/data-portal/HoursDrawer'
import { useCustomVariant } from '~ui/hooks/useCustomVariant'
import { useLocalizedDays } from '~ui/hooks/useLocalizedDays'
Expand Down Expand Up @@ -39,11 +40,13 @@ const nullObj = {
6: [],
}

export const Hours = ({ parentId, label = 'regular', edit }: HoursProps) => {
export const Hours = ({ parentId, label = 'regular', edit, data: passedData }: HoursProps) => {
const { t, i18n } = useTranslation('common')
const variants = useCustomVariant()
const { classes } = useStyles()
const { data, isLoading } = api.orgHours.forHoursDisplay.useQuery(parentId)
const { data, isLoading } = passedData
? { data: passedData, isLoading: false }
: api.orgHours.forHoursDisplay.useQuery(parentId)
const dayMap = useLocalizedDays(i18n.resolvedLanguage)
if (!data && !isLoading) return null

Expand Down Expand Up @@ -106,4 +109,5 @@ export interface HoursProps {
parentId: string
label?: keyof typeof labelKeys
edit?: boolean
data?: ApiOutput['orgHours']['forHoursDisplay']
}
17 changes: 13 additions & 4 deletions packages/ui/hooks/useFreeText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ import { useTranslation } from 'next-i18next'

import { type DB } from '@weareinreach/api/prisma/types'

const isNestedFreeText = (item: unknown): item is NestedFreeText => {
if (!item || typeof item !== 'object') return false
if ('tsKey' in item) return true
return false
}

export const getFreeText: GetFreeText = (freeTextRecord, tOptions) => {
const { key: dbKey, tsKey } = freeTextRecord
const { key: dbKey, tsKey } = isNestedFreeText(freeTextRecord)
? freeTextRecord
: { key: freeTextRecord.key, tsKey: { text: freeTextRecord.text } }
const deconstructedKey = dbKey.split('.')
const ns = deconstructedKey[0]
if (!deconstructedKey.length || !ns) throw new Error('Invalid key')
Expand All @@ -20,13 +28,14 @@ export const useFreeText: UseFreeText = (freeTextRecord, tOptions) => {
return t(key, options)
}

export interface UseFreeTextProps extends Pick<DB.FreeText, 'key'>, Partial<Omit<DB.FreeText, 'key'>> {
export interface NestedFreeText extends Pick<DB.FreeText, 'key'>, Partial<Omit<DB.FreeText, 'key'>> {
tsKey: Pick<DB.TranslationKey, 'text'> & Partial<Omit<DB.TranslationKey, 'text'>>
}
export type TranslationKeyRecord = Pick<DB.TranslationKey, 'key' | 'ns' | 'text'>

export type GetFreeText = (
freeTextRecord: UseFreeTextProps,
freeTextRecord: NestedFreeText | TranslationKeyRecord,
tOptions?: TOptions
) => { key: string; options: TOptions }

export type UseFreeText = (freeTextRecord: UseFreeTextProps, tOptions?: TOptions) => string
export type UseFreeText = (freeTextRecord: NestedFreeText, tOptions?: TOptions) => string

0 comments on commit 03a07a0

Please sign in to comment.