result.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { For, Show } from "solid-js"
  2. import type { LookupResult, WorkspaceSection } from "~/lib/lookup"
  3. export function Result(props: { data: LookupResult }) {
  4. return (
  5. <>
  6. <Show when={props.data.auth}>
  7. {(auth) => (
  8. <section data-component="section">
  9. <h2>Auth</h2>
  10. <DataTable rows={auth()} />
  11. </section>
  12. )}
  13. </Show>
  14. <Show when={props.data.accountWorkspaces}>
  15. {(workspaces) => (
  16. <section data-component="section">
  17. <h2>Workspaces</h2>
  18. <DataTable rows={workspaces()} />
  19. </section>
  20. )}
  21. </Show>
  22. <For each={props.data.workspaces}>{(ws) => <WorkspaceView section={ws} />}</For>
  23. </>
  24. )
  25. }
  26. function WorkspaceView(props: { section: WorkspaceSection }) {
  27. return (
  28. <section data-component="section" id={`workspace-${props.section.workspaceID}`}>
  29. <h2>{props.section.title}</h2>
  30. <h3>Users</h3>
  31. <DataTable rows={props.section.users} />
  32. <h3>Billing</h3>
  33. <DataTable rows={props.section.billing ? [props.section.billing] : []} />
  34. <h3>GO</h3>
  35. <DataTable rows={props.section.go} />
  36. <h3>Payments</h3>
  37. <DataTable rows={props.section.payments} />
  38. <h3>28-Day Usage</h3>
  39. <DataTable rows={props.section.usage} />
  40. <h3>Disabled Models</h3>
  41. <DataTable rows={props.section.disabledModels} />
  42. </section>
  43. )
  44. }
  45. function DataTable(props: { rows: Record<string, unknown>[] }) {
  46. const columns = () => {
  47. const cols = new Set<string>()
  48. for (const row of props.rows) {
  49. for (const key of Object.keys(row)) cols.add(key)
  50. }
  51. return [...cols]
  52. }
  53. return (
  54. <Show when={props.rows.length > 0} fallback={<div data-empty>(no data)</div>}>
  55. <table>
  56. <thead>
  57. <tr>
  58. <For each={columns()}>{(col) => <th>{col}</th>}</For>
  59. </tr>
  60. </thead>
  61. <tbody>
  62. <For each={props.rows}>
  63. {(row) => (
  64. <tr>
  65. <For each={columns()}>{(col) => <td>{renderCell(row[col])}</td>}</For>
  66. </tr>
  67. )}
  68. </For>
  69. </tbody>
  70. </table>
  71. </Show>
  72. )
  73. }
  74. function renderCell(value: unknown) {
  75. if (value === null || value === undefined) return ""
  76. if (typeof value === "string" && value.startsWith("https://")) {
  77. return (
  78. <a href={value} target="_blank" rel="noopener noreferrer">
  79. {value}
  80. </a>
  81. )
  82. }
  83. if (isLinkCell(value)) {
  84. const external = value.__link.startsWith("http")
  85. return (
  86. <a
  87. href={value.__link}
  88. target={external ? "_blank" : undefined}
  89. rel={external ? "noopener noreferrer" : undefined}
  90. >
  91. {value.label}
  92. </a>
  93. )
  94. }
  95. if (typeof value === "object") return JSON.stringify(value)
  96. return String(value)
  97. }
  98. function isLinkCell(value: unknown): value is { __link: string; label: string } {
  99. return (
  100. typeof value === "object" &&
  101. value !== null &&
  102. "__link" in value &&
  103. typeof (value as { __link: unknown }).__link === "string"
  104. )
  105. }