Skip to content

Commit

Permalink
refactor: moved all entity/component components to a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
rmrt1n committed Feb 28, 2024
1 parent 58b3122 commit ea59064
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 114 deletions.
38 changes: 0 additions & 38 deletions src/components/component-details.tsx

This file was deleted.

41 changes: 0 additions & 41 deletions src/components/entity-cards.tsx

This file was deleted.

33 changes: 0 additions & 33 deletions src/components/entity-list.tsx

This file was deleted.

106 changes: 106 additions & 0 deletions src/components/entity-views.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
import { Entity } from "@/lib/types"

interface EntityViewsProps {
entities: Entity[]
}

// TODO: make this responsive, along with the sidebar
export function EntityCards({ entities }: EntityViewsProps) {
return (
<>
<div className="grid grid-cols-3 gap-4">
{entities.map((entity) => (
<EntityCard key={entity.id} entity={entity} />
))}
</div>

</>
)
}

interface EntityCardProps {
entity: Entity
}

export function EntityCard({ entity }: EntityCardProps) {
return (
<>
<div className="bg-background border border-border rounded-lg font-mono text-sm">
<div className="px-3 py-2 font-bold border-b border-border">
Entity {entity.id}
</div>
<div className="px-3 py-2 space-y-2">
{Object.keys(entity.components).map((name) => (
<ComponentDetails key={name} name={name} component={entity.components[name]} />
))}
</div>
</div>
</>
)
}

export function EntityList({ entities }: EntityViewsProps) {
return (
<>
<Accordion type="multiple" className="font-mono space-y-1">
{entities.map((entity) => (
<AccordionItem key={entity.id} value={entity.id.toString()} className="border-0">
<AccordionTrigger
className="px-3 py-2 border border-border rounded-lg data-[state=open]:rounded-b-none bg-background font-bold text-sm"
>
Entity {entity.id}
</AccordionTrigger>
<AccordionContent
asChild
className="px-3 py-2 border border-t-0 border-border rounded-b-lg bg-background whitespace-pre-wrap text-xs"
>
{Object.keys(entity.components).map((name) => (
<ComponentDetails key={name} name={name} component={entity.components[name]} />
))}
</AccordionContent>
</AccordionItem>
))}
</Accordion>
</>
)
}

interface ComponentDetailsProps {
name: string
component: object
}

// TODO: add case for object types (arrays are included as objects too)
const formatAttribute = (attr: any): React.ReactNode => {
switch (typeof attr) {
case 'string': return <span className="text-green-500">{`"${attr}"`}</span>
case 'number': return <span className="text-orange-500">{attr}</span>
case 'boolean': return <span className="text-blue-500">{attr}</span>
default: return <span>{attr}</span>
}
}

function ComponentDetails({ name, component }: ComponentDetailsProps) {
const attributes = Object.keys(component).filter((k) => !k.startsWith("_"))

return (
// 0.8125rem / 13px since the default sizes are too small/big
<details open className="space-y-1 text-[0.8125rem]">
<summary className="font-bold">
{name}
<span className="ml-2 text-muted-foreground font-medium">
{`{} ${attributes.length} keys`}
</span>
</summary>
<div>
{attributes.map((attr) => (
<p key={attr} className="ml-3 text-muted-foreground font-medium">
{/* @ts-ignore */}
{attr}: {formatAttribute(component[attr])}
</p>
))}
</div>
</details>
)
}
3 changes: 1 addition & 2 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { useQuery } from '@tanstack/react-query';
import { createFileRoute } from '@tanstack/react-router';
import { Box, LayoutGrid, List, Unlink } from 'lucide-react';

import { EntityCards } from '@/components/entity-cards';
import { EntityCards, EntityList } from '@/components/entity-views';
import { Button } from '@/components/ui/button';
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
import { useCardinal } from '@/lib/cardinal-provider';
import { EntityList } from '@/components/entity-list';
import { useConfig } from '@/lib/config-provider';

export const Route = createFileRoute('/')({
Expand Down

0 comments on commit ea59064

Please sign in to comment.