Compare commits
10 Commits
5519c016aa
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c84a583992 | |||
| ea12e01eed | |||
| e0a74eeb9e | |||
| 35043fc9e3 | |||
| 42ceb798d1 | |||
| 5cf8b26737 | |||
| a85950d6ef | |||
| d3bb5c2be1 | |||
| c068e243b0 | |||
| 81ae2650b2 |
@@ -38,7 +38,7 @@
|
|||||||
"class-variance-authority": "^0.7.1",
|
"class-variance-authority": "^0.7.1",
|
||||||
"clsx": "^2.1.1",
|
"clsx": "^2.1.1",
|
||||||
"lucide-react": "^1.16.0",
|
"lucide-react": "^1.16.0",
|
||||||
"nitro": "npm:nitro-nightly@latest",
|
"nitro": "3.0.260429-beta",
|
||||||
"radix-ui": "^1.4.3",
|
"radix-ui": "^1.4.3",
|
||||||
"shadcn": "^4.7.0",
|
"shadcn": "^4.7.0",
|
||||||
"solid-js": "^1.9.12",
|
"solid-js": "^1.9.12",
|
||||||
|
|||||||
962
pnpm-lock.yaml
generated
962
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
allowBuilds:
|
allowBuilds:
|
||||||
'@bufbuild/buf': true
|
'@bufbuild/buf': true
|
||||||
esbuild: set this to true or false
|
esbuild: true
|
||||||
msw: true
|
msw: true
|
||||||
onlyBuiltDependencies:
|
onlyBuiltDependencies:
|
||||||
- esbuild
|
- esbuild
|
||||||
|
|||||||
79
src/components/shortener/createRedirectionForm.tsx
Normal file
79
src/components/shortener/createRedirectionForm.tsx
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import {
|
||||||
|
useConnectMutation,
|
||||||
|
getConnectQueryKey,
|
||||||
|
} from "~/integrations/connect-query/solid";
|
||||||
|
import {
|
||||||
|
createURLRedirection,
|
||||||
|
listURLRedirections,
|
||||||
|
} from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
||||||
|
import useAppForm from "../form/appform";
|
||||||
|
import { Button } from "../ui/button";
|
||||||
|
import { useQueryClient } from "@tanstack/solid-query";
|
||||||
|
|
||||||
|
const CreateRedirectionForm = () => {
|
||||||
|
const mutation = useConnectMutation(createURLRedirection);
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const form = useAppForm(() => ({
|
||||||
|
defaultValues: {
|
||||||
|
url: "",
|
||||||
|
slug: "",
|
||||||
|
},
|
||||||
|
onSubmit: async (values) => {
|
||||||
|
await mutation.mutateAsync({
|
||||||
|
url: values.value.url,
|
||||||
|
shortCode: values.value.slug,
|
||||||
|
});
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: getConnectQueryKey(listURLRedirections),
|
||||||
|
});
|
||||||
|
form.reset();
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
return (
|
||||||
|
<form
|
||||||
|
onSubmit={(e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
form.handleSubmit();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div class="bg-card p-2">
|
||||||
|
<h2 class="p-2 my-2">Create New Redirection</h2>
|
||||||
|
<div class="flex gap-x-2 p-2">
|
||||||
|
<div class="w-1/2">
|
||||||
|
<form.AppField
|
||||||
|
name="slug"
|
||||||
|
children={(field) => (
|
||||||
|
<field.TextField
|
||||||
|
name="slug"
|
||||||
|
label="Slug"
|
||||||
|
description={"The slug which should redirect"}
|
||||||
|
placeholder="https://google.com"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div class="w-full">
|
||||||
|
<form.AppField
|
||||||
|
name="url"
|
||||||
|
children={(field) => (
|
||||||
|
<field.TextField
|
||||||
|
name="url"
|
||||||
|
label="URL"
|
||||||
|
description={"The URL to redirect to"}
|
||||||
|
placeholder="https://google.com"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<Button type="submit" class="my-auto">
|
||||||
|
Create
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CreateRedirectionForm;
|
||||||
73
src/components/shortener/listRedirections.tsx
Normal file
73
src/components/shortener/listRedirections.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { For, Match, Switch } from "solid-js";
|
||||||
|
import { listURLRedirections } from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
||||||
|
import type { URLRedirection } from "~/gen/proto/shorten/v1/shorten_pb";
|
||||||
|
import { useConnectQuery } from "~/integrations/connect-query/solid";
|
||||||
|
|
||||||
|
const ListRedirections = () => {
|
||||||
|
const query = useConnectQuery(listURLRedirections);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div class="bg-card p-4">
|
||||||
|
<h2 class="p-2 my-2">List of Redirections</h2>
|
||||||
|
<Switch fallback={<div>Loading...</div>}>
|
||||||
|
<Match when={query.isError}>
|
||||||
|
<div>Error: {query.error?.message}</div>
|
||||||
|
</Match>
|
||||||
|
<Match when={query.isLoading}>
|
||||||
|
<div>Loading...</div>
|
||||||
|
</Match>
|
||||||
|
<Match when={query.data}>
|
||||||
|
<table class="border w-full border-collapse p-2">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<td class="border p-2">Short Code</td>
|
||||||
|
<td class="border p-2">URL</td>
|
||||||
|
<td class="border p-2">Active</td>
|
||||||
|
<td class="border p-2">Created At</td>
|
||||||
|
<td class="border p-2">Expires At</td>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<For each={query.data?.urlRedirections}>
|
||||||
|
{(item) => <Redirection item={item} />}
|
||||||
|
</For>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</Match>
|
||||||
|
</Switch>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Redirection = (props: { item: URLRedirection }) => {
|
||||||
|
return (
|
||||||
|
<tr>
|
||||||
|
<td class="border p-2">
|
||||||
|
<a
|
||||||
|
href={`https://l.kocoder.xyz/${props.item.shortCode}`}
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
{props.item.shortCode}
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
<td class="border p-2">{props.item.url}</td>
|
||||||
|
<td class="border p-2">{props.item.isActive ? "Yes" : "No"}</td>
|
||||||
|
<td class="border p-2">
|
||||||
|
{props.item.createdAt
|
||||||
|
? new Date(
|
||||||
|
Number(props.item.createdAt.seconds) * 1000,
|
||||||
|
).toLocaleDateString()
|
||||||
|
: ""}
|
||||||
|
</td>
|
||||||
|
<td class="border p-2">
|
||||||
|
{props.item.createdAt
|
||||||
|
? new Date(
|
||||||
|
Number(props.item.expiresAt.seconds) * 1000,
|
||||||
|
).toLocaleDateString()
|
||||||
|
: ""}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default ListRedirections;
|
||||||
@@ -1,10 +1,20 @@
|
|||||||
import { createContext, useContext } from "solid-js";
|
import { createContext, useContext } from "solid-js";
|
||||||
import type { JSX } from "solid-js";
|
import type { JSX } from "solid-js";
|
||||||
import type { Transport, ConnectError } from "@connectrpc/connect";
|
import type { Transport, ConnectError } from "@connectrpc/connect";
|
||||||
import { createQuery } from "@tanstack/solid-query";
|
import { createQuery, createMutation } from "@tanstack/solid-query";
|
||||||
import type { CreateQueryResult, CreateQueryOptions } from "@tanstack/solid-query";
|
import type {
|
||||||
import { createQueryOptions } from "@connectrpc/connect-query";
|
CreateQueryResult,
|
||||||
import type { DescMethodUnary, DescMessage, MessageInitShape, MessageShape } from "@bufbuild/protobuf";
|
CreateQueryOptions,
|
||||||
|
CreateMutationResult,
|
||||||
|
CreateMutationOptions,
|
||||||
|
} from "@tanstack/solid-query";
|
||||||
|
import { createQueryOptions, callUnaryMethod } from "@connectrpc/connect-query";
|
||||||
|
import type {
|
||||||
|
DescMethodUnary,
|
||||||
|
DescMessage,
|
||||||
|
MessageInitShape,
|
||||||
|
MessageShape,
|
||||||
|
} from "@bufbuild/protobuf";
|
||||||
|
|
||||||
const TransportContext = createContext<Transport>();
|
const TransportContext = createContext<Transport>();
|
||||||
|
|
||||||
@@ -31,15 +41,15 @@ export function useTransport() {
|
|||||||
return transport;
|
return transport;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useConnectQuery<
|
export function useConnectQuery<I extends DescMessage, O extends DescMessage>(
|
||||||
I extends DescMessage,
|
|
||||||
O extends DescMessage
|
|
||||||
>(
|
|
||||||
schema: DescMethodUnary<I, O>,
|
schema: DescMethodUnary<I, O>,
|
||||||
input?: MessageInitShape<I>,
|
input?: MessageInitShape<I>,
|
||||||
options?: Omit<CreateQueryOptions<MessageShape<O>, ConnectError>, "queryKey" | "queryFn"> & {
|
options?: Omit<
|
||||||
|
CreateQueryOptions<MessageShape<O>, ConnectError>,
|
||||||
|
"queryKey" | "queryFn"
|
||||||
|
> & {
|
||||||
transport?: Transport;
|
transport?: Transport;
|
||||||
}
|
},
|
||||||
): CreateQueryResult<MessageShape<O>, ConnectError> {
|
): CreateQueryResult<MessageShape<O>, ConnectError> {
|
||||||
const contextTransport = useTransport();
|
const contextTransport = useTransport();
|
||||||
|
|
||||||
@@ -52,3 +62,46 @@ export function useConnectQuery<
|
|||||||
} as any;
|
} as any;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useConnectMutation<
|
||||||
|
I extends DescMessage,
|
||||||
|
O extends DescMessage,
|
||||||
|
Ctx = unknown
|
||||||
|
>(
|
||||||
|
schema: DescMethodUnary<I, O>,
|
||||||
|
options?: Omit<
|
||||||
|
CreateMutationOptions<MessageShape<O>, ConnectError, MessageInitShape<I>, Ctx>,
|
||||||
|
"mutationFn"
|
||||||
|
> & {
|
||||||
|
transport?: Transport;
|
||||||
|
},
|
||||||
|
): CreateMutationResult<MessageShape<O>, ConnectError, MessageInitShape<I>, Ctx> {
|
||||||
|
const contextTransport = useTransport();
|
||||||
|
|
||||||
|
return createMutation(() => {
|
||||||
|
const transport = options?.transport ?? contextTransport;
|
||||||
|
return {
|
||||||
|
...options,
|
||||||
|
mutationFn: async (input: MessageInitShape<I>) => {
|
||||||
|
return callUnaryMethod(transport, schema, input);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getConnectQueryKey<
|
||||||
|
I extends DescMessage,
|
||||||
|
O extends DescMessage
|
||||||
|
>(
|
||||||
|
schema: DescMethodUnary<I, O>,
|
||||||
|
input?: MessageInitShape<I>,
|
||||||
|
): [string, { serviceName: string; methodName: string; input?: MessageInitShape<I> }] {
|
||||||
|
return [
|
||||||
|
"connect-query",
|
||||||
|
{
|
||||||
|
serviceName: schema.parent.typeName,
|
||||||
|
methodName: schema.name,
|
||||||
|
...(input !== undefined ? { input } : {}),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { createConnectTransport } from "@connectrpc/connect-web";
|
|||||||
export function getContext() {
|
export function getContext() {
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
const transport = createConnectTransport({
|
const transport = createConnectTransport({
|
||||||
baseUrl: "http://127.0.0.1:8080",
|
baseUrl: "https://l.kocoder.xyz",
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ function RootComponent() {
|
|||||||
<HeadContent />
|
<HeadContent />
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<QueryClientProvider client={context().queryClient}>
|
<TransportProvider transport={context().transport}>
|
||||||
<TransportProvider transport={context().transport}>
|
<QueryClientProvider client={context().queryClient}>
|
||||||
<Suspense>
|
<Suspense>
|
||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
<Header />
|
<Header />
|
||||||
@@ -56,8 +56,8 @@ function RootComponent() {
|
|||||||
|
|
||||||
<TanStackRouterDevtools />
|
<TanStackRouterDevtools />
|
||||||
</Suspense>
|
</Suspense>
|
||||||
</TransportProvider>
|
</QueryClientProvider>
|
||||||
</QueryClientProvider>
|
</TransportProvider>
|
||||||
<Scripts />
|
<Scripts />
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,30 +1,17 @@
|
|||||||
import { useConnectQuery } from "~/integrations/connect-query/solid";
|
|
||||||
import { createFileRoute } from "@tanstack/solid-router";
|
import { createFileRoute } from "@tanstack/solid-router";
|
||||||
import { listURLRedirections } from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
import ListRedirections from "~/components/shortener/listRedirections";
|
||||||
import { For, Match, Switch } from "solid-js";
|
import CreateRedirectionForm from "~/components/shortener/createRedirectionForm";
|
||||||
|
|
||||||
export const Route = createFileRoute("/shorten")({
|
export const Route = createFileRoute("/shorten")({
|
||||||
component: RouteComponent,
|
component: RouteComponent,
|
||||||
});
|
});
|
||||||
|
|
||||||
function RouteComponent() {
|
function RouteComponent() {
|
||||||
const query = useConnectQuery(listURLRedirections, {});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div class="p-4 flex flex-col gap-y-4">
|
||||||
<Switch fallback={<div>Loading...</div>}>
|
<h1 class="text-xl text-bold">URL Redirections</h1>
|
||||||
<Match when={query.isError}>
|
<CreateRedirectionForm />
|
||||||
<div>Error: {query.error?.message}</div>
|
<ListRedirections />
|
||||||
</Match>
|
|
||||||
<Match when={query.isLoading}>
|
|
||||||
<div>Loading...</div>
|
|
||||||
</Match>
|
|
||||||
<Match when={query.data}>
|
|
||||||
<For each={query.data?.urlRedirections}>
|
|
||||||
{(item) => <div>{item.url}</div>}
|
|
||||||
</For>
|
|
||||||
</Match>
|
|
||||||
</Switch>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,14 @@ globalThis.Response = FastResponse;
|
|||||||
|
|
||||||
export default createServerEntry({
|
export default createServerEntry({
|
||||||
fetch(request) {
|
fetch(request) {
|
||||||
return handler.fetch(request)
|
console.log("==> Server entry fetch start:", request.method, request.url);
|
||||||
|
try {
|
||||||
|
const res = handler.fetch(request);
|
||||||
|
console.log("<== Server entry fetch sync return:", res);
|
||||||
|
return res;
|
||||||
|
} catch (e) {
|
||||||
|
console.error("<== Server entry fetch sync throw:", e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user