-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): add htmx to simplify the loading requirements of the si…
…debar content (#43) * feat(ui): add htmx import via script tag * feat(ui): use htmx for loading in the sidebar organizations
- Loading branch information
1 parent
f8ae0ed
commit e8a653d
Showing
11 changed files
with
109 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { FC } from "hono/jsx"; | ||
|
||
import type { TenantRecord } from "@/types/db.mjs"; | ||
|
||
export const SidebarOrganizations: FC<{ | ||
workspace: string; | ||
tenants: Array<TenantRecord>; | ||
}> = ({ workspace, tenants }) => { | ||
return tenants.length > 0 ? ( | ||
<ul> | ||
{tenants.map((item) => ( | ||
<li> | ||
<a | ||
href={`/app/${item.workspace}`} | ||
class='block py-1 data-[active-tenant="true"]:text-blue-500' | ||
data-active-tenant={workspace.length ? item.workspace === workspace : false} | ||
> | ||
{item.name} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>😞 You have no organizations.</p> | ||
); | ||
}; |
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,29 @@ | ||
import { Hono } from "hono"; | ||
|
||
import { db } from "@/config/db/drizzle.mjs"; | ||
import type { ServerContext } from "@/types/hono.mjs"; | ||
|
||
import { SidebarOrganizations } from "./components/sidebar-organizations.js"; | ||
|
||
import { checkUserAuthed } from "./utils/middleware.mjs"; | ||
|
||
const app = new Hono<ServerContext>(); | ||
|
||
app.get("/sidebar-organizations", checkUserAuthed, async (c) => { | ||
const user = c.var.user!; | ||
|
||
// DO NOT USE THIS PARAMETER FOR AUTH | ||
// THIS IS PURELY BEING USED FOR STYLING | ||
const workspace = c.req.query("current_workspace") || ""; | ||
|
||
const relationships = await db.query.usersToTenants.findMany({ | ||
where: (fields, { eq }) => eq(fields.userId, user.id), | ||
with: { tenant: true }, | ||
}); | ||
|
||
const tenants = relationships.map((r) => r.tenant); | ||
|
||
return c.html(<SidebarOrganizations workspace={workspace} tenants={tenants} />); | ||
}); | ||
|
||
export default app; |
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 |
---|---|---|
@@ -1,52 +1,38 @@ | ||
import type { FC, PropsWithChildren } from "hono/jsx"; | ||
import { User } from "lucia"; | ||
|
||
import type { TenantRecord } from "@/types/db.mjs"; | ||
|
||
export interface AppContainerProps { | ||
user: User; | ||
tenant: TenantRecord | null; | ||
tenants: Array<TenantRecord>; | ||
workspace: string; | ||
mainClass?: string; | ||
} | ||
|
||
export const AppContainer: FC<PropsWithChildren<AppContainerProps>> = ({ | ||
user, | ||
tenant, | ||
tenants, | ||
children, | ||
mainClass, | ||
}) => { | ||
export const AppContainer: FC<PropsWithChildren<AppContainerProps>> = (props) => { | ||
const { user, children, mainClass } = props; | ||
|
||
return ( | ||
<div className="flex flex-col md:grid md:grid-cols-4 lg:grid-cols-5 min-h-full"> | ||
<div class="flex flex-col md:grid md:grid-cols-4 lg:grid-cols-5 min-h-full"> | ||
<aside class="h-[250px] border-b flex flex-col bg-gray-100 dark:bg-gray-800 md:h-auto md:col-span-1 md:border-r md:border-b-0"> | ||
<div class="flex-grow p-2"> | ||
<p class="pb-2">Hello {user.username}!</p> | ||
<p class="pb-2 border-b">Your organizations</p> | ||
{tenants.length > 0 ? ( | ||
<ul> | ||
{tenants.map((item) => ( | ||
<li> | ||
<a | ||
href={`/app/${item.workspace}`} | ||
class='block py-1 data-[active-tenant="true"]:text-blue-500' | ||
data-active-tenant={tenant ? item.id === tenant.id : false} | ||
> | ||
{item.name} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
) : ( | ||
<p>😞 You have no organizations.</p> | ||
)} | ||
<SidebarFetcher {...props} /> | ||
<span class="border-t" /> | ||
</div> | ||
<div class="p-2"> | ||
<a href="/auth/logout">👋🏼 Logout</a> | ||
</div> | ||
</aside> | ||
<main className={["md:col-span-3 lg:col-span-4", mainClass].filter(Boolean).join(" ")}>{children}</main> | ||
<main class={["md:col-span-3 lg:col-span-4", mainClass].filter(Boolean).join(" ")}>{children}</main> | ||
</div> | ||
); | ||
}; | ||
|
||
const SidebarFetcher: FC<Pick<AppContainerProps, "user" | "workspace">> = ({ user, workspace }) => { | ||
return ( | ||
<div | ||
hx-trigger="load" | ||
hx-get={["/app/hx/sidebar-organizations", workspace.length > 0 ? `?current_workspace=${workspace}` : ""].join("")} | ||
></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 |
---|---|---|
|
@@ -7,6 +7,11 @@ export const RootDocument: FC<PropsWithChildren<{ title: string }>> = ({ title, | |
<meta charset="UTF-8"></meta> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"></meta> | ||
<title>{title}</title> | ||
<script | ||
src="https://unpkg.com/[email protected]" | ||
integrity="sha384-wS5l5IKJBvK6sPTKa2WZ1js3d947pvWXbPJ1OmWfEuxLgeHcEbjUUA5i9V5ZkpCw" | ||
crossorigin="anonymous" | ||
></script> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
</head> | ||
<body class="bg-gray-50 text-gray-950 dark:bg-gray-950 dark:text-gray-50">{children}</body> | ||
|
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
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
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