Skip to content

Commit b294fa9

Browse files
authored
Feature/frontend images (#79)
* Added frontend images * Fixed random not working when roles disabled
1 parent 77d2e70 commit b294fa9

File tree

4 files changed

+618
-74
lines changed

4 files changed

+618
-74
lines changed

ui/src/components/custom/matrixLoader.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import axios from "axios";
33
import { useMultiMatrix } from "./multiMatrixProvider";
44

55
const MatrixLoader: React.FC = () => {
6-
const { setMultiMatrix, setConfig, setItemIdMap, setRawItems } = useMultiMatrix();
6+
const {
7+
setMultiMatrix,
8+
setConfig,
9+
setItemIdMap,
10+
setRawItems,
11+
setItemImages
12+
} = useMultiMatrix();
713

814
useEffect(() => {
915
const params = new URLSearchParams(window.location.search);
@@ -61,6 +67,15 @@ const MatrixLoader: React.FC = () => {
6167
// Save raw items into provider (we'll let the UI decide which ones to display)
6268
setRawItems(matrixData.items);
6369

70+
// Load images data into the provider context if available
71+
if (matrixData.images && Array.isArray(matrixData.images)) {
72+
console.log("Loading images into context:", matrixData.images.length);
73+
setItemImages(matrixData.images);
74+
} else {
75+
console.log("No images found in matrix data");
76+
setItemImages([]);
77+
}
78+
6479
// Set the config, including hosts, participants, and the current user's email.
6580
setConfig({
6681
r2Multi: matrixData.r2Multi,
@@ -81,8 +96,12 @@ const MatrixLoader: React.FC = () => {
8196

8297
// For backwards compatibility (when roleBased is false), apply filtering based on randomAssignment.
8398
let processedItems = matrixData.items;
84-
if (!matrixData.roleBased) {
85-
processedItems = matrixData.items;
99+
if (!matrixData.roleBased && matrixData.randomAssignment) {
100+
// When role restrictions are disabled but random assignment is enabled,
101+
// only show items where the current user is in targetUsers
102+
processedItems = matrixData.items.filter((item: any) =>
103+
Array.isArray(item.targetUsers) && item.targetUsers.includes(currentUserEmail)
104+
);
86105
}
87106

88107
// Build a matrix map and id map from the processed items
@@ -114,7 +133,7 @@ const MatrixLoader: React.FC = () => {
114133
.catch((error) => {
115134
console.error("Error fetching user info:", error);
116135
});
117-
}, [setMultiMatrix, setConfig, setItemIdMap, setRawItems]);
136+
}, [setMultiMatrix, setConfig, setItemIdMap, setRawItems, setItemImages]);
118137

119138
return null;
120139
};

ui/src/components/custom/multiMatrixProvider.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ import React, { createContext, useContext, useState, ReactNode } from "react";
22

33
export type MultiMatrix = Map<string, Map<string, number>>;
44

5+
// Add interface for image data
6+
export interface MatrixImage {
7+
itemId: number;
8+
imageId: number;
9+
imageUrl: string;
10+
}
11+
512
export type ConfigType = {
613
r2Multi: number;
714
randomAssignment: boolean;
@@ -32,6 +39,13 @@ export type MultiMatrixContextType = {
3239
// New rawItems state to store the full items list from the API:
3340
rawItems: any[];
3441
setRawItems: React.Dispatch<React.SetStateAction<any[]>>;
42+
// New state for storing image data:
43+
itemImages: MatrixImage[];
44+
setItemImages: React.Dispatch<React.SetStateAction<MatrixImage[]>>;
45+
// Helper function to check if an item has images
46+
hasItemImages: (itemId: number) => boolean;
47+
// Helper function to get images for a specific item
48+
getItemImages: (itemId: number) => MatrixImage[];
3549
};
3650

3751
const initialMultiMatrix: MultiMatrix = new Map([
@@ -103,6 +117,17 @@ export const MultiMatrixProvider: React.FC<{ children: ReactNode }> = ({ childre
103117
const [itemIdMap, setItemIdMap] = useState<Map<string, number>>(new Map());
104118
const [updates, setUpdates] = useState<any[]>([]);
105119
const [rawItems, setRawItems] = useState<any[]>([]);
120+
const [itemImages, setItemImages] = useState<MatrixImage[]>([]);
121+
122+
// Helper function to check if an item has associated images
123+
const hasItemImages = (itemId: number): boolean => {
124+
return itemImages.some(img => Number(img.itemId) === Number(itemId));
125+
};
126+
127+
// Helper function to get all images for a specific item
128+
const getItemImages = (itemId: number): MatrixImage[] => {
129+
return itemImages.filter(img => Number(img.itemId) === Number(itemId));
130+
};
106131

107132
const contextValue: MultiMatrixContextType = {
108133
multiMatrix,
@@ -115,6 +140,10 @@ export const MultiMatrixProvider: React.FC<{ children: ReactNode }> = ({ childre
115140
setUpdates,
116141
rawItems,
117142
setRawItems,
143+
itemImages,
144+
setItemImages,
145+
hasItemImages,
146+
getItemImages,
118147
};
119148

120149
return (

0 commit comments

Comments
 (0)