1+ import { FunctionDefinition } from "@code0-tech/sagittarius-graphql-types" ;
2+ import React from "react" ;
3+ import { Card as FumaCard } from "fumadocs-ui/components/card" ;
4+ import { IconNote , IconX } from "@tabler/icons-react" ;
5+ import { md5 } from "js-md5" ;
6+
7+ export interface FunctionCardProps {
8+ definition ?: FunctionDefinition
9+ }
10+
11+ const GOLDEN_ANGLE = 137.50776405003785
12+
13+ const extractIdNumber = ( s : string ) => {
14+ const m = s . match ( / \/ ( \d + ) \s * $ / )
15+ return m ? Number ( m [ 1 ] ) : null
16+ }
17+
18+ export const hashToColor = ( s : string , from : number = 25 , to : number = 320 ) : string => {
19+ const range = to - from ;
20+ const n = extractIdNumber ( s ) ;
21+ if ( n != null ) {
22+ const hue = from + ( ( n * GOLDEN_ANGLE ) % range ) ;
23+ return `hsl(${ hue } , 100%, 72%)` ;
24+ }
25+
26+ const h = md5 ( md5 ( s ) ) ;
27+ const a = parseInt ( h . slice ( 0 , 8 ) , 16 ) ;
28+ return `hsl(${ from + ( a % range ) } , 100%, 72%)` ;
29+ }
30+
31+ const FunctionCard : React . FC < FunctionCardProps > = ( props ) => {
32+
33+ const { definition} = props
34+
35+ const splitTemplate = ( str : string ) =>
36+ str
37+ . split ( / ( \$ \{ [ ^ } ] + \} ) / )
38+ . filter ( Boolean )
39+ . flatMap ( part =>
40+ part . startsWith ( "${" )
41+ ? [ part . slice ( 2 , - 1 ) ] // variable name ohne ${}
42+ : part . split ( / ( \s * , \s * ) / ) // Kommas einzeln extrahieren
43+ . filter ( Boolean )
44+ . flatMap ( p => p . trim ( ) === "," ? [ "," ] : p . trim ( ) ? [ p . trim ( ) ] : [ ] )
45+ ) ;
46+
47+
48+ const displayMessage = splitTemplate ( definition ?. displayMessages ?. [ 0 ] . content ?? "Some ${example} function" )
49+ const renderedMessage = displayMessage . map ( ( part , index ) => {
50+ if ( definition ?. parameterDefinitions ?. nodes ?. find ( pd => pd ?. identifier === part ) ) {
51+ return < span style = { {
52+ background : "#191825" ,
53+ padding : ".116667rem .35rem" ,
54+ borderRadius : "1rem" ,
55+ boxShadow : "inset 0 1px 1px #bfbfbf1a"
56+ } } >
57+ { part }
58+ </ span >
59+ }
60+
61+ return < span > { part } </ span >
62+ } )
63+
64+ console . log ( definition )
65+
66+ return < FumaCard title = { "" } key = { "function-card-" + definition ?. identifier } >
67+ < div style = { { gap : "1rem" , display : "flex" , justifyContent : "space-between" , alignItems : "center" } } >
68+ < FumaCard title = { "" } style = { { textWrap : "nowrap" , background : "#070514" , borderRadius : "1rem" , padding : "0.35rem 0.7rem" , border : "1px solid #bfbfbf1a" } } >
69+ < div style = { { gap : "0.35rem" , display : "flex" , alignItems : "center" , justifyContent : "center" } } >
70+ < IconNote color = { hashToColor ( definition ?. identifier ?? "" ) } size = { 16 } />
71+ { renderedMessage }
72+ </ div >
73+ </ FumaCard >
74+ < FumaCard title = { "" } style = { { background : "#070514" , border : "none" } } >
75+ < div style = { {
76+ background : "#2c2a36" ,
77+ padding : ".35rem .7rem" ,
78+ borderRadius : "1rem" ,
79+ boxShadow : "inset 0 1px 1px #bfbfbf1a" ,
80+ width : "fit-content" ,
81+ display : "flex" ,
82+ gap : "0.35rem" ,
83+ alignItems : "center" ,
84+ fontSize : "12px" ,
85+ marginBottom : "1rem" ,
86+ } } >
87+ < IconNote color = { hashToColor ( definition ?. identifier ?? "" ) } size = { 13 } />
88+ { definition ?. names ?. [ 0 ] . content }
89+ < IconX size = { 13 } />
90+ </ div >
91+ < div style = { { flexDirection : "column" , gap : "1rem" , display : "flex" } } >
92+ { definition ?. parameterDefinitions ?. nodes ?. map ( pd => (
93+ < div key = { "param-" + pd ?. identifier } >
94+ < span > { pd ?. names ?. [ 0 ] . content } </ span >
95+ < p style = { { margin : "0" , paddingTop : "0.7rem" } } > { pd ?. descriptions ?. [ 0 ] . content } </ p >
96+ < div style = { {
97+ marginTop : "0.7rem" ,
98+ background : "#191825" ,
99+ borderRadius : "1rem" ,
100+ boxShadow : "inset 0 1px 1px #bfbfbf1a" ,
101+ height : "40px" ,
102+ width : "100%" ,
103+ position : "relative" ,
104+ display : "block"
105+ } } />
106+ </ div >
107+ ) ) }
108+ </ div >
109+ </ FumaCard >
110+ </ div >
111+ </ FumaCard >
112+ }
113+
114+ export default FunctionCard ;
0 commit comments