Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions app/[locale]/(dashboard)/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ import { Input } from '@/components/ui/input';
import { Button } from '@/components/ui/button';
import { OrgProfileForm } from '@/components/ui/org-profile-form';
import Link from 'next/link';
import * as React from 'react';

import { usePreferences } from '@/hooks';

export default function SettingsPage() {
const { data: session } = useSession();
const animationsDisabled = usePreferences((state) => state.animationsDisabled);
const setAnimationsDisabled = usePreferences((state) => state.setAnimationsDisabled);
// Avoid hydration mismatch by only rendering after mount
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => setMounted(true), []);

return (
<div className="flex flex-col gap-6">
Expand Down Expand Up @@ -56,6 +64,27 @@ export default function SettingsPage() {
</Card>
</div>

<Card>
<CardHeader>
<CardTitle className="text-foreground">Accessibility</CardTitle>
<CardDescription>Customize the interface for reduced motion.</CardDescription>
</CardHeader>
<CardContent className="flex items-center space-x-2">
{mounted && (
<input
type="checkbox"
id="disable-animations"
className="h-4 w-4 rounded border-border"
checked={animationsDisabled}
onChange={(e) => setAnimationsDisabled(e.target.checked)}
/>
)}
<Label htmlFor="disable-animations" className="cursor-pointer">
Disable Animations
</Label>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
</CardContent>
</Card>

<Card>
<CardHeader>
<CardTitle className="text-foreground">Account</CardTitle>
Expand Down
18 changes: 15 additions & 3 deletions components/graph/supply-chain-graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { usePreferences } from '@/hooks';

function SupplierNode({ data }: { data: { label: string } }) {
return (
Expand Down Expand Up @@ -75,6 +76,8 @@ export function SupplyChainGraph({
routes: RouteRecord[];
emissionsByRoute?: Map<string, number>;
}) {
const animationsDisabled = usePreferences((state) => state.animationsDisabled);

const { nodes, edges } = React.useMemo(() => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
const nodes: Node[] = [];
const edges: Edge[] = [];
Expand Down Expand Up @@ -107,21 +110,30 @@ export function SupplyChainGraph({
const sourceId = r.originSupplierId ? `supplier-${r.originSupplierId}` : `facility-${r.originFacilityId}`;
const targetId = `facility-${r.destinationId}`;
const emissions = emissionsByRoute?.get(r.id);

const isAnimated = !animationsDisabled;
const thickness = emissions !== undefined ? Math.min(6, Math.max(1.5, 1.5 + (emissions / 2000))) : 1.5;
const speed = emissions !== undefined ? Math.max(0.5, 3 - (emissions / 4000)) : 2;
Comment on lines +117 to +118

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep animation duration within the promised 2s–0.5s range.

At zero emissions this calculates 3s, and negative emissions produce durations above 3s; only the lower bound is clamped. Normalize non-negative emissions and clamp the result to both 0.5s and 2s.

Proposed fix
-      const speed = emissions !== undefined ? Math.max(0.5, 3 - (emissions / 4000)) : 2;
+      const speed =
+        emissions !== undefined
+          ? Math.min(2, Math.max(0.5, 2 - Math.max(0, emissions) / 4000))
+          : 2;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const thickness = emissions !== undefined ? Math.min(6, Math.max(1.5, 1.5 + (emissions / 2000))) : 1.5;
const speed = emissions !== undefined ? Math.max(0.5, 3 - (emissions / 4000)) : 2;
const thickness = emissions !== undefined ? Math.min(6, Math.max(1.5, 1.5 + (emissions / 2000))) : 1.5;
const speed =
emissions !== undefined
? Math.min(2, Math.max(0.5, 2 - Math.max(0, emissions) / 4000))
: 2;


edges.push({
id: `route-${r.id}`,
source: sourceId,
target: targetId,
label: emissions !== undefined ? `${MODE_LABEL[r.mode]} · ${formatKg(emissions)}` : `${MODE_LABEL[r.mode]} · ${r.distanceKm}km`,
animated: r.mode === 'AIR',
animated: isAnimated,
markerEnd: { type: MarkerType.ArrowClosed },
style: { stroke: r.mode === 'AIR' ? 'hsl(var(--destructive))' : 'hsl(var(--muted-foreground))' },
style: {
stroke: r.mode === 'AIR' ? 'hsl(var(--destructive))' : 'hsl(var(--muted-foreground))',
strokeWidth: thickness,
...(isAnimated && { animationDuration: `${speed}s` })
},
labelStyle: { fill: 'hsl(var(--foreground))', fontSize: 10, fontWeight: 500 },
labelBgStyle: { fill: 'hsl(var(--background))' },
});
});

return { nodes, edges };
}, [suppliers, facilities, routes, emissionsByRoute]);
}, [suppliers, facilities, routes, emissionsByRoute, animationsDisabled]);

const graphRef = React.useRef<HTMLDivElement>(null);

Expand Down
1 change: 1 addition & 0 deletions hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { useApi } from './use-api';
export type { ApiState } from './use-api';
export { useMutation } from './use-mutation';
export { useDebounce } from './use-debounce';
export { usePreferences } from './use-preferences';
19 changes: 19 additions & 0 deletions hooks/use-preferences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { create } from 'zustand';
import { persist } from 'zustand/middleware';
Comment thread
coderabbitai[bot] marked this conversation as resolved.

interface PreferencesState {
animationsDisabled: boolean;
setAnimationsDisabled: (disabled: boolean) => void;
}

export const usePreferences = create<PreferencesState>()(
persist(
(set) => ({
animationsDisabled: false,
setAnimationsDisabled: (disabled) => set({ animationsDisabled: disabled }),
}),
{
name: 'ecosphere-preferences',
}
)
);
Loading