SarahXia0405 commited on
Commit
cceb576
·
verified ·
1 Parent(s): 8e67c57

Update web/src/components/sidebar/CourseInfoSection.tsx

Browse files
web/src/components/sidebar/CourseInfoSection.tsx CHANGED
@@ -11,6 +11,10 @@ function gmailComposeLink(email: string, subject?: string, body?: string) {
11
  return `https://mail.google.com/mail/?view=cm&fs=1&${to}${su}${bd}`;
12
  }
13
 
 
 
 
 
14
  export function CourseInfoSection({
15
  currentWorkspaceId,
16
  workspaces,
@@ -27,40 +31,74 @@ export function CourseInfoSection({
27
  [workspaces, currentWorkspaceId]
28
  );
29
 
30
- const courseInfo: CourseDirectoryItem | null = useMemo(() => {
31
- const sel = (selectedCourse || "").trim();
32
- if (sel) {
33
- const hitById = availableCourses.find((c) => c.id === sel);
34
- if (hitById) return hitById;
35
 
36
- const hitByName = availableCourses.find((c) => c.name === sel);
37
- if (hitByName) return hitByName;
 
 
 
 
 
 
38
  }
39
 
 
40
  const wsCourse = (currentWorkspace as any)?.courseInfo as
41
- | { id?: string; name?: string }
42
  | undefined;
43
 
44
- const wsId = wsCourse?.id?.trim();
45
- if (wsId) return availableCourses.find((c) => c.id === wsId) ?? (wsCourse as any);
 
 
46
 
47
- const wsName = wsCourse?.name?.trim();
48
- if (wsName) return availableCourses.find((c) => c.name === wsName) ?? (wsCourse as any);
 
 
49
 
50
  return null;
51
  }, [availableCourses, currentWorkspace, selectedCourse]);
52
 
53
- if (!courseInfo) return null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  const instructorName = courseInfo?.instructor?.name ?? "N/A";
56
- const instructorEmail = courseInfo?.instructor?.email?.trim() || "";
 
57
  const taName = courseInfo?.teachingAssistant?.name ?? "N/A";
58
- const taEmail = courseInfo?.teachingAssistant?.email?.trim() || "";
59
 
60
  return (
61
  <div className="w-full">
62
  <div className="px-4 pt-4 pb-3 space-y-2">
63
- <div className="font-semibold text-base">{courseInfo.name}</div>
64
 
65
  <div className="text-sm text-muted-foreground">
66
  Instructor:&nbsp;
@@ -68,8 +106,8 @@ export function CourseInfoSection({
68
  <a
69
  href={gmailComposeLink(
70
  instructorEmail,
71
- `[Clare] Question about ${courseInfo.name}`,
72
- `Hi ${instructorName},\n\nI have a question about ${courseInfo.name}:\n\n(Write your question here)\n\nThanks,\n`
73
  )}
74
  target="_blank"
75
  rel="noopener noreferrer"
@@ -88,8 +126,8 @@ export function CourseInfoSection({
88
  <a
89
  href={gmailComposeLink(
90
  taEmail,
91
- `[Clare] Help request for ${courseInfo.name}`,
92
- `Hi ${taName},\n\nI need help with ${courseInfo.name}:\n\n(Write your question here)\n\nThanks,\n`
93
  )}
94
  target="_blank"
95
  rel="noopener noreferrer"
@@ -103,7 +141,6 @@ export function CourseInfoSection({
103
  </div>
104
  </div>
105
 
106
- {/* 固定颜色分割线 */}
107
  <Separator className="bg-[#ECECF1]" />
108
  </div>
109
  );
 
11
  return `https://mail.google.com/mail/?view=cm&fs=1&${to}${su}${bd}`;
12
  }
13
 
14
+ function norm(s: any) {
15
+ return String(s ?? "").trim().toLowerCase();
16
+ }
17
+
18
  export function CourseInfoSection({
19
  currentWorkspaceId,
20
  workspaces,
 
31
  [workspaces, currentWorkspaceId]
32
  );
33
 
34
+ const courseInfo = useMemo((): CourseDirectoryItem | null => {
35
+ const list = Array.isArray(availableCourses) ? availableCourses : [];
 
 
 
36
 
37
+ // 1) selectedCourse: 可能是 id / name(大小写/空格不一致也能匹配)
38
+ const selRaw = (selectedCourse || "").trim();
39
+ const sel = norm(selRaw);
40
+ if (sel) {
41
+ const hit =
42
+ list.find((c) => norm(c.id) === sel) ||
43
+ list.find((c) => norm(c.name) === sel);
44
+ if (hit) return hit;
45
  }
46
 
47
+ // 2) workspace.courseInfo: group workspace 的 courseInfo 可能只带 id/name
48
  const wsCourse = (currentWorkspace as any)?.courseInfo as
49
+ | { id?: string; name?: string; instructor?: any; teachingAssistant?: any }
50
  | undefined;
51
 
52
+ const wsId = norm(wsCourse?.id);
53
+ if (wsId) {
54
+ return list.find((c) => norm(c.id) === wsId) ?? (wsCourse as any);
55
+ }
56
 
57
+ const wsName = norm(wsCourse?.name);
58
+ if (wsName) {
59
+ return list.find((c) => norm(c.name) === wsName) ?? (wsCourse as any);
60
+ }
61
 
62
  return null;
63
  }, [availableCourses, currentWorkspace, selectedCourse]);
64
 
65
+ // ---------- Fallback:不要静默消失 ----------
66
+ if (!courseInfo) {
67
+ const wsName = (currentWorkspace as any)?.name ?? "";
68
+ const sel = (selectedCourse || "").trim();
69
+
70
+ return (
71
+ <div className="w-full">
72
+ <div className="px-4 pt-4 pb-3 space-y-2">
73
+ <div className="font-semibold text-base">Course</div>
74
+ <div className="text-sm text-muted-foreground">
75
+ Not resolved (courseInfo is null)
76
+ </div>
77
+ <div className="text-xs text-muted-foreground">
78
+ selectedCourse: <span className="font-medium">{sel || "—"}</span>
79
+ </div>
80
+ <div className="text-xs text-muted-foreground">
81
+ currentWorkspace: <span className="font-medium">{wsName || "—"}</span>
82
+ </div>
83
+ </div>
84
+ <Separator className="bg-[#ECECF1]" />
85
+ </div>
86
+ );
87
+ }
88
+
89
+ // ---------- Display fields ----------
90
+ const courseName = courseInfo.name ?? (selectedCourse || "Course");
91
 
92
  const instructorName = courseInfo?.instructor?.name ?? "N/A";
93
+ const instructorEmail = (courseInfo?.instructor?.email || "").trim();
94
+
95
  const taName = courseInfo?.teachingAssistant?.name ?? "N/A";
96
+ const taEmail = (courseInfo?.teachingAssistant?.email || "").trim();
97
 
98
  return (
99
  <div className="w-full">
100
  <div className="px-4 pt-4 pb-3 space-y-2">
101
+ <div className="font-semibold text-base">{courseName}</div>
102
 
103
  <div className="text-sm text-muted-foreground">
104
  Instructor:&nbsp;
 
106
  <a
107
  href={gmailComposeLink(
108
  instructorEmail,
109
+ `[Clare] Question about ${courseName}`,
110
+ `Hi ${instructorName},\n\nI have a question about ${courseName}:\n\n(Write your question here)\n\nThanks,\n`
111
  )}
112
  target="_blank"
113
  rel="noopener noreferrer"
 
126
  <a
127
  href={gmailComposeLink(
128
  taEmail,
129
+ `[Clare] Help request for ${courseName}`,
130
+ `Hi ${taName},\n\nI need help with ${courseName}:\n\n(Write your question here)\n\nThanks,\n`
131
  )}
132
  target="_blank"
133
  rel="noopener noreferrer"
 
141
  </div>
142
  </div>
143
 
 
144
  <Separator className="bg-[#ECECF1]" />
145
  </div>
146
  );