-
Notifications
You must be signed in to change notification settings - Fork 2
Team Chart with YChart Basics #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f758f85
chore: add @mieweb/ychart dependency to package.json
mfisher31 ce44db8
feat: integrate intitial TeamChart component into TeamsPage for visua…
mfisher31 e0890a7
feat: enhance TeamChartMount with wrapperRef for improved layout hand…
mfisher31 3bc0d66
fix: remove unused eslint-disable comments for effect dependencies in…
mfisher31 87ab8c0
update: REPO_URL to match timehuddles URL on github.
mfisher31 fef457e
lint: downgrade fails to warns. to be resolved in another issue.
mfisher31 c6b187a
housekeeping: remove unneeded comments from TeamChart
mfisher31 c026bdd
housekeeping: avoid runtime error by hydrating at inappropriate times.
mfisher31 bcd1cac
safety: wrap chart strings in stringify for double quoting.
mfisher31 ec094a6
fix: replace JSON.stringify members dep with stable memberKey in useMemo
mfisher31 2dc1b69
fix: downgrade @mieweb/ychart dependency from 1.1.0 to 1.0.0
mfisher31 df2d9b8
refactor: simplify TeamChartMount by removing unused wrapperRef prop
mfisher31 10bb89e
refactor: optimize member lookup by using a Map for usersById
mfisher31 2b4ba2a
chore: add new dependencies for @popperjs/core, delaunator, and style…
mfisher31 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * TeamChart — Renders an interactive org chart using @mieweb/ychart (YChartEditor). | ||
| */ | ||
| import '@mieweb/ychart'; | ||
| import React, { useEffect, useRef, useMemo } from 'react'; | ||
|
|
||
| type ChartState = { | ||
| svgWidth: number; | ||
| svgHeight: number; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| type OrgChartInstance = { | ||
| render: () => OrgChartInstance; | ||
| clear?: () => void; | ||
| fit?: (params?: { animate?: boolean; scale?: boolean }) => OrgChartInstance; | ||
| getChartState?: () => ChartState; | ||
| }; | ||
|
|
||
| type YChartInstance = { | ||
| initView(containerId: string, yaml: string): YChartInstance; | ||
| destroy?: () => void; | ||
| handleFit?: () => void; | ||
| orgChart?: OrgChartInstance; | ||
| }; | ||
|
|
||
| declare global { | ||
| interface Window { | ||
| YChartEditor: new () => YChartInstance; | ||
| } | ||
| } | ||
|
|
||
| interface Member { | ||
| id: string; | ||
| name: string; | ||
| email?: string; | ||
| isAdmin?: boolean; | ||
| } | ||
|
|
||
| interface TeamChartProps { | ||
| teamName: string; | ||
| members: Member[]; | ||
| } | ||
|
|
||
| function buildYaml(teamName: string, members: Member[]): string { | ||
| const lines: string[] = []; | ||
| lines.push(`- id: 0`); | ||
| lines.push(` name: ${JSON.stringify(teamName)}`); | ||
| lines.push(` title: Team`); | ||
| members.forEach((m, i) => { | ||
| lines.push(`- id: ${i + 1}`); | ||
| lines.push(` parentId: 0`); | ||
| lines.push(` name: ${JSON.stringify(m.name)}`); | ||
| if (m.isAdmin) lines.push(` title: Admin`); | ||
| if (m.email) lines.push(` email: ${JSON.stringify(m.email)}`); | ||
| }); | ||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| // Inner component: one mount = one initView, one unmount = one clean teardown. | ||
| // Remounted via key= in the outer component when data changes. | ||
| const TeamChartMount: React.FC<{ yaml: string }> = ({ yaml }) => { | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
| const instanceRef = useRef<YChartInstance | null>(null); | ||
| const chartId = useRef( | ||
| `tc-${Date.now()}-${Math.random().toString(36).slice(2)}` | ||
| ).current; | ||
|
|
||
| useEffect(() => { | ||
| const el = containerRef.current!; | ||
| el.id = chartId; | ||
| let fitTimerId = 0; | ||
|
|
||
| // One RAF so the container has real layout dimensions before initView | ||
| const rafId = requestAnimationFrame(() => { | ||
| if (!el.isConnected) return; | ||
| try { | ||
| instanceRef.current = new window.YChartEditor().initView(chartId, yaml); | ||
| fitTimerId = window.setTimeout(() => { | ||
| const oc = instanceRef.current?.orgChart; | ||
| if (!oc || !el.isConnected) return; | ||
| const rect = el.getBoundingClientRect(); | ||
| const state = oc.getChartState?.(); | ||
| if (state && rect.width > 0 && rect.height > 0) { | ||
| state.svgWidth = rect.width; | ||
| state.svgHeight = rect.height; | ||
| } | ||
| oc.fit?.({ animate: false }); | ||
| }, 450); | ||
| } catch (err) { | ||
| // Unmounted before RAF fired is expected; anything else is a real error | ||
| if (el.isConnected) console.error('[TeamChart] initView failed:', err); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelAnimationFrame(rafId); | ||
| window.clearTimeout(fitTimerId); | ||
|
|
||
| const inst = instanceRef.current; | ||
| if (inst) { | ||
| // Step 1: Remove d3-org-chart's window resize + keydown listeners. | ||
| // Without this, those listeners fire after the DOM is cleared and crash | ||
| // on null.getBoundingClientRect(). | ||
| inst.orgChart?.clear?.(); | ||
|
|
||
| // Step 2: Patch render to a no-op as a safety net for any already-queued | ||
| // RAF callbacks that slip through before clear() takes effect. | ||
| if (inst.orgChart) { | ||
| const oc = inst.orgChart; | ||
| oc.render = () => oc; | ||
| } | ||
|
|
||
| // Step 3: Now safe — destroy clears innerHTML and frees React roots | ||
| inst.destroy?.(); | ||
| instanceRef.current = null; | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| return <div ref={containerRef} style={{ width: '100%', height: '100%' }} />; | ||
| }; | ||
|
|
||
| export const TeamChart: React.FC<TeamChartProps> = ({ teamName, members }) => { | ||
| const memberKey = members.map((m) => `${m.id}:${m.name}:${m.email ?? ''}:${m.isAdmin ? '1' : '0'}`).join(','); | ||
| const yaml = useMemo(() => buildYaml(teamName, members), [teamName, memberKey]); | ||
|
|
||
| if (members.length === 0) { | ||
| return ( | ||
| <p className="text-center text-sm text-neutral-500 py-8">No members to display.</p> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ width: '100%', height: '500px' }} | ||
| aria-label={`Org chart for ${teamName}`} | ||
| > | ||
| <TeamChartMount key={yaml} yaml={yaml} /> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.