Skip to content

Commit

Permalink
Merge pull request #24 from eyefloaters/bookmarks
Browse files Browse the repository at this point in the history
Bookmarks
  • Loading branch information
riccardo-forina authored Sep 7, 2023
2 parents 9884196 + bc95cbe commit af979cc
Show file tree
Hide file tree
Showing 58 changed files with 928 additions and 712 deletions.
4 changes: 2 additions & 2 deletions .github/project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Web UI
release:
current-version: 0.0.4
next-version: 0.0.5-SNAPSHOT
current-version: 0.0.5
next-version: 0.0.6-SNAPSHOT

81 changes: 0 additions & 81 deletions ui/api/auth.tsx

This file was deleted.

72 changes: 72 additions & 0 deletions ui/api/bookmarks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use server";
import { Bookmark, BookmarkSchema, Cluster, Response } from "@/api/types";
import { getSession, setSession } from "@/utils/session";
import { redirect } from "next/navigation";

export async function getBookmarks(): Promise<Bookmark[]> {
const session = await getSession();

if (session?.bookmarks && Array.isArray(session.bookmarks)) {
return session.bookmarks.map((p) => BookmarkSchema.parse(p));
}
return [];
}

export async function getBookmark(id: string) {
const bookmarks = await getBookmarks();

const bookmark = bookmarks.find((p) => p.id === id);
if (!bookmark) {
redirect("/");
}
return bookmark;
}

export async function setPartialBookmark(formData: FormData) {
const session = await getSession();
const newBookmark = session?.newBookmark || {};
const data = Object.fromEntries(formData);
const newSession = { ...session, newBookmark: { ...newBookmark, ...data } };
console.dir({ data, session, newSession });
await setSession(newSession);
}

export async function createBookmark({
name,
bootstrapServer,
principal,
cluster,
}: {
bootstrapServer: string;
principal: string;
name: string;
cluster: Cluster | undefined;
}) {
const session = await getSession();
const bookmarks = (session?.bookmarks || []) as Bookmark[];
const newProfile: Bookmark = {
id: crypto.randomUUID(),
type: "bookmark",
attributes: {
cluster,
mechanism: "PLAIN",
name,
bootstrapServer,
principal,
},
};
const newAuthProfiles = [...bookmarks, newProfile];
await setSession({ bookmarks: newAuthProfiles, newBookmark: undefined });
return newProfile;
}

export async function getClusters(): Promise<Cluster[]> {
const url = `${process.env.BACKEND_URL}/api/kafkas?fields%5Bkafkas%5D=name,bootstrapServers,authType`;
const res = await fetch(url, {
headers: {
Accept: "application/json",
},
});
const rawData = await res.json();
return Response.parse(rawData).data;
}
File renamed without changes.
59 changes: 1 addition & 58 deletions ui/api/topics.tsx → ui/api/topics.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,4 @@
import { z } from "zod";

const PartitionSchema = z.object({
partition: z.number(),
leader: z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
replicas: z.array(
z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
),
isr: z.array(
z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
),
offset: z.object({
offset: z.number(),
leaderEpoch: z.number(),
}),
});

const ConfigSchema = z.object({
value: z.string(),
source: z.string(),
sensitive: z.boolean(),
readOnly: z.boolean(),
type: z.string(),
});

const TopicSchema = z.object({
id: z.string(),
type: z.string(),
attributes: z.object({
name: z.string(),
internal: z.boolean(),
partitions: z.array(PartitionSchema),
authorizedOperations: z.array(z.string()),
configs: z.record(z.string(), ConfigSchema),
}),
});

const TopicsResponse = z.object({
data: z.array(TopicSchema),
});

const TopicResponse = z.object({
data: TopicSchema,
});

export type Topic = z.infer<typeof TopicSchema>;
import { Topic, TopicResponse, TopicsResponse } from "@/api/types";

export async function getTopics(kafkaId: string): Promise<Topic[]> {
const url = `${process.env.BACKEND_URL}/api/kafkas/${kafkaId}/topics?fields%5Btopics%5D=name,internal,partitions,authorizedOperations,configs&offsetSpec=latest`;
Expand Down
77 changes: 77 additions & 0 deletions ui/api/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { z } from "zod";

const ClusterSchema = z.object({
id: z.string(),
type: z.string(),
attributes: z.object({
name: z.string(),
bootstrapServers: z.string(),
}),
});
export const Response = z.object({
data: z.array(ClusterSchema),
});
export type Cluster = z.infer<typeof ClusterSchema>;
export const BookmarkSchema = z.object({
id: z.string(),
type: z.string(),
attributes: z.object({
name: z.string(),
bootstrapServer: z.string(),
principal: z.string(),
cluster: ClusterSchema.optional(),
mechanism: z.string(),
}),
});
export type Bookmark = z.infer<typeof BookmarkSchema>;
const PartitionSchema = z.object({
partition: z.number(),
leader: z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
replicas: z.array(
z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
),
isr: z.array(
z.object({
id: z.number(),
host: z.string(),
port: z.number(),
}),
),
offset: z.object({
offset: z.number(),
leaderEpoch: z.number(),
}),
});
const ConfigSchema = z.object({
value: z.string(),
source: z.string(),
sensitive: z.boolean(),
readOnly: z.boolean(),
type: z.string(),
});
const TopicSchema = z.object({
id: z.string(),
type: z.string(),
attributes: z.object({
name: z.string(),
internal: z.boolean(),
partitions: z.array(PartitionSchema),
authorizedOperations: z.array(z.string()),
configs: z.record(z.string(), ConfigSchema),
}),
});
export const TopicsResponse = z.object({
data: z.array(TopicSchema),
});
export const TopicResponse = z.object({
data: TopicSchema,
});
export type Topic = z.infer<typeof TopicSchema>;
3 changes: 0 additions & 3 deletions ui/api/user.ts

This file was deleted.

5 changes: 0 additions & 5 deletions ui/app/[locale]/(authProfiles)/loading.tsx

This file was deleted.

Loading

0 comments on commit af979cc

Please sign in to comment.