Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/assets/translations/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ export default {
fileTeacher: 'Teacher',
fileUploadTime: 'Uploaded',
fileCategory: 'Category',
courseFileView: 'Course File View',
filesByTime: 'By Time',
filesByCategory: 'By Category',
uncategorized: 'Uncategorized',
fileDescription: 'Description',
somethingWentWrong: 'Something Went Wrong',
tryAgain: 'Try Again',
Expand Down
4 changes: 4 additions & 0 deletions src/assets/translations/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ const zh: typeof en = {
fileTeacher: '教师',
fileUploadTime: '上传时间',
fileCategory: '分类',
courseFileView: '课程文件显示方式',
filesByTime: '按时间',
filesByCategory: '按分类',
uncategorized: '未分类',
fileDescription: '描述',
somethingWentWrong: '出错了',
tryAgain: '重试',
Expand Down
1 change: 1 addition & 0 deletions src/data/__tests__/legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ describe('importing user data saved by 18.0.x', () => {
archived: { notice: ['n2'], assignment: [], file: ['f1'] },
hiddenCourses: ['c3'],
courseOrder: ['c2', 'c1'],
collapsedFileCategories: {},
currentSemester: '2025-2026-1',
latestSemester: null,
});
Expand Down
24 changes: 24 additions & 0 deletions src/data/__tests__/userData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
setArchived,
setCourseHidden,
setCourseOrder,
toggleFileCategory,
queueAction,
removeAction,
failAction,
Expand All @@ -31,6 +32,7 @@ describe('user data', () => {
archived: { notice: [], assignment: [], file: [] },
hiddenCourses: [],
courseOrder: [],
collapsedFileCategories: {},
currentSemester: null,
latestSemester: null,
});
Expand Down Expand Up @@ -116,6 +118,27 @@ describe('user data', () => {
});
});

it('restores file category choices independently for each course', async () => {
toggleFileCategory('a', 'category:slides');
toggleFileCategory('a', 'uncategorized');
toggleFileCategory('b', 'category:slides');
toggleFileCategory('a', 'category:slides');
const saved = Storage.getItemSync(USER_DATA_KEY)!;

resetUserData();
Storage.setItemSync(USER_DATA_KEY, saved);
await useUserDataStore.persist.rehydrate();
expect(getUserData().collapsedFileCategories).toEqual({
a: ['uncategorized'],
b: ['category:slides'],
});

toggleFileCategory('a', 'uncategorized');
expect(getUserData().collapsedFileCategories).toEqual({
b: ['category:slides'],
});
});

