Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/components/tabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface Tab {
label: string;
value: string;
}

export default function Tabs({ tabs, activeTab }: { tabs: Tab[], activeTab: string }) {
return (
<box borderStyle="single" width="100%" flexDirection="row" flexShrink={0} columnGap={1}>
{tabs.map((tab) => (
<text key={tab.value} bg={activeTab === tab.value ? 'orange' : 'transparent'}>
{tab.label}
</text>
))}
</box>
);
}
19 changes: 19 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const VIEWS = [
"home",
"single-worker",
"single-r2-bucket",
"single-d1-database",
"single-queue",
];

export const PANELS = [
"workers",
"durables",
"buckets",
"domains",
"queues",
"d1",
"kv",
];

export const WORKER_TABS = ["events", "deployments"] as const;
39 changes: 13 additions & 26 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import SingleD1DatabaseView from "./views/d1/single";
import SingleQueueView from "./views/queues/single";
import { CheckForUpdate } from "./components/utils/check-for-update";
import type { Namespace } from "cloudflare/src/resources/kv.js";
import { PANELS, WORKER_TABS } from "./constants";

const { values, positionals } = parseArgs({
args: Bun.argv,
Expand Down Expand Up @@ -67,24 +68,6 @@ if (positionals.length == 2) {

// user has just called cftop
function App() {
const views = [
"home",
"single-worker",
"single-r2-bucket",
"single-d1-database",
"single-queue",
];

const panels = [
"workers",
"durables",
"buckets",
"domains",
"queues",
"d1",
"kv",
];

const renderer = useRenderer();
const [view, setView] = useState<string>("home");
const [workers, setWorkers] = useState<Script[]>([]);
Expand All @@ -106,6 +89,8 @@ if (positionals.length == 2) {
const [metrics, setMetrics] = useState<WorkerSummary[]>([]);
const [loading, setLoading] = useState<boolean>(false);

const [activeTab, setActiveTab] = useState<(typeof WORKER_TABS)[number]>("events");

const { start, end } = CloudflareAPI.getTimeRange(24);
const nowTimestamp = Date.now();
const startTimestamp = nowTimestamp - 24 * 60 * 60 * 1000;
Expand Down Expand Up @@ -217,9 +202,12 @@ if (positionals.length == 2) {
}

if (key.name === "tab") {
setFocussedSection(
panels[(panels.indexOf(focussedSection) + 1) % panels.length] as string,
);
if (view === "home") {
setFocussedSection(
PANELS[(PANELS.indexOf(focussedSection) + 1) % PANELS.length] as string,
);
} else if (view === "single-worker") {
}
}

if (key.name === "escape" || key.name === "esc") {
Expand Down Expand Up @@ -298,13 +286,9 @@ if (positionals.length == 2) {
const nextIndex = (currentIndex + 1) % d1Databases.length;
setFocussedItem(d1Databases[nextIndex]?.uuid || "");
} else if (focussedSection === "queues") {
console.log("down in queues");
const currentIndex = queues.findIndex((q) => q.queue_id === focussedItem);
console.log(`queues current index: ${currentIndex}`);
const nextIndex = (currentIndex + 1) % queues.length;
console.log(`queues next index: ${nextIndex}`);
setFocussedItem(queues[nextIndex]?.queue_id || "");
console.log(`queues focussed item: ${focussedItem}`);
} else {
setFocussedItem("");
}
Expand Down Expand Up @@ -368,7 +352,10 @@ if (positionals.length == 2) {
);
} else if (view === "single-worker") {
visibleView = (
<SingleWorkerView focussedItemLogs={focussedItemLogs} focussedItem={focussedItem} />
<SingleWorkerView
focussedItemLogs={focussedItemLogs}
focussedItem={focussedItem}
/>
);
} else if (view === "single-r2-bucket") {
visibleView = <SingleR2BucketView focussedItem={focussedItem} />;
Expand Down
199 changes: 116 additions & 83 deletions src/views/workers/single.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,105 +5,138 @@ import type { WorkerSummary } from "../../types";
import { getConfig } from "../../config";
import { CloudflareAPI } from "../../api";
import { useKeyboard } from "@opentui/react";
import Tabs from "../../components/tabs";
import { WORKER_TABS } from "../../constants";

function SingleWorkerView({
focussedItemLogs,
focussedItem,
focussedItemLogs,
focussedItem,
}: {
focussedItemLogs: any[];
focussedItem: string;
focussedItemLogs: any[];
focussedItem: string;
}) {
const logCount = Array.isArray(focussedItemLogs)
? focussedItemLogs.length
: 0;
const [metrics, setMetrics] = useState<WorkerSummary | null>(null);
const [showTimestamp, setShowTimestamp] = useState<boolean>(true);
const { start, end } = CloudflareAPI.getTimeRange(24);
const logCount = Array.isArray(focussedItemLogs)
? focussedItemLogs.length
: 0;
const [view, setView] = useState<"events" | "deployments">("events");
const [metrics, setMetrics] = useState<WorkerSummary | null>(null);
const [showTimestamp, setShowTimestamp] = useState<boolean>(true);
const [activeTab, setActiveTab] = useState<typeof WORKER_TABS[number]>("events");
const { start, end } = CloudflareAPI.getTimeRange(24);
const tabs = [
{
label: 'Events',
value: 'events',
},
{
label: 'Deployments',
value: 'deployments',
},
];

useKeyboard((key) => {
if (key.name === "t") {
setShowTimestamp((prev) => !prev);
}
});
useKeyboard((key) => {
if (key.name === "t") {
setShowTimestamp((prev) => !prev);
}

useEffect(() => {
const fetchMetrics = async () => {
const config = await getConfig();
const api = new CloudflareAPI({
apiToken: config.apiToken,
accountTag: config.accountId,
});
const metrics = await api.getSingleWorkerSummary(
focussedItem,
start,
end,
);
setMetrics(metrics);
};
fetchMetrics();
}, [focussedItem]);
if (key.name === 'tab') {
setActiveTab(
WORKER_TABS[
(WORKER_TABS.indexOf(activeTab) + 1) % WORKER_TABS.length
] as (typeof WORKER_TABS)[number],
);
}

return (
<box flexGrow={1} flexShrink={1} minHeight={0}>
<SingleMetrics metrics={metrics} />
if (key.name === "return") {
setActiveTab("events");
}
});

<Keybindings />
useEffect(() => {
const fetchMetrics = async () => {
const config = await getConfig();
const api = new CloudflareAPI({
apiToken: config.apiToken,
accountTag: config.accountId,
});
const metrics = await api.getSingleWorkerSummary(
focussedItem,
start,
end,
);
setMetrics(metrics);
};
fetchMetrics();
}, [focussedItem]);

<scrollbox
borderStyle="single"
borderColor="orange"
width="100%"
focused
flexGrow={1}
flexShrink={1}
minHeight={0}
style={{ wrapperOptions: { borderColor: "orange" } }}
>
<box width="100%">
<box marginBottom={1}>
<text>
<strong>
<u>
{focussedItem} events ({logCount} total)
</u>
</strong>
</text>
</box>
{/* @ts-ignore */}
{focussedItemLogs &&
Array.isArray(focussedItemLogs) &&
focussedItemLogs.length > 0 ? (
focussedItemLogs.map((log: any, index: number) => {
const logMessage =
log.$metadata?.messageTemplate || JSON.stringify(log);
return (
<box flexGrow={1} flexShrink={1} minHeight={0}>
<SingleMetrics metrics={metrics} />
<Tabs tabs={tabs} activeTab={activeTab} />

return (
<box
key={`${log.$metadata?.id || index}-${index}`}
flexDirection="column"
{view === "events" && (
<scrollbox
borderStyle="single"
borderColor="orange"
width="100%"
focused
flexGrow={1}
flexShrink={1}
minHeight={0}
style={{ wrapperOptions: { borderColor: "orange" } }}
>
<box flexDirection="row">
{showTimestamp && <LogTimestamp log={log} />}
<text>{logMessage}</text>
</box>
<box width="100%">
<box marginBottom={1}>
<text>
<strong>
<u>
{focussedItem} events ({logCount} total)
</u>
</strong>
</text>
</box>
{/* @ts-ignore */}
{focussedItemLogs &&
Array.isArray(focussedItemLogs) &&
focussedItemLogs.length > 0 ? (
focussedItemLogs.map((log: any, index: number) => {
const logMessage =
log.$metadata?.messageTemplate || JSON.stringify(log);

return (
<box
key={`${log.$metadata?.id || index}-${index}`}
flexDirection="column"
>
<box flexDirection="row">
{showTimestamp && <LogTimestamp log={log} />}
<text>{logMessage}</text>
</box>
</box>
);
})
) : (
<text>No logs yet...</text>
)}
</box>
</scrollbox>
)}
{view === "deployments" && (
<box>
<text>Deployments</text>
</box>
);
})
) : (
<text>No logs yet...</text>
)}
)}
<Keybindings />
</box>
</scrollbox>
</box>
);
);
}

function Keybindings() {
return (
<box borderStyle="single" width="100%" flexDirection="row" flexShrink={0}>
<text fg={"#888888"}>t: toggle timestamp</text>
</box>
);
return (
<box borderStyle="single" width="100%" flexDirection="row" flexShrink={0}>
<text fg={"#888888"}>t: toggle timestamp</text>
</box>
);
}

export default SingleWorkerView;