|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { id } from '@instantdb/react'; |
| 3 | +import clientDB from '@/lib/feedback/clientDB'; |
| 4 | +import { BlockHeading, Button, ToggleGroup } from '@/components/ui'; |
| 5 | +import { Rating } from '@/lib/feedback/instant.schema'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A handy component to collect feedback for a particular page. |
| 9 | + * |
| 10 | + * Asks "Was this page helpful?", and lets the user |
| 11 | + * provide more details if they want too. |
| 12 | + * |
| 13 | + * No login required! |
| 14 | + */ |
| 15 | +export default function RatingBoxContainer({ pageId }: { pageId: string }) { |
| 16 | + const localId = clientDB.useLocalId('feedback'); |
| 17 | + |
| 18 | + const { isLoading, error, data } = clientDB.useQuery( |
| 19 | + localId |
| 20 | + ? { |
| 21 | + ratings: { |
| 22 | + $: { |
| 23 | + where: { pageId, localId }, |
| 24 | + }, |
| 25 | + }, |
| 26 | + } |
| 27 | + : null, |
| 28 | + { ruleParams: { localId } }, |
| 29 | + ); |
| 30 | + |
| 31 | + if (!localId || isLoading || error) return null; |
| 32 | + |
| 33 | + return ( |
| 34 | + <RatingBox |
| 35 | + localId={localId} |
| 36 | + pageId={pageId} |
| 37 | + previousRating={data.ratings[0]} |
| 38 | + /> |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function RatingBox({ |
| 43 | + pageId, |
| 44 | + localId, |
| 45 | + previousRating, |
| 46 | +}: { |
| 47 | + pageId: string; |
| 48 | + localId: string; |
| 49 | + previousRating?: Rating; |
| 50 | +}) { |
| 51 | + // The first time you rate, let's auto-focus the 'details' textarea |
| 52 | + // so you can quickly add more details if you want to. |
| 53 | + const [shouldAutoFocus, setShouldAutoFocus] = useState(false); |
| 54 | + |
| 55 | + const selectedId = previousRating |
| 56 | + ? previousRating.wasHelpful |
| 57 | + ? 'yes' |
| 58 | + : 'no' |
| 59 | + : undefined; |
| 60 | + return ( |
| 61 | + <div className="space-y-2"> |
| 62 | + <div className="inline-flex space-x-4 items-center"> |
| 63 | + <BlockHeading>Was this page helpful?</BlockHeading> |
| 64 | + <div className="w-20"> |
| 65 | + <ToggleGroup |
| 66 | + selectedId={selectedId} |
| 67 | + onChange={(item) => { |
| 68 | + if (!previousRating) { |
| 69 | + setShouldAutoFocus(true); |
| 70 | + } |
| 71 | + clientDB.transact( |
| 72 | + clientDB.tx.ratings[previousRating?.id ?? id()] |
| 73 | + .ruleParams({ localId }) |
| 74 | + .update({ |
| 75 | + pageId, |
| 76 | + localId, |
| 77 | + key: `${localId}_${pageId}`, |
| 78 | + wasHelpful: 'yes' === item.id, |
| 79 | + }), |
| 80 | + ); |
| 81 | + }} |
| 82 | + items={[ |
| 83 | + { id: 'yes', label: 'Yes' }, |
| 84 | + { id: 'no', label: 'No' }, |
| 85 | + ]} |
| 86 | + /> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + {previousRating && selectedId && ( |
| 90 | + <div className="space-y-2"> |
| 91 | + <p>Thank you for your feedback! More details to share?</p> |
| 92 | + <SavingTextArea |
| 93 | + savedValue={previousRating.extraComment || ''} |
| 94 | + onSave={(extraComment) => { |
| 95 | + clientDB.transact( |
| 96 | + clientDB.tx.ratings[previousRating.id] |
| 97 | + .ruleParams({ localId }) |
| 98 | + .update({ extraComment }), |
| 99 | + ); |
| 100 | + }} |
| 101 | + placeholder="Tell us more about your experience..." |
| 102 | + autoFocus={shouldAutoFocus} |
| 103 | + /> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + ); |
| 108 | +} |
| 109 | + |
| 110 | +// ---------- |
| 111 | +// Components |
| 112 | + |
| 113 | +type SavingTextAreaProps = { |
| 114 | + savedValue: string; |
| 115 | + onSave: (value: string) => void; |
| 116 | +} & Omit< |
| 117 | + React.TextareaHTMLAttributes<HTMLTextAreaElement>, |
| 118 | + 'value' | 'onChange' | 'onKeyDown' |
| 119 | +>; |
| 120 | + |
| 121 | +/** |
| 122 | + * A handy textarea that lets you save a value. |
| 123 | + * If the incoming `savedValue` changes, we'll update the textarea. |
| 124 | + * We'll _skip_ the update if you are focused on the input |
| 125 | + * and in the middle of making a change though! :) |
| 126 | + */ |
| 127 | +function SavingTextArea({ savedValue, onSave, ...props }: SavingTextAreaProps) { |
| 128 | + const textAreaRef = useRef<HTMLTextAreaElement>(null); |
| 129 | + const [value, setValue] = useState(savedValue); |
| 130 | + useEffect(() => { |
| 131 | + const ref = textAreaRef.current!; |
| 132 | + const isEditing = ref === document.activeElement; |
| 133 | + if (isEditing) return; |
| 134 | + setValue(savedValue); |
| 135 | + }, [savedValue]); |
| 136 | + return ( |
| 137 | + <div className="space-y-1"> |
| 138 | + <textarea |
| 139 | + {...props} |
| 140 | + value={value} |
| 141 | + className="flex w-full flex-1 rounded-sm border-gray-200 bg-white px-3 py-1 placeholder:text-gray-400 disabled:text-gray-400" |
| 142 | + onChange={(e) => { |
| 143 | + setValue(e.target.value); |
| 144 | + }} |
| 145 | + onKeyDown={(e) => { |
| 146 | + if (e.key === 'Enter' && e.metaKey) { |
| 147 | + onSave(value); |
| 148 | + } |
| 149 | + }} |
| 150 | + /> |
| 151 | + <div className="text-right"> |
| 152 | + <Button |
| 153 | + disabled={value === savedValue} |
| 154 | + onClick={() => { |
| 155 | + onSave(value); |
| 156 | + }} |
| 157 | + size="mini" |
| 158 | + type="submit" |
| 159 | + > |
| 160 | + {value && value === savedValue ? 'Saved!' : 'Save'} |
| 161 | + </Button> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + ); |
| 165 | +} |
0 commit comments