Skip to content

Commit

Permalink
add title automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
codegod100 committed Oct 29, 2024
1 parent c9266ab commit c511e95
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/frontpage/app/(app)/post/new/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Input } from "@/lib/components/ui/input";
import { Button } from "@/lib/components/ui/button";
import { Alert, AlertDescription, AlertTitle } from "@/lib/components/ui/alert";
import { Spinner } from "@/lib/components/ui/spinner";
import { grabTitle } from "@/lib/utils";
import {
MAX_POST_TITLE_LENGTH,
MAX_POST_URL_LENGTH,
Expand Down Expand Up @@ -51,8 +52,11 @@ export function NewPostForm() {
id={`${id}-url`}
type="url"
value={url}
onChange={(e) => {
setUrl(e.currentTarget.value);
onChange={async (e) => {
const url = e.currentTarget.value
const title = await grabTitle(url)
setTitle(title)
setUrl(url);
}}
/>
<InputLengthIndicator
Expand Down
7 changes: 7 additions & 0 deletions packages/frontpage/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

export async function grabTitle(url: string): Promise<string> {
url = encodeURI(url);
const response = await fetch(`https://cardyb.bsky.app/v1/extract?url=${url}`)
const body = await response.json()
return body.title
}

export type Prettify<T> = {
[K in keyof T]: T[K];
// eslint-disable-next-line @typescript-eslint/ban-types
Expand Down

0 comments on commit c511e95

Please sign in to comment.