Skip to content

Commit

Permalink
streaming vs regular query test
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Sep 19, 2024
1 parent f2ac595 commit 289bc7c
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 44 deletions.
57 changes: 54 additions & 3 deletions apps/web/src/api/trpc/routers/blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
import { createTRPCRouter } from "../helpers";
import { z } from "zod";
import { eq, asc, and, gt } from "drizzle-orm";
import { createTRPCRouter, tenantProcedure } from "../helpers";
import { blueprints, db } from "~/db";
import { createId } from "@paralleldrive/cuid2";

// await ctx.db.insert(blueprints).values(
// Array.from({ length: 1000 }).map((_, i) => ({
// // pk: i,
// id: createId(),
// name: `Blueprint ${i}`,
// tenantPk: ctx.tenant.pk,
// })),
// );

export const blueprintRouter = createTRPCRouter({
// TODO: CRUD
list: tenantProcedure.query(async function* ({ ctx }) {
let cursor: string | undefined = undefined;
do {
const data = await db

Check failure on line 20 in apps/web/src/api/trpc/routers/blueprint.ts

View workflow job for this annotation

GitHub Actions / Typecheck

'data' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
.select({
id: blueprints.id,
name: blueprints.name,
description: blueprints.description,
})
.from(blueprints)
.where(
and(
eq(blueprints.tenantPk, ctx.tenant.pk),
cursor !== undefined ? gt(blueprints.id, cursor) : undefined,
),
)
.limit(100)
.orderBy(asc(blueprints.id));

cursor = data.length !== 0 ? data[data.length - 1]!.id : undefined;
yield data;
} while (cursor !== undefined);
}),

list2: tenantProcedure.query(async ({ ctx }) => {
return await db
.select({
id: blueprints.id,
name: blueprints.name,
description: blueprints.description,
})
.from(blueprints)
.where(eq(blueprints.tenantPk, ctx.tenant.pk))
.orderBy(asc(blueprints.id));
}),

// TODO: create
// TODO: update
// TODO: delete

// TODO: Add/remove devices
// TODO: Modify content
});
22 changes: 0 additions & 22 deletions apps/web/src/api/trpc/routers/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const deviceRouter = createTRPCRouter({
// .leftJoin(users, eq(users.pk, devices.owner))
// .where(and(eq(devices.tenantPk, ctx.tenant.pk)));
// }),

// get: authedProcedure
// .input(z.object({ deviceId: z.string() }))
// .query(async ({ ctx, input }) => {
Expand Down Expand Up @@ -74,12 +73,9 @@ export const deviceRouter = createTRPCRouter({
// .leftJoin(users, eq(users.pk, devices.owner))
// .where(eq(devices.id, input.deviceId));
// if (!device) return null;

// await ctx.ensureTenantMember(device.tenantPk);

// return omit(device, ["tenantPk"]);
// }),

// action: authedProcedure
// .input(
// z.object({
Expand All @@ -92,26 +88,20 @@ export const deviceRouter = createTRPCRouter({
// where: eq(devices.id, input.deviceId),
// });
// if (!device) return null;

// await ctx.ensureTenantMember(device.tenantPk);

// if (input.action !== "sync") {
// await ctx.db.insert(deviceActions).values({
// action: input.action,
// devicePk: device.pk,
// createdBy: ctx.account.pk,
// });
// }

// // TODO: Talk with WNS or APNS to ask the device to checkin to MDM.
// console.log("TODO: Trigger MDM device checkin");

// return {};
// }),

// assignments: deviceProcedure.query(async ({ ctx }) => {
// const { device } = ctx;

// const [p, a] = await Promise.all([
// ctx.db
// .select({ pk: policies.pk, id: policies.id, name: policies.name })
Expand Down Expand Up @@ -141,10 +131,8 @@ export const deviceRouter = createTRPCRouter({
// eq(applicationAssignables.applicationPk, applications.pk),
// ),
// ]);

// return { policies: p, apps: a };
// }),

// addAssignments: deviceProcedure
// .input(
// z.object({
Expand All @@ -160,12 +148,10 @@ export const deviceRouter = createTRPCRouter({
// // biome-ignore lint/style/useSingleVarDeclarator: <explanation>
// const pols: Array<number> = [],
// apps: Array<number> = [];

// input.assignments.forEach((a) => {
// if (a.variant === "policy") pols.push(a.pk);
// else apps.push(a.pk);
// });

// const ops: Promise<unknown>[] = [];
// if (pols.length > 0)
// ops.push(
Expand All @@ -184,7 +170,6 @@ export const deviceRouter = createTRPCRouter({
// },
// }),
// );

// if (apps.length > 0)
// ops.push(
// db
Expand All @@ -202,17 +187,14 @@ export const deviceRouter = createTRPCRouter({
// },
// }),
// );

// await db.transaction((db) => Promise.all(ops));
// }),

// generateEnrollmentSession: tenantProcedure
// .input(z.object({ userId: z.string().nullable() }))
// .mutation(async ({ ctx, input }) => {
// const p = new URLSearchParams();
// p.set("mode", "mdm");
// p.set("servername", env.VITE_PROD_ORIGIN);

// let data: { uid: number; upn: string } | undefined = undefined;
// if (input.userId) {
// const [user] = await db
Expand All @@ -223,14 +205,12 @@ export const deviceRouter = createTRPCRouter({
// .from(users)
// .where(eq(users.id, input.userId));
// if (!user) throw new TRPCError({ code: "NOT_FOUND", message: "user" }); // TODO: Handle this on the frontend

// p.set("username", user.upn);
// data = {
// uid: user.pk,
// upn: user.upn,
// };
// }

// const jwt = await createEnrollmentSession(
// data
// ? {
Expand All @@ -244,9 +224,7 @@ export const deviceRouter = createTRPCRouter({
// // 7 days
// 7 * 24 * 60,
// );

// p.set("accesstoken", jwt);

// return `ms-device-enrollment:?${p.toString()}`;
// }),
});
33 changes: 32 additions & 1 deletion apps/web/src/app/(dash)/t/[tenantId]/blueprints/(blueprints).tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import { BreadcrumbItem } from "@mattrax/ui";
import { Suspense } from "solid-js";
import { useTenantId } from "~/app/(dash)";
import { Page } from "~/components/Page";
import { Table } from "~/components/Table";
import { trpc } from "~/lib";
import {
createCollectedGenerator,
refetchWhenStale,
} from "~/lib/createCollectedGenerator";

export default function () {
const tenantId = useTenantId();
const ctx = trpc.useContext();

const blueprints = createCollectedGenerator((signal) =>
ctx.blueprint.list.fetch(
{
tenantId: tenantId(),
},
{
queryKey: [
["blueprint", "list"],
{
tenantId: tenantId(),
},
],
signal,
},
),
);
refetchWhenStale(blueprints);

return (
<Page
title="Blueprints"
Expand All @@ -12,7 +40,10 @@ export default function () {
</BreadcrumbItem>,
]}
>
<Table />
<Suspense fallback={<p>TODO: Loading...</p>}>
{/* <p>{blueprints.loading.toString()}</p> */}
<Table data={blueprints.data.flat() || []} />
</Suspense>
</Page>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BreadcrumbItem, BreadcrumbLink } from "@mattrax/ui/breadcrumb";
import { z } from "zod";
import { Page, Page2 } from "~/components/Page";
import { Page } from "~/components/Page";
import { useZodParams } from "~/lib/useZodParams";

export default function () {
Expand All @@ -10,7 +10,8 @@ export default function () {
const blueprintName = "Marketing Blueprint";

return (
<Page2
<Page
title={null}
breadcrumbs={[
<BreadcrumbItem>
<BreadcrumbLink href="../../blueprints">Blueprints</BreadcrumbLink>
Expand Down Expand Up @@ -41,6 +42,6 @@ export default function () {
<h1>Hello World</h1>
</div>
</div>
</Page2>
</Page>
);
}
31 changes: 31 additions & 0 deletions apps/web/src/app/(dash)/t/[tenantId]/blueprints/full.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// TODO: Remove this file once testing is done

import { BreadcrumbItem } from "@mattrax/ui";
import { Suspense } from "solid-js";
import { useTenantId } from "~/app/(dash)";
import { Page } from "~/components/Page";
import { Table } from "~/components/Table";
import { trpc } from "~/lib";

export default function () {
const tenantId = useTenantId();

const blueprints = trpc.blueprint.list2.createQuery(() => ({
tenantId: tenantId(),
}));

return (
<Page
title="Blueprints"
breadcrumbs={[
<BreadcrumbItem>
<BreadcrumbItem>Blueprints</BreadcrumbItem>
</BreadcrumbItem>,
]}
>
<Suspense fallback={<p>TODO: Loading...</p>}>
<Table data={blueprints.data?.flat() || []} />
</Suspense>
</Page>
);
}
2 changes: 1 addition & 1 deletion apps/web/src/app/(dash)/t/[tenantId]/devices/(devices).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function () {
title="Devices"
breadcrumbs={[<BreadcrumbItem>Devices</BreadcrumbItem>]}
>
<Table />
<Table data={[]} />
</Page>
);
}
21 changes: 14 additions & 7 deletions apps/web/src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { For } from "solid-js";
// TODO: Maybe rename cause this overlaps with `@mattrax/ui`
// TODO: Export data button

export function Table() {
type TableProps<T> = {
data: T[];
};

export function Table<T>(props: TableProps<T>) {
return (
<div class="space-y-4 w-full">
<TableHeader />
Expand Down Expand Up @@ -139,7 +143,8 @@ export function Table() {
</tr>
</thead>
<tbody class="[&amp;_tr:last-child]:border-0">
<For each={Array.from({ length: 10 })}>{(i) => <TableRow />}</For>
{/* // TODO: Suspense */}
<For each={props.data}>{(row) => <TableRow row={row} />}</For>
</tbody>
</table>
</div>
Expand Down Expand Up @@ -246,7 +251,7 @@ function TableHeader() {
);
}

function TableRow() {
function TableRow<T>(props: { row: T }) {
return (
<tr
class="border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted"
Expand All @@ -261,19 +266,21 @@ function TableRow() {
value="on"
class="peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground translate-y-[2px]"
aria-label="Select row"
disabled
></button>
</td>
<td class="p-2 align-middle [&amp;:has([role=checkbox])]:pr-0 [&amp;>[role=checkbox]]:translate-y-[2px]">
<div class="w-[80px]">TASK-3360</div>
</td>
<td class="p-2 align-middle [&amp;:has([role=checkbox])]:pr-0 [&amp;>[role=checkbox]]:translate-y-[2px]">
<div class="flex space-x-2">
<div class="inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 text-foreground">
{/* <div class="inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 text-foreground">
Feature
</div>
</div> */}
<span class="max-w-[500px] truncate font-medium">
You can't quantify the program without synthesizing the neural OCR
interface!
{/* You can't quantify the program without synthesizing the neural OCR
interface! */}
{props.row.name}

Check failure on line 283 in apps/web/src/components/Table/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'name' does not exist on type 'T'.
</span>
</div>
</td>
Expand Down
Loading

0 comments on commit 289bc7c

Please sign in to comment.