-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a91fc2
commit 8c44ac7
Showing
8 changed files
with
62 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export type * from "./index.types"; | ||
export * from "./unsplash-provider"; | ||
export * from "./unsplash"; |
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
packages/ui/src/components/shared/unsplash/unsplash-context.tsx
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
packages/ui/src/components/shared/unsplash/unsplash-provider.tsx
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
...components/shared/unsplash/index.test.tsx → ...ponents/shared/unsplash/unsplash.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { render } from "@testing-library/react"; | ||
|
||
import { Unsplash } from "."; | ||
import { Unsplash } from "./unsplash"; | ||
|
||
describe("<Unsplash />", () => { | ||
it("should render the default text", () => { | ||
const ui = render(<Unsplash apiKey="" />); | ||
expect(ui.getByPlaceholderText(/Search for an image/i)).toBeDefined(); | ||
expect(ui.getByPlaceholderText(/Search for an image/i)).toBeInTheDocument(); | ||
}); | ||
}); |
18 changes: 8 additions & 10 deletions
18
...nents/shared/unsplash/unsplash-picker.tsx → ...c/components/shared/unsplash/unsplash.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 50 additions & 36 deletions
86
packages/ui/src/components/shared/unsplash/use-unsplash.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,61 @@ | ||
"use client"; | ||
|
||
import useSWR, { type Fetcher } from "swr"; | ||
import { useEffect, useMemo, useState, useTransition } from "react"; | ||
import { createApi } from "unsplash-js"; | ||
|
||
import { defaultImages } from "./constants"; | ||
import type { UnsplashImage } from "./index.types"; | ||
import { useUnsplashInternal } from "./unsplash-context"; | ||
|
||
export interface UnsplashConfig { | ||
query?: string; | ||
count?: number; | ||
interface UnsplashImage { | ||
id: string; | ||
user: { name: string; portfolio_url: string }; | ||
links: { html: string }; | ||
urls: { thumb: string; full: string; regular: string; small: string }; | ||
description?: string; | ||
} | ||
|
||
export const useUnsplash = ({ query = "", count }: UnsplashConfig) => { | ||
const { unsplash } = useUnsplashInternal(); | ||
const fetcher: Fetcher<UnsplashImage[], string> = async (key) => { | ||
let search; | ||
try { | ||
const [, query] = key.split(":"); | ||
if (!query?.length) { | ||
search = await unsplash.photos.getRandom({ | ||
collectionIds: ["317099"], | ||
count, | ||
interface UnsplashConfig { | ||
apiKey: string; | ||
} | ||
|
||
export const useUnsplash = ({ apiKey }: UnsplashConfig) => { | ||
const unsplash = useMemo( | ||
() => createApi({ accessKey: apiKey, fetch }), | ||
[apiKey], | ||
); | ||
const [query, setQuery] = useState(""); | ||
const [images, setImages] = useState<UnsplashImage[]>(); | ||
const [isLoading, startTransition] = useTransition(); | ||
|
||
useEffect(() => { | ||
const fetchImages = async (query: string, count = 24) => { | ||
try { | ||
if (query.length === 0) { | ||
const search = await unsplash.photos.getRandom({ | ||
collectionIds: ["317099"], | ||
count, | ||
}); | ||
if (!search.response) throw new Error("Failed to get random photos"); | ||
return search.response as UnsplashImage[]; | ||
} | ||
|
||
const search = await unsplash.search.getPhotos({ | ||
query, | ||
perPage: count, | ||
}); | ||
if (!search.response) throw new Error("Failed to get random photos"); | ||
return search.response as UnsplashImage[]; | ||
if (!search.response) throw new Error(`Failed to get photos: ${query}`); | ||
console.log(`search res: ${search.response.total}`); | ||
return search.response.results as UnsplashImage[]; | ||
} catch (e) { | ||
console.log("[unsplash] Failed to get images from Unsplash!"); | ||
console.log(e); | ||
return defaultImages as UnsplashImage[]; | ||
} | ||
}; | ||
|
||
search = await unsplash.search.getPhotos({ query, perPage: count }); | ||
if (!search.response) throw new Error(`Failed to get photos: ${query}`); | ||
return search.response.results as UnsplashImage[]; | ||
} catch (e) { | ||
console.log(`[unsplash] Failed to get images from Unsplash!`); | ||
console.log(e); | ||
return defaultImages as UnsplashImage[]; | ||
} | ||
}; | ||
const { data, isLoading, error, mutate } = useSWR<UnsplashImage[], Error>( | ||
`unsplash:${query}`, | ||
fetcher, | ||
{ | ||
onError: (e) => console.log(`[unsplash] Error: ${e.message}`), | ||
revalidateOnFocus: false, | ||
}, | ||
); | ||
return { images: data, isLoading, error, mutate }; | ||
startTransition( | ||
() => void fetchImages(query).then((images) => setImages(images)), | ||
); | ||
}, [unsplash, query]); | ||
|
||
return { isLoading, images, setQuery }; | ||
}; |