Skip to content

Commit a59d62b

Browse files
committed
Graphs lists improvements
1 parent d4c03d2 commit a59d62b

5 files changed

Lines changed: 18 additions & 15 deletions

File tree

backend/src/db/operations/graphOperations.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,24 +144,28 @@ async function getGraphDataFromDb(graphIds: string[]): Promise<GraphData[]> {
144144
const placeholders = graphIds.map((_, i) => `$${i + 1}`).join(',');
145145
const result = await query(
146146
`SELECT g.id, g.name,
147-
COUNT(a.id) as argument_count,
148-
MAX(a.id) as latest_argument_id
147+
COUNT(DISTINCT a.id) as argument_count,
148+
COUNT(r.id) as reaction_count,
149+
GREATEST(MAX(a.id), MAX(r.id)) as latest_activity
149150
FROM graphs g
150151
LEFT JOIN arguments a ON g.id = a.graph_id
152+
LEFT JOIN reactions r ON a.id = r.argument_id
151153
WHERE g.id IN (${placeholders})
152-
GROUP BY g.id, g.name`,
154+
GROUP BY g.id, g.name
155+
ORDER BY GREATEST(MAX(a.id), MAX(r.id)) DESC NULLS FIRST`,
153156
graphIds
154157
);
155158

156159
if (result.rows.length === 0) {
157160
throw new Error('Graphs not found');
158161
}
159162

160-
return result.rows.map(row => ({
163+
return result.rows.map((row): GraphData => ({
161164
id: row.id,
162165
name: row.name,
163-
argumentCount: parseInt(row.argument_count),
164-
lastActivity: row.latest_argument_id ? getTimestamp(row.latest_argument_id) : undefined
166+
argumentCount: parseInt(row.argument_count, 10),
167+
reactionCount: parseInt(row.reaction_count, 10),
168+
lastActivity: row.latest_activity ? getTimestamp(row.latest_activity) : undefined
165169
}));
166170
}
167171

frontend/src/components/FeaturedDiscussionsList.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@ export const FeaturedDiscussionsList = () => {
1111
useEffect(() => {
1212
socket?.emit('get featured graphs', {}, (response: any) => {
1313
if (response.success) {
14-
const sortedGraphs = [...response.data.graphs].sort((a, b) => {
15-
if (a.name === "Nexus Feedback") return 1;
16-
if (b.name === "Nexus Feedback") return -1;
17-
return 0;
18-
});
19-
setGraphs(sortedGraphs);
14+
setGraphs(response.data.graphs);
2015
}
2116
});
2217
}, [socket]);

frontend/src/components/GraphCard.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { formatDate } from '../utils/time';
44
import { PiArrowRight } from 'react-icons/pi';
55
import { getActivityColor } from '../utils/colors';
66

7-
export const GraphCard = ({ id, name, argumentCount, lastActivity }: GraphData) => {
7+
export const GraphCard = ({ id, name, argumentCount, reactionCount, lastActivity }: GraphData) => {
88
return (
99
<Link to={`/feed/${id}`} className="block">
1010
<div className="
@@ -18,10 +18,14 @@ export const GraphCard = ({ id, name, argumentCount, lastActivity }: GraphData)
1818
<div className="flex justify-between items-start">
1919
<div className="flex-1">
2020
<h4 className="m-0 text-stone-800 transition-colors duration-150">{name}</h4>
21-
<div className="justify-between mt-2 flex flex-wrap">
21+
<div className="mt-2 flex flex-wrap">
2222
<small className="mr-2">
2323
<span className="font-bold">{argumentCount}</span> statement{argumentCount !== 1 ? 's' : ''}
2424
</small>
25+
<small className="mr-2">
26+
<span className="font-bold">{reactionCount}</span> reaction{argumentCount !== 1 ? 's' : ''}
27+
</small>
28+
<div className="flex-grow"></div>
2529
<small>
2630
Last activity: <span style={{ color: getActivityColor(lastActivity) }} className="font-bold">
2731
{lastActivity ? formatDate(lastActivity) : 'never'}

frontend/src/components/GraphsList.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ interface GraphsListProps {
77
}
88

99
// TODO This should be paginated
10-
// TODO This should be sorted by most recent
1110
// TODO If the list of graphs is empty, show a message
1211
export const GraphsList = ({ graphs }: GraphsListProps) => {
1312
return (

frontend/src/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface GraphData {
6666
id: string;
6767
name: string;
6868
argumentCount: number;
69+
reactionCount: number;
6970
lastActivity: number | undefined;
7071
}
7172

0 commit comments

Comments
 (0)