chore: format
This commit is contained in:
parent
c8338e5516
commit
527139f298
33 changed files with 848 additions and 3132 deletions
|
@ -1,33 +1,33 @@
|
|||
"use client";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
import type { ColumnDef } from "@tanstack/react-table";
|
||||
|
||||
export type UrlTable = {
|
||||
clicks: number;
|
||||
date_accessed: Date;
|
||||
date_added: Date;
|
||||
id: string;
|
||||
long_url: string;
|
||||
clicks: number;
|
||||
date_accessed: Date;
|
||||
date_added: Date;
|
||||
id: string;
|
||||
long_url: string;
|
||||
};
|
||||
|
||||
export const columns: ColumnDef<UrlTable>[] = [
|
||||
{
|
||||
accessorKey: "clicks",
|
||||
header: "Clicks",
|
||||
},
|
||||
{
|
||||
accessorKey: "long_url",
|
||||
header: "URL",
|
||||
},
|
||||
{
|
||||
accessorKey: "id",
|
||||
header: "Short URL",
|
||||
},
|
||||
{
|
||||
accessorKey: "date_accessed",
|
||||
header: "Date Accessed",
|
||||
},
|
||||
{
|
||||
accessorKey: "date_added",
|
||||
header: "Date Added",
|
||||
},
|
||||
{
|
||||
accessorKey: "clicks",
|
||||
header: "Clicks",
|
||||
},
|
||||
{
|
||||
accessorKey: "long_url",
|
||||
header: "URL",
|
||||
},
|
||||
{
|
||||
accessorKey: "id",
|
||||
header: "Short URL",
|
||||
},
|
||||
{
|
||||
accessorKey: "date_accessed",
|
||||
header: "Date Accessed",
|
||||
},
|
||||
{
|
||||
accessorKey: "date_added",
|
||||
header: "Date Added",
|
||||
},
|
||||
];
|
||||
|
|
|
@ -1,79 +1,79 @@
|
|||
"use client";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import {
|
||||
ColumnDef,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
useReactTable
|
||||
type ColumnDef,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
useReactTable,
|
||||
} from "@tanstack/react-table";
|
||||
|
||||
interface DataTableProps<TData, TValue> {
|
||||
columns: ColumnDef<TData, TValue>[];
|
||||
data: TData[];
|
||||
slug: string[];
|
||||
columns: ColumnDef<TData, TValue>[];
|
||||
data: TData[];
|
||||
slug: string[];
|
||||
}
|
||||
|
||||
export function DataTable<TData, TValue>({
|
||||
columns,
|
||||
data,
|
||||
columns,
|
||||
data,
|
||||
}: DataTableProps<TData, TValue>) {
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
});
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id}>
|
||||
{headerGroup.headers.map((header) => {
|
||||
return (
|
||||
<TableHead key={header.id}>
|
||||
{header.isPlaceholder
|
||||
? null
|
||||
: flexRender(
|
||||
header.column.columnDef.header,
|
||||
header.getContext()
|
||||
)}
|
||||
</TableHead>
|
||||
)
|
||||
})}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{table.getRowModel().rows?.length ? (
|
||||
table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<TableCell key={cell.id}>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={columns.length} className="h-24 text-center">
|
||||
No results.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</>
|
||||
);
|
||||
return (
|
||||
<>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
{table.getHeaderGroups().map((headerGroup) => (
|
||||
<TableRow key={headerGroup.id}>
|
||||
{headerGroup.headers.map((header) => {
|
||||
return (
|
||||
<TableHead key={header.id}>
|
||||
{header.isPlaceholder
|
||||
? null
|
||||
: flexRender(
|
||||
header.column.columnDef.header,
|
||||
header.getContext(),
|
||||
)}
|
||||
</TableHead>
|
||||
);
|
||||
})}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{table.getRowModel().rows?.length ? (
|
||||
table.getRowModel().rows.map((row) => (
|
||||
<TableRow
|
||||
key={row.id}
|
||||
data-state={row.getIsSelected() && "selected"}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<TableCell key={cell.id}>
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
))
|
||||
) : (
|
||||
<TableRow>
|
||||
<TableCell colSpan={columns.length} className="h-24 text-center">
|
||||
No results.
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,33 +1,36 @@
|
|||
"use server";
|
||||
import { initConnectionPostgres, initConnectionSurreal } from "@/components/db-utils";
|
||||
import {
|
||||
initConnectionPostgres,
|
||||
initConnectionSurreal,
|
||||
} from "@/components/db-utils";
|
||||
|
||||
export async function querydb() {
|
||||
try {
|
||||
let stats = [];
|
||||
if (process.env.DB_TYPE === "surrealdb") {
|
||||
let db = await initConnectionSurreal();
|
||||
// console.log(db);
|
||||
stats = await db.query(`
|
||||
try {
|
||||
let stats = [];
|
||||
if (process.env.DB_TYPE === "surrealdb") {
|
||||
const db = await initConnectionSurreal();
|
||||
// console.log(db);
|
||||
stats = await db.query(`
|
||||
select * from url
|
||||
order by clicks desc
|
||||
limit 10;
|
||||
`);
|
||||
|
||||
// @ts-ignore
|
||||
stats = stats[0];
|
||||
}
|
||||
// @ts-ignore
|
||||
stats = stats[0];
|
||||
}
|
||||
|
||||
if (process.env.DB_TYPE === "postgres") {
|
||||
let sql = await initConnectionPostgres();
|
||||
stats = await sql`
|
||||
if (process.env.DB_TYPE === "postgres") {
|
||||
const sql = await initConnectionPostgres();
|
||||
stats = await sql`
|
||||
select * from url
|
||||
order by clicks desc
|
||||
limit 10;
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
return stats;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
return stats;
|
||||
} catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
"use client";
|
||||
import CardGrid from "@/components/card-grid";
|
||||
import {
|
||||
Card,
|
||||
CardHeader,
|
||||
CardTitle
|
||||
} from "@/components/ui/card";
|
||||
import { Card, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
|
||||
export default function Loading() {
|
||||
return (
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-1xl text-amber-400">Loading...</CardTitle>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
</CardGrid>
|
||||
);
|
||||
return (
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle className="text-1xl text-amber-400">Loading...</CardTitle>
|
||||
</CardHeader>
|
||||
</Card>
|
||||
</CardGrid>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -8,47 +8,47 @@ import { querydb } from "./db";
|
|||
import Loading from "./loading";
|
||||
|
||||
export default function StatsPage() {
|
||||
let [data, setData] = useState([]);
|
||||
let [data, setData] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
const result = await querydb();
|
||||
// @ts-ignore
|
||||
setData(result);
|
||||
};
|
||||
fetchData();
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
const result = await querydb();
|
||||
// @ts-ignore
|
||||
setData(result);
|
||||
};
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
if (data.length !== 0 && data !== undefined && data !== null) {
|
||||
const formatDate = (dateString: string | number | Date) => {
|
||||
const date = new Date(dateString);
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const year = String(date.getFullYear()).slice(-2);
|
||||
return `${month}/${day}/${year}`;
|
||||
};
|
||||
if (data.length !== 0 && data !== undefined && data !== null) {
|
||||
const formatDate = (dateString: string | number | Date) => {
|
||||
const date = new Date(dateString);
|
||||
const day = String(date.getDate()).padStart(2, "0");
|
||||
const month = String(date.getMonth() + 1).padStart(2, "0");
|
||||
const year = String(date.getFullYear()).slice(-2);
|
||||
return `${month}/${day}/${year}`;
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
data = data.map(item => ({
|
||||
// @ts-ignore
|
||||
...item,
|
||||
// @ts-ignore
|
||||
date_accessed: formatDate(item.date_accessed),
|
||||
// @ts-ignore
|
||||
date_added: formatDate(item.date_added),
|
||||
// @ts-ignore
|
||||
id: item.id.replace(/^url:\['(.*)'\]$/, '$1')
|
||||
}));
|
||||
}
|
||||
// @ts-ignore
|
||||
data = data.map((item) => ({
|
||||
// @ts-ignore
|
||||
...item,
|
||||
// @ts-ignore
|
||||
date_accessed: formatDate(item.date_accessed),
|
||||
// @ts-ignore
|
||||
date_added: formatDate(item.date_added),
|
||||
// @ts-ignore
|
||||
id: item.id.replace(/^url:\['(.*)'\]$/, "$1"),
|
||||
}));
|
||||
}
|
||||
|
||||
return data.length === 0 ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
{/* @ts-ignore */}
|
||||
<DataTable columns={columns} data={data} />
|
||||
</Card>
|
||||
</CardGrid>
|
||||
);
|
||||
return data.length === 0 ? (
|
||||
<Loading />
|
||||
) : (
|
||||
<CardGrid maxCols={1}>
|
||||
<Card>
|
||||
{/* @ts-ignore */}
|
||||
<DataTable columns={columns} data={data} />
|
||||
</Card>
|
||||
</CardGrid>
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue