Ride Feedback
Post-ride feedback with a deliberate rating, optional contextual tags and comment, and guarded async submission.
Install
First install? Set up the registry in your app.
npx --yes --package=https://native.uipkge.dev/cli/uipkge-native-0.1.1.tgz uipkge-native add ride-feedbackSource files
Expand a file to read the source the installer copies into your app.
components/blocks/ride-feedback/RideFeedback.tsx
import { useEffect, useRef, useState, type ReactNode } from 'react'
import { View, type ViewProps } from 'react-native'
import { Button } from '@/components/ui/button'
import { FormField } from '@/components/ui/form-field'
import { Rating } from '@/components/ui/rating'
import { Text } from '@/components/ui/text'
import { Textarea } from '@/components/ui/textarea'
import { useAnnouncement } from '@/lib/accessibility'
import { cn } from '@/lib/utils'
export interface RideFeedbackValue {
rating: number
tags: readonly string[]
comment: string
}
export interface RideFeedbackTagControls {
rating: number
tags: readonly string[]
/** Toggle a stable caller-owned tag ID. */
toggleTag: (id: string) => void
disabled: boolean
}
const defaultLabels = {
rating: 'Overall rating',
star: (value: number, max: number) => `${value} of ${max} stars`,
tags: 'What stood out?',
comment: 'Comment (optional)',
submit: 'Submit feedback',
submitting: 'Submitting feedback…',
submitted: 'Feedback submitted.',
failed: 'Feedback could not be submitted. Try again.',
}
type Submission = 'idle' | 'submitting' | 'submitted' | 'error'
export interface RideFeedbackProps extends Omit<ViewProps, 'children'> {
className?: string
/** Resolve only when the submission is accepted. Reject to preserve the draft for retry. */
onSubmit: (value: RideFeedbackValue) => void | Promise<void>
/** Tags appear after rating. Changing the rating clears tags so obsolete context is never submitted. */
renderTags?: (controls: RideFeedbackTagControls) => ReactNode
showComment?: boolean
disabled?: boolean
labels?: Partial<typeof defaultLabels>
}
export function RideFeedback({
onSubmit,
renderTags,
showComment = true,
disabled = false,
labels,
className,
...props
}: RideFeedbackProps) {
const copy = { ...defaultLabels, ...labels }
const [rating, setRating] = useState(0)
const [tags, setTags] = useState<readonly string[]>([])
const [comment, setComment] = useState('')
const [status, setStatus] = useState<Submission>('idle')
const submission = useRef<Submission>('idle')
const mounted = useRef(true)
const blocked = disabled || status === 'submitting' || status === 'submitted'
const message = status === 'submitted' ? copy.submitted : status === 'error' ? copy.failed : undefined
useAnnouncement(message)
useEffect(() => {
mounted.current = true
return () => {
mounted.current = false
}
}, [])
function updateSubmission(next: Submission) {
submission.current = next
if (mounted.current) setStatus(next)
}
function clearError() {
if (submission.current === 'error') updateSubmission('idle')
}
function editBlocked() {
return disabled || submission.current === 'submitting' || submission.current === 'submitted'
}
async function submit() {
if (rating === 0 || editBlocked()) return
updateSubmission('submitting')
try {
await onSubmit({
rating,
tags: renderTags ? tags : [],
comment: showComment ? Array.from(comment.trim()).slice(0, 280).join('') : '',
})
updateSubmission('submitted')
} catch {
updateSubmission('error')
}
}
const tagControls =
rating > 0
? renderTags?.({
rating,
tags,
disabled: blocked,
toggleTag(id) {
if (editBlocked() || !id.trim()) return
setTags((current) => (current.includes(id) ? current.filter((tag) => tag !== id) : [...current, id]))
clearError()
},
})
: null
return (
<View {...props} className={cn('gap-[16px]', className)}>
<View className="gap-[4px] rounded-lg bg-muted p-[16px]">
<Text className="font-semibold">{copy.rating}</Text>
<Rating
accessibilityLabel={copy.rating}
getValueLabel={copy.star}
value={rating}
disabled={blocked}
onValueChange={(value) => {
if (editBlocked()) return
setRating(value)
setTags([])
clearError()
}}
/>
</View>
{tagControls ? (
<View className="gap-[8px]">
<Text className="font-semibold">{copy.tags}</Text>
<View className="flex-row flex-wrap gap-[8px]">{tagControls}</View>
</View>
) : null}
{showComment ? (
<FormField label={copy.comment}>
{(field) => (
<Textarea
{...field}
value={comment}
editable={!blocked}
maxLength={280}
onChangeText={(value) => {
if (editBlocked()) return
setComment(value)
clearError()
}}
/>
)}
</FormField>
) : null}
{message ? (
<Text
accessibilityRole={status === 'error' ? 'alert' : undefined}
className={status === 'error' ? 'text-destructive' : 'text-success'}
>
{message}
</Text>
) : null}
{status !== 'submitted' ? (
<Button
accessibilityLabel={status === 'submitting' ? copy.submitting : copy.submit}
disabled={disabled || rating === 0}
loading={status === 'submitting'}
onPress={() => void submit()}
>
{status === 'submitting' ? copy.submitting : copy.submit}
</Button>
) : null}
</View>
)
}
components/blocks/ride-feedback/index.ts
export {
RideFeedback,
type RideFeedbackProps,
type RideFeedbackTagControls,
type RideFeedbackValue,
} from './RideFeedback'