it('keeps lists added later when reading older saved data', async () => {
(Storage as unknown as { __store: Map<string, string> }).__store.set(
USER_DATA_KEY,
Expand All @@ -132,6 +155,7 @@ describe('user data', () => {
});
expect(getUserData().hiddenCourses).toEqual(['c']);
expect(getUserData().courseOrder).toEqual([]);
expect(getUserData().collapsedFileCategories).toEqual({});
});

describe('applyLatestSemester', () => {
Expand Down
1 change: 1 addition & 0 deletions src/data/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,7 @@ export const mockUserData: UserData = {
},
hiddenCourses: [research.id],
courseOrder: [],
collapsedFileCategories: {},
// Fetched from the emulated server, like a real account.
currentSemester: null,
latestSemester: null,
Expand Down
18 changes: 18 additions & 0 deletions src/data/userData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface UserData {
hiddenCourses: string[];
/** Course ids in the order the user dragged them into. */
courseOrder: string[];
/** Collapsed file category keys, by course id. */
collapsedFileCategories: Partial<Record<string, string[]>>;
/**
* The semester the app shows. Starts as the school's current semester and
* can be changed in Settings.
Expand All @@ -50,6 +52,7 @@ export const emptyUserData = (): UserData => ({
archived: { notice: [], assignment: [], file: [] },
hiddenCourses: [],
courseOrder: [],
collapsedFileCategories: {},
currentSemester: null,
latestSemester: null,
});
Expand Down Expand Up @@ -181,6 +184,21 @@ export const setCourseOrder = (courseIds: string[]) => {
useUserDataStore.setState({ courseOrder: courseIds });
};

export const toggleFileCategory = (courseId: string, categoryKey: string) => {
useUserDataStore.setState(state => {
const current = state.collapsedFileCategories[courseId] ?? [];
const next = toggle(current, [categoryKey], !current.includes(categoryKey));
const collapsedFileCategories = {
...state.collapsedFileCategories,
[courseId]: next,
};
if (!next.length) {
delete collapsedFileCategories[courseId];
}
return { collapsedFileCategories };
});
};

export const setCurrentSemester = (semesterId: string) => {
useUserDataStore.setState({ currentSemester: semesterId });
};
Expand Down
61 changes: 61 additions & 0 deletions src/helpers/__tests__/groupCourseFiles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { groupCourseFiles } from 'helpers/groupCourseFiles';
import { file } from 'test-utils/fixtures/cards';

const slides = file().category!;
const labs = { ...slides, id: 'labs', title: 'Labs' };
const other = 'Uncategorized';

it('keeps upload order within each category and puts unclassified files last', () => {
const syllabus = file({ id: 'syllabus', category: undefined });
const lecture2 = file({ id: 'lecture2', category: slides });
const lab = file({ id: 'lab', category: labs });
const lecture1 = file({ id: 'lecture1', category: slides });
const notes = file({ id: 'notes', category: { ...slides, title: ' ' } });
const files = Object.freeze([syllabus, lecture2, lab, lecture1, notes]);

expect(groupCourseFiles(files, other)).toEqual([
{ key: `category:${slides.id}`, heading: slides.title, expanded: true },
lecture2,
lecture1,
{ key: `category:${labs.id}`, heading: labs.title, expanded: true },
lab,
{ key: 'uncategorized', heading: other, expanded: true },
syllabus,
notes,
]);
});

it('keeps different categories with the same title distinct and omits empty groups', () => {
const first = file({ id: 'first', category: slides });
const second = file({
id: 'second',
category: { ...labs, title: slides.title },
});

expect(groupCourseFiles([first, second], other)).toEqual([
{ key: `category:${slides.id}`, heading: slides.title, expanded: true },
first,
{ key: `category:${labs.id}`, heading: slides.title, expanded: true },
second,
]);
expect(groupCourseFiles([], other)).toEqual([]);
});

it('keeps collapsed headings in order and uses ids even when a category is renamed', () => {
const renamed = { ...slides, title: 'Lecture notes' };
const lecture = file({ id: 'lecture', category: renamed });
const lab = file({ id: 'lab', category: labs });
const syllabus = file({ id: 'syllabus', category: undefined });

expect(
groupCourseFiles([syllabus, lecture, lab], other, [
`category:${slides.id}`,
'uncategorized',
]),
).toEqual([
{ key: `category:${slides.id}`, heading: renamed.title, expanded: false },
{ key: `category:${labs.id}`, heading: labs.title, expanded: true },
lab,
{ key: 'uncategorized', heading: other, expanded: false },
]);
});
46 changes: 46 additions & 0 deletions src/helpers/groupCourseFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { File } from 'data/types/state';

export interface FileCategoryHeading {
key: string;
heading: string;
expanded: boolean;
}

/**
* Files arrive newest first. Keeping each category in the order it first
* appears puts the category with the latest upload at the top.
*/
export const groupCourseFiles = (
files: readonly File[],
uncategorizedTitle: string,
collapsedKeys: readonly string[] = [],
): (File | FileCategoryHeading)[] => {
const groups = new Map<string, [FileCategoryHeading, ...File[]]>();
const uncategorizedKey = 'uncategorized';
const collapsed = new Set(collapsedKeys);
for (const file of files) {
const category = file.category?.title.trim() ? file.category : undefined;
const key = category ? `category:${category.id}` : uncategorizedKey;
let group = groups.get(key);
if (!group) {
group = [
{
key,
heading: category?.title ?? uncategorizedTitle,
expanded: !collapsed.has(key),
},
];
groups.set(key, group);
}
if (group[0].expanded) {
group.push(file);
}
}

const uncategorized = groups.get(uncategorizedKey);
if (uncategorized) {
groups.delete(uncategorizedKey);
groups.set(uncategorizedKey, uncategorized);
}
return [...groups.values()].flat();
};
12 changes: 12 additions & 0 deletions src/helpers/systemIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ export const systemIcons = {
android: 'arrow_forward',
windows: 'ArrowRightRegular',
},
disclosureExpanded: {
ios: 'chevron.down',
android: 'expand_more',
windows: 'ChevronDownRegular',
},
disclosureCollapsed: {
ios: 'chevron.forward',
android: 'chevron_right',
windows: 'ChevronRightRegular',
},
close: { ios: 'xmark', android: 'close', windows: 'DismissRegular' },
refresh: {
ios: 'arrow.clockwise',
Expand Down Expand Up @@ -510,6 +520,7 @@ export const materialCodepoints: Record<SystemIconAndroidName, number> = {
check: 0xe5ca,
check_circle: 0xe86c,
checklist: 0xe6b1,
chevron_right: 0xe5cc,
close: 0xe5cd,
cloud_off: 0xe2c1,
code: 0xe86f,
Expand All @@ -524,6 +535,7 @@ export const materialCodepoints: Record<SystemIconAndroidName, number> = {
error: 0xe000,
event_busy: 0xe615,
event_upcoming: 0xf238,
expand_more: 0xe5cf,
favorite: 0xe87d,
feedback: 0xe87f,
file_export: 0xf3b2,
Expand Down
Loading