-
Notifications
You must be signed in to change notification settings - Fork 93
feat: add new TupleViewer component #1236
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
Open
aaguiarz
wants to merge
5
commits into
main
Choose a base branch
from
feat/tuple-viewer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef8fd0c
feat: add new TupleViewer component
aaguiarz 1933469
fix: addressed PR feedback
aaguiarz b85b3f6
chore: CSS improvements
aaguiarz 769aa16
chore: made copy button similar to other snippets
aaguiarz a2787db
fix: fix linter
aaguiarz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| /** | ||
| * TupleViewer — displays OpenFGA relationship tuples in a styled code block. | ||
| * | ||
| * - Renders tuples with column-aligned keys in italic muted color. | ||
| * - Exposes CSS hook classes so shared styles can control the block surface, keys, copy button, and responsive layout. | ||
| * - Copy button writes valid YAML list format to the clipboard. | ||
| * - Optional `rightColumnTuples` renders two columns via CSS grid with a mobile fallback. | ||
| */ | ||
| import React, { useMemo, useRef } from 'react'; | ||
| import { usePrismTheme } from '@docusaurus/theme-common'; | ||
| import { CodeBlockContextProvider, createCodeBlockMetadata } from '@docusaurus/theme-common/internal'; | ||
| import CopyButton from '@theme/CodeBlock/Buttons/CopyButton'; | ||
| import type { RelationshipCondition } from '../RelationshipTuples/Viewer'; | ||
|
|
||
|
Comment on lines
+11
to
+14
|
||
| interface Tuple { | ||
| user: string; | ||
| relation: string; | ||
| object: string; | ||
| condition?: RelationshipCondition; | ||
| } | ||
|
|
||
| interface TupleViewerProps { | ||
| tuples: Tuple[]; | ||
| rightColumnTuples?: Tuple[]; | ||
| } | ||
|
|
||
| const PAD = 'condition'.length; | ||
| const INNER_PAD = 'context'.length; | ||
| const keyStyle: React.CSSProperties = { fontStyle: 'italic' }; | ||
|
|
||
| function Key({ name, pad }: { name: string; pad: number }): JSX.Element { | ||
| return ( | ||
| <span className="tuple-viewer__key" style={keyStyle}> | ||
| {name.padEnd(pad)} | ||
| </span> | ||
| ); | ||
| } | ||
|
|
||
| function formatDisplayValue(value: unknown): string { | ||
| return typeof value === 'string' ? value : JSON.stringify(value); | ||
|
|
||
| } | ||
|
|
||
| function formatYamlValue(value: unknown): string { | ||
| if (value === null) { | ||
| return 'null'; | ||
| } | ||
|
|
||
| if (typeof value === 'string') { | ||
| return JSON.stringify(value); | ||
| } | ||
|
|
||
| if (typeof value === 'number' || typeof value === 'boolean') { | ||
| return String(value); | ||
| } | ||
|
|
||
| return JSON.stringify(value); | ||
| } | ||
|
|
||
| function getTupleKey(tuple: Tuple): string { | ||
| return JSON.stringify([tuple.user, tuple.relation, tuple.object, tuple.condition ?? null]); | ||
| } | ||
|
|
||
| function TupleBlock({ tuple }: { tuple: Tuple }): JSX.Element { | ||
| const contextEntries = Object.entries(tuple.condition?.context ?? {}); | ||
|
|
||
| return ( | ||
| <div className="tuple-viewer__tuple-block"> | ||
| <div> | ||
| <Key name="user" pad={PAD} /> : {tuple.user} | ||
| </div> | ||
| <div> | ||
| <Key name="relation" pad={PAD} /> : {tuple.relation} | ||
| </div> | ||
| <div> | ||
| <Key name="object" pad={PAD} /> : {tuple.object} | ||
| </div> | ||
| {tuple.condition && ( | ||
| <> | ||
| <div> | ||
| <Key name="condition" pad={PAD} /> : | ||
| </div> | ||
| <div className="tuple-viewer__nested-block"> | ||
| <div> | ||
| <Key name="name" pad={INNER_PAD} /> : {tuple.condition.name} | ||
| </div> | ||
| {contextEntries.length > 0 && ( | ||
| <> | ||
| <div> | ||
| <Key name="context" pad={INNER_PAD} /> : | ||
| </div> | ||
| <div className="tuple-viewer__nested-block tuple-viewer__context-block"> | ||
| {contextEntries.map(([key, value]) => ( | ||
| <div key={key}> | ||
| <Key name={key} pad={0} /> : {formatDisplayValue(value)} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function TupleList({ tuples }: { tuples: Tuple[] }): JSX.Element { | ||
| const seenTuples = new Map<string, number>(); | ||
|
|
||
| return ( | ||
| <> | ||
| {tuples.map((tuple) => { | ||
| const baseKey = getTupleKey(tuple); | ||
| const count = seenTuples.get(baseKey) ?? 0; | ||
| seenTuples.set(baseKey, count + 1); | ||
| const tupleKey = count === 0 ? baseKey : `${baseKey}:${count}`; | ||
|
|
||
| return <TupleBlock key={tupleKey} tuple={tuple} />; | ||
| })} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| function formatYaml(tuple: Tuple): string { | ||
| const lines = [`- user: ${tuple.user}`, ` relation: ${tuple.relation}`, ` object: ${tuple.object}`]; | ||
|
|
||
| if (tuple.condition) { | ||
| lines.push(` condition:`); | ||
| lines.push(` name: ${tuple.condition.name}`); | ||
|
|
||
|
aaguiarz marked this conversation as resolved.
|
||
| const contextEntries = Object.entries(tuple.condition.context ?? {}); | ||
| if (contextEntries.length > 0) { | ||
| lines.push(` context:`); | ||
| } | ||
|
|
||
| for (const [key, value] of contextEntries) { | ||
| lines.push(` ${key}: ${formatYamlValue(value)}`); | ||
| } | ||
| } | ||
|
Comment on lines
+124
to
+139
|
||
|
|
||
| return lines.join('\n'); | ||
| } | ||
|
|
||
| function TupleViewerCopyButton({ text }: { text: string }): JSX.Element { | ||
| const codeBlockRef = useRef<HTMLPreElement>(null); | ||
| const metadata = useMemo( | ||
| () => | ||
| createCodeBlockMetadata({ | ||
| code: text, | ||
| className: 'language-yaml', | ||
| language: 'yaml', | ||
| defaultLanguage: undefined, | ||
| metastring: undefined, | ||
| magicComments: [], | ||
| title: null, | ||
| showLineNumbers: false, | ||
| }), | ||
| [text], | ||
| ); | ||
|
|
||
| return ( | ||
| <CodeBlockContextProvider | ||
| metadata={metadata} | ||
| wordWrap={{ | ||
| codeBlockRef, | ||
| isEnabled: false, | ||
| isCodeScrollable: false, | ||
| toggle: () => undefined, | ||
| }} | ||
| > | ||
| <div className="tuple-viewer__button-group"> | ||
| <CopyButton className="tuple-viewer__copy-button" /> | ||
| </div> | ||
| </CodeBlockContextProvider> | ||
| ); | ||
| } | ||
|
|
||
| export function TupleViewer({ tuples, rightColumnTuples }: TupleViewerProps): JSX.Element { | ||
| const { plain } = usePrismTheme(); | ||
| const allTuples = rightColumnTuples ? [...tuples, ...rightColumnTuples] : tuples; | ||
| const yaml = allTuples.map(formatYaml).join('\n'); | ||
|
|
||
| return ( | ||
| <div | ||
| className="theme-code-block tuple-viewer" | ||
| style={{ | ||
| position: 'relative', | ||
| marginBottom: 'var(--ifm-leading)', | ||
| color: plain.color, | ||
| borderRadius: 'var(--ifm-code-border-radius)', | ||
| }} | ||
| > | ||
| <div | ||
| className="tuple-viewer__body" | ||
| style={{ | ||
| padding: 'var(--ifm-pre-padding)', | ||
| fontFamily: 'var(--ifm-font-family-monospace)', | ||
| fontSize: 'var(--ifm-code-font-size)', | ||
| lineHeight: 'var(--ifm-pre-line-height)', | ||
| overflow: 'auto', | ||
| whiteSpace: 'pre', | ||
| }} | ||
| > | ||
| {rightColumnTuples ? ( | ||
| <div className="tuple-viewer__grid"> | ||
| <div className="tuple-viewer__column"> | ||
| <TupleList tuples={tuples} /> | ||
| </div> | ||
| <div className="tuple-viewer__column tuple-viewer__column--secondary"> | ||
| <TupleList tuples={rightColumnTuples} /> | ||
| </div> | ||
| </div> | ||
| ) : ( | ||
| <TupleList tuples={tuples} /> | ||
| )} | ||
| </div> | ||
| <TupleViewerCopyButton text={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
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.