-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add flow animations to supply-chain graph edges (#154) #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ import { | |||||||||||||||
| DropdownMenuItem, | ||||||||||||||||
| DropdownMenuTrigger, | ||||||||||||||||
| } from '@/components/ui/dropdown-menu'; | ||||||||||||||||
| import { usePreferences } from '@/hooks'; | ||||||||||||||||
|
|
||||||||||||||||
| function SupplierNode({ data }: { data: { label: string } }) { | ||||||||||||||||
| return ( | ||||||||||||||||
|
|
@@ -75,6 +76,10 @@ export function SupplyChainGraph({ | |||||||||||||||
| routes: RouteRecord[]; | ||||||||||||||||
| emissionsByRoute?: Map<string, number>; | ||||||||||||||||
| }) { | ||||||||||||||||
| const animationsDisabled = usePreferences((state) => state.animationsDisabled); | ||||||||||||||||
| const [mounted, setMounted] = React.useState(false); | ||||||||||||||||
| React.useEffect(() => setMounted(true), []); | ||||||||||||||||
|
|
||||||||||||||||
| const { nodes, edges } = React.useMemo(() => { | ||||||||||||||||
| const nodes: Node[] = []; | ||||||||||||||||
| const edges: Edge[] = []; | ||||||||||||||||
|
|
@@ -107,21 +112,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 = mounted ? !animationsDisabled : true; | ||||||||||||||||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| 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, mounted]); | ||||||||||||||||
|
|
||||||||||||||||
| const graphRef = React.useRef<HTMLDivElement>(null); | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { create } from 'zustand'; | ||
| import { persist } from 'zustand/middleware'; | ||
|
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', | ||
| } | ||
| ) | ||
| ); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.