generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show schema snippets on hover of decl links in the console (#2706)
Fixes #2673 This only adds the snippet implementations for data, enum, and typealias, since those are the types that currently would actually come up as a link in a decl panel. This also changes the static decl panel bodies for enum + data to follow the actual FTL schema format. <img width="563" alt="Screenshot 2024-09-17 at 3 32 26 PM" src="https://github.com/user-attachments/assets/e3763141-081f-4d6f-8d28-775f5b2fbc57"> <img width="393" alt="Screenshot 2024-09-17 at 3 33 01 PM" src="https://github.com/user-attachments/assets/f21029e2-91a0-4b03-b9d9-97bd43c230b3">
- Loading branch information
Showing
11 changed files
with
179 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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
22 changes: 22 additions & 0 deletions
22
frontend/console/src/features/modules/decls/DataSnippet.tsx
This file contains 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,22 @@ | ||
import type { Data } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { TypeEl } from './TypeEl' | ||
|
||
export const DataSnippet = ({ value }: { value: Data }) => { | ||
const maybeTypeParams = value.typeParameters.length === 0 ? '' : `<${value.typeParameters.map((p) => p.name).join(', ')}>` | ||
return ( | ||
<div className='text-xs font-mono'> | ||
<div> | ||
{value.export ? 'export ' : ''} | ||
data {value.name} | ||
{maybeTypeParams} | ||
{value.fields.length === 0 ? ' {}' : ' {'} | ||
</div> | ||
{value.fields.length === 0 || ( | ||
<div className='text-xs font-mono inline-grid grid-cols-2 gap-x-4 gap-y-2 ml-8 my-2' style={{ gridTemplateColumns: 'auto auto' }}> | ||
{value.fields.map((f, i) => [<span key={`field-name-${i}`}>{f.name}</span>, <TypeEl key={`field-type-${i}`} t={f.type} />])} | ||
</div> | ||
)} | ||
<div>{value.fields.length === 0 ? '' : '}'}</div> | ||
</div> | ||
) | ||
} |
This file contains 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
16 changes: 16 additions & 0 deletions
16
frontend/console/src/features/modules/decls/DeclSnippet.tsx
This file contains 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,16 @@ | ||
import type { Decl } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { DataSnippet } from './DataSnippet' | ||
import { EnumSnippet } from './EnumSnippet' | ||
import { TypeAliasSnippet } from './TypeAliasSnippet' | ||
|
||
export const DeclSnippet = ({ decl }: { decl: Decl }) => { | ||
switch (decl.value.case) { | ||
case 'data': | ||
return <DataSnippet value={decl.value.value} /> | ||
case 'enum': | ||
return <EnumSnippet value={decl.value.value} /> | ||
case 'typeAlias': | ||
return <TypeAliasSnippet value={decl.value.value} /> | ||
} | ||
return <div>under construction: {decl.value.case}</div> | ||
} |
This file contains 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 |
---|---|---|
@@ -1,75 +1,15 @@ | ||
import type { Enum, Type, Value } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { classNames } from '../../../utils' | ||
import type { Enum } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { EnumSnippet } from './EnumSnippet' | ||
import { PanelHeader } from './PanelHeader' | ||
import { TypeEl } from './TypeEl' | ||
|
||
const VariantComments = ({ comments, fullRow }: { comments?: string[]; fullRow?: boolean }) => { | ||
if (!comments) { | ||
return | ||
} | ||
return comments.map((c, i) => ( | ||
<div key={i} className={classNames('text-gray-500 dark:text-gray-400 mb-0.5', fullRow ? 'col-start-1 col-end-3' : '')}> | ||
{c} | ||
</div> | ||
)) | ||
} | ||
|
||
const VariantValue = ({ name, value }: { name?: string; value?: Value }) => { | ||
const v = value?.value.value?.value | ||
if (v === undefined) { | ||
return | ||
} | ||
const valueText = value?.value.case === 'intValue' ? v.toString() : `"${v}"` | ||
return ( | ||
<div className='mb-3'> | ||
{name && `${name} = `} | ||
{valueText} | ||
</div> | ||
) | ||
} | ||
|
||
const VariantNameAndType = ({ name, t }: { name: string; t: Type }) => { | ||
return [ | ||
<span key='n' className='mb-3'> | ||
{name} | ||
</span>, | ||
<TypeEl key='t' t={t} />, | ||
] | ||
} | ||
|
||
const ValueEnumVariants = ({ value }: { value: Enum }) => { | ||
return value.variants.map((v) => [<VariantComments key='c' comments={v.comments} />, <VariantValue key='v' name={v.name} value={v.value} />]) | ||
} | ||
|
||
const TypeEnumVariants = ({ value }: { value: Enum }) => { | ||
return ( | ||
<div className='inline-grid grid-cols-2 gap-x-4' style={{ gridTemplateColumns: 'auto auto' }}> | ||
{value.variants.map((v) => [ | ||
<VariantComments key='c' fullRow comments={v.comments} />, | ||
<VariantNameAndType key='n' name={v.name} t={v.value?.value.value?.value as Type} />, | ||
])} | ||
</div> | ||
) | ||
} | ||
|
||
function enumType(value: Enum): string { | ||
if (!value.type) { | ||
return 'Type' | ||
} | ||
return value.type.value.case === 'string' ? 'String' : 'Int' | ||
} | ||
import { enumType } from './enum.utils' | ||
|
||
export const EnumPanel = ({ value, moduleName, declName }: { value: Enum; moduleName: string; declName: string }) => { | ||
const isValueEnum = value.type !== undefined | ||
return ( | ||
<div className='py-2 px-4'> | ||
<PanelHeader exported={value.export} comments={value.comments}> | ||
{enumType(value)} Enum: {moduleName}.{declName} | ||
</PanelHeader> | ||
<div className='mt-8'> | ||
<div className='mb-2'>Variants</div> | ||
<div className='text-xs font-mono'>{isValueEnum ? <ValueEnumVariants value={value} /> : <TypeEnumVariants value={value} />}</div> | ||
</div> | ||
<EnumSnippet value={value} /> | ||
</div> | ||
) | ||
} |
77 changes: 77 additions & 0 deletions
77
frontend/console/src/features/modules/decls/EnumSnippet.tsx
This file contains 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,77 @@ | ||
import type { Enum, Type, Value } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { classNames } from '../../../utils' | ||
import { TypeEl } from './TypeEl' | ||
import { enumType } from './enum.utils' | ||
|
||
const VariantComments = ({ comments, fullRow }: { comments?: string[]; fullRow?: boolean }) => { | ||
if (!comments) { | ||
return | ||
} | ||
return comments.map((c, i) => ( | ||
<div key={i} className={classNames('text-gray-500 dark:text-gray-400 mb-0.5', fullRow ? 'col-start-1 col-end-3' : '')}> | ||
{c} | ||
</div> | ||
)) | ||
} | ||
|
||
const VariantValue = ({ name, value }: { name?: string; value?: Value }) => { | ||
const v = value?.value.value?.value | ||
if (v === undefined) { | ||
return | ||
} | ||
const valueText = value?.value.case === 'intValue' ? v.toString() : `"${v}"` | ||
return ( | ||
<div className='mb-2'> | ||
{name && `${name} = `} | ||
{valueText} | ||
</div> | ||
) | ||
} | ||
|
||
const VariantNameAndType = ({ name, t }: { name: string; t: Type }) => { | ||
return [ | ||
<span key='n' className='mb-2'> | ||
{name} | ||
</span>, | ||
<TypeEl key='t' t={t} />, | ||
] | ||
} | ||
|
||
const ValueEnumSnippet = ({ value }: { value: Enum }) => { | ||
return ( | ||
<div> | ||
<div> | ||
{value.export ? 'export ' : ''} | ||
enum {value.name}: {`${enumType(value)} {`} | ||
</div> | ||
<div className='my-2 ml-8'> | ||
{value.variants.map((v) => [<VariantComments key='c' comments={v.comments} />, <VariantValue key='v' name={v.name} value={v.value} />])} | ||
</div> | ||
<div>{'}'}</div> | ||
</div> | ||
) | ||
} | ||
|
||
const TypeEnumSnippet = ({ value }: { value: Enum }) => { | ||
return ( | ||
<div> | ||
<div> | ||
{value.export ? 'export ' : ''} | ||
enum {value.name} | ||
{' {'} | ||
</div> | ||
<div className='inline-grid grid-cols-2 gap-x-4 mt-2 ml-8' style={{ gridTemplateColumns: 'auto auto' }}> | ||
{value.variants.map((v) => [ | ||
<VariantComments key='c' fullRow comments={v.comments} />, | ||
<VariantNameAndType key='n' name={v.name} t={v.value?.value.value?.value as Type} />, | ||
])} | ||
</div> | ||
<div>{'}'}</div> | ||
</div> | ||
) | ||
} | ||
|
||
export const EnumSnippet = ({ value }: { value: Enum }) => { | ||
const isValueEnum = value.type !== undefined | ||
return <div className='text-xs font-mono'>{isValueEnum ? <ValueEnumSnippet value={value} /> : <TypeEnumSnippet value={value} />}</div> | ||
} |
This file contains 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
6 changes: 2 additions & 4 deletions
6
frontend/console/src/features/modules/decls/TypeAliasPanel.tsx
This file contains 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 |
---|---|---|
@@ -1,16 +1,14 @@ | ||
import type { TypeAlias } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { PanelHeader } from './PanelHeader' | ||
import { TypeEl } from './TypeEl' | ||
import { TypeAliasSnippet } from './TypeAliasSnippet' | ||
|
||
export const TypeAliasPanel = ({ value, moduleName, declName }: { value: TypeAlias; moduleName: string; declName: string }) => { | ||
return ( | ||
<div className='py-2 px-4'> | ||
<PanelHeader exported={value.export} comments={value.comments}> | ||
Type Alias: {moduleName}.{declName} | ||
</PanelHeader> | ||
<div className='text-sm my-4'> | ||
Underlying type: <TypeEl t={value.type} /> | ||
</div> | ||
<TypeAliasSnippet value={value} /> | ||
</div> | ||
) | ||
} |
11 changes: 11 additions & 0 deletions
11
frontend/console/src/features/modules/decls/TypeAliasSnippet.tsx
This file contains 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,11 @@ | ||
import type { TypeAlias } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
import { TypeEl } from './TypeEl' | ||
|
||
export const TypeAliasSnippet = ({ value }: { value: TypeAlias }) => { | ||
return ( | ||
<div className='font-mono text-xs'> | ||
{value.export ? 'export ' : ''} | ||
typealias {value.name} <TypeEl t={value.type} /> | ||
</div> | ||
) | ||
} |
This file contains 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 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,8 @@ | ||
import type { Enum } from '../../../protos/xyz/block/ftl/v1/schema/schema_pb' | ||
|
||
export function enumType(value: Enum): string { | ||
if (!value.type) { | ||
return 'Type' | ||
} | ||
return value.type.value.case === 'string' ? 'String' : 'Int' | ||
} |