|
| 1 | +import { useJson } from "Hooks/useJson"; |
| 2 | +import { useTranslation } from "i18n/hooks"; |
| 3 | + |
| 4 | +interface OptionCount { |
| 5 | + option: string; |
| 6 | + count: number; |
| 7 | +} |
| 8 | + |
| 9 | +interface QuestionTypeStats { |
| 10 | + question_type: string; |
| 11 | + total_respondents: number; |
| 12 | + options: OptionCount[]; |
| 13 | +} |
| 14 | + |
| 15 | +interface MemberPreferencesResponse { |
| 16 | + preference_statistics: Record<string, QuestionTypeStats>; |
| 17 | +} |
| 18 | + |
| 19 | +// Human-readable names for question types |
| 20 | +const QUESTION_TYPE_NAMES: Record<string, string> = { |
| 21 | + ROOM_PREFERENCE: "Room Preferences", |
| 22 | + MACHINE_PREFERENCE: "Machine Preferences", |
| 23 | + SKILL_LEVEL: "Self-Reported Skill Level", |
| 24 | +}; |
| 25 | + |
| 26 | +export default function MemberPreferencesPage() { |
| 27 | + const { t } = useTranslation("task_statistics"); |
| 28 | + |
| 29 | + const { data, isLoading, error } = useJson<MemberPreferencesResponse>({ |
| 30 | + url: `/tasks/statistics/member_preferences`, |
| 31 | + }); |
| 32 | + |
| 33 | + if (isLoading) { |
| 34 | + return ( |
| 35 | + <div className="uk-width-1-1"> |
| 36 | + <h2>{t("member_preferences.title")}</h2> |
| 37 | + <div>{t("loading")}</div> |
| 38 | + </div> |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (error) { |
| 43 | + return ( |
| 44 | + <div className="uk-width-1-1"> |
| 45 | + <h2>{t("member_preferences.title")}</h2> |
| 46 | + <div className="uk-alert-danger">{t("error_loading")}</div> |
| 47 | + </div> |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + if (!data || Object.keys(data.preference_statistics).length === 0) { |
| 52 | + return ( |
| 53 | + <div className="uk-width-1-1"> |
| 54 | + <h2>{t("member_preferences.title")}</h2> |
| 55 | + <p>{t("member_preferences.no_data")}</p> |
| 56 | + </div> |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // Sort question types for consistent display order |
| 61 | + const sortedQuestionTypes = Object.keys(data.preference_statistics).sort(); |
| 62 | + |
| 63 | + return ( |
| 64 | + <div className="uk-width-1-1"> |
| 65 | + <h2>{t("member_preferences.title")}</h2> |
| 66 | + <p className="uk-text-muted"> |
| 67 | + {t("member_preferences.description")} |
| 68 | + </p> |
| 69 | + |
| 70 | + {sortedQuestionTypes.map((questionType) => { |
| 71 | + const stats = data.preference_statistics[questionType]; |
| 72 | + if (!stats) return null; |
| 73 | + const displayName = |
| 74 | + QUESTION_TYPE_NAMES[questionType] || questionType; |
| 75 | + |
| 76 | + return ( |
| 77 | + <div key={questionType} className="uk-margin-large-bottom"> |
| 78 | + <h3>{displayName}</h3> |
| 79 | + <p className="uk-text-meta"> |
| 80 | + {t("member_preferences.total_respondents", { |
| 81 | + count: stats.total_respondents, |
| 82 | + })} |
| 83 | + </p> |
| 84 | + |
| 85 | + {stats.options.length === 0 ? ( |
| 86 | + <p className="uk-text-muted"> |
| 87 | + {t("member_preferences.no_responses")} |
| 88 | + </p> |
| 89 | + ) : ( |
| 90 | + <table className="uk-table uk-table-small uk-table-striped uk-table-hover"> |
| 91 | + <thead> |
| 92 | + <tr> |
| 93 | + <th> |
| 94 | + {t("member_preferences.option")} |
| 95 | + </th> |
| 96 | + <th style={{ width: "100px" }}> |
| 97 | + {t("member_preferences.count")} |
| 98 | + </th> |
| 99 | + <th style={{ width: "150px" }}> |
| 100 | + {t("member_preferences.percentage")} |
| 101 | + </th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + {stats.options.map((optionData) => { |
| 106 | + const percentage = |
| 107 | + stats.total_respondents > 0 |
| 108 | + ? ( |
| 109 | + (optionData.count / |
| 110 | + stats.total_respondents) * |
| 111 | + 100 |
| 112 | + ).toFixed(1) |
| 113 | + : "0"; |
| 114 | + |
| 115 | + return ( |
| 116 | + <tr key={optionData.option}> |
| 117 | + <td>{optionData.option}</td> |
| 118 | + <td>{optionData.count}</td> |
| 119 | + <td> |
| 120 | + <div |
| 121 | + style={{ |
| 122 | + display: "flex", |
| 123 | + alignItems: |
| 124 | + "center", |
| 125 | + gap: "8px", |
| 126 | + }} |
| 127 | + > |
| 128 | + <div |
| 129 | + style={{ |
| 130 | + width: "80px", |
| 131 | + height: "16px", |
| 132 | + backgroundColor: |
| 133 | + "#e5e5e5", |
| 134 | + borderRadius: |
| 135 | + "4px", |
| 136 | + overflow: |
| 137 | + "hidden", |
| 138 | + }} |
| 139 | + > |
| 140 | + <div |
| 141 | + style={{ |
| 142 | + width: `${percentage}%`, |
| 143 | + height: "100%", |
| 144 | + backgroundColor: |
| 145 | + "#1e87f0", |
| 146 | + transition: |
| 147 | + "width 0.3s ease", |
| 148 | + }} |
| 149 | + /> |
| 150 | + </div> |
| 151 | + <span> |
| 152 | + {percentage}% |
| 153 | + </span> |
| 154 | + </div> |
| 155 | + </td> |
| 156 | + </tr> |
| 157 | + ); |
| 158 | + })} |
| 159 | + </tbody> |
| 160 | + </table> |
| 161 | + )} |
| 162 | + </div> |
| 163 | + ); |
| 164 | + })} |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments