@rubixstudios/payload-media 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +73 -0
- package/dist/components/media-search/index.d.ts +4 -0
- package/dist/components/media-search/index.d.ts.map +1 -0
- package/dist/components/media-search/index.js +31 -0
- package/dist/components/search-drawer/filters/pexels.d.ts +11 -0
- package/dist/components/search-drawer/filters/pexels.d.ts.map +1 -0
- package/dist/components/search-drawer/filters/pexels.js +48 -0
- package/dist/components/search-drawer/filters/pixabay.d.ts +11 -0
- package/dist/components/search-drawer/filters/pixabay.d.ts.map +1 -0
- package/dist/components/search-drawer/filters/pixabay.js +74 -0
- package/dist/components/search-drawer/filters/unsplash.d.ts +10 -0
- package/dist/components/search-drawer/filters/unsplash.d.ts.map +1 -0
- package/dist/components/search-drawer/filters/unsplash.js +35 -0
- package/dist/components/search-drawer/index.d.ts +9 -0
- package/dist/components/search-drawer/index.d.ts.map +1 -0
- package/dist/components/search-drawer/index.js +306 -0
- package/dist/components/search-drawer/media/heart.d.ts +3 -0
- package/dist/components/search-drawer/media/heart.d.ts.map +1 -0
- package/dist/components/search-drawer/media/heart.js +12 -0
- package/dist/components/search-drawer/media/image.d.ts +10 -0
- package/dist/components/search-drawer/media/image.d.ts.map +1 -0
- package/dist/components/search-drawer/media/image.js +58 -0
- package/dist/components/search-drawer/media/video.d.ts +9 -0
- package/dist/components/search-drawer/media/video.d.ts.map +1 -0
- package/dist/components/search-drawer/media/video.js +55 -0
- package/dist/components/search-drawer/style.css +1 -0
- package/dist/defaults.d.ts +3 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +8 -0
- package/dist/endpoints/handler/pexels.d.ts +12 -0
- package/dist/endpoints/handler/pexels.d.ts.map +1 -0
- package/dist/endpoints/handler/pexels.js +105 -0
- package/dist/endpoints/handler/pixabay.d.ts +12 -0
- package/dist/endpoints/handler/pixabay.d.ts.map +1 -0
- package/dist/endpoints/handler/pixabay.js +112 -0
- package/dist/endpoints/handler/provider.d.ts +20 -0
- package/dist/endpoints/handler/provider.d.ts.map +1 -0
- package/dist/endpoints/handler/provider.js +53 -0
- package/dist/endpoints/handler/unsplash.d.ts +12 -0
- package/dist/endpoints/handler/unsplash.d.ts.map +1 -0
- package/dist/endpoints/handler/unsplash.js +82 -0
- package/dist/endpoints/index.d.ts +3 -0
- package/dist/endpoints/index.d.ts.map +1 -0
- package/dist/endpoints/index.js +131 -0
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/lib/cache.d.ts +7 -0
- package/dist/lib/cache.d.ts.map +1 -0
- package/dist/lib/cache.js +34 -0
- package/dist/types.d.ts +262 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +336 -0
- package/dist/utils/fetchCache.d.ts +2 -0
- package/dist/utils/fetchCache.d.ts.map +1 -0
- package/dist/utils/fetchCache.js +20 -0
- package/dist/utils/getMediaType.d.ts +2 -0
- package/dist/utils/getMediaType.d.ts.map +1 -0
- package/dist/utils/getMediaType.js +4 -0
- package/dist/utils/getProvider.d.ts +6 -0
- package/dist/utils/getProvider.d.ts.map +1 -0
- package/dist/utils/getProvider.js +16 -0
- package/dist/utils/getProviders.d.ts +6 -0
- package/dist/utils/getProviders.d.ts.map +1 -0
- package/dist/utils/getProviders.js +10 -0
- package/dist/utils/requireAccess.d.ts +3 -0
- package/dist/utils/requireAccess.d.ts.map +1 -0
- package/dist/utils/requireAccess.js +13 -0
- package/dist/utils/resolveProvider.d.ts +6 -0
- package/dist/utils/resolveProvider.d.ts.map +1 -0
- package/dist/utils/resolveProvider.js +5 -0
- package/package.json +96 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 rilrom / Riley Langbein
|
|
4
|
+
Copyright (c) 2025-2026 Rubix Studios Pty. Ltd. <https://rubixstudios.com.au>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# PayloadCMS Media Search Plugin
|
|
2
|
+
|
|
3
|
+
PayloadCMS plugin for stock image and video search across Unsplash, Pexels, and Pixabay, with filterable results and instant import.
|
|
4
|
+
|
|
5
|
+
<img width="1899" height="907" alt="image" src="https://github.com/user-attachments/assets/1c19f977-d075-4dfc-9f52-8d9426bc1500" />
|
|
6
|
+
|
|
7
|
+
This project is an independent implementation derived [PayloadBites' Image Search](https://github.com/rilrom/payload-bites/tree/main/packages/image-search) extended with provider-specific image filtering and improved Windows OS compatibility.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pnpm add @rubixstudios/payload-media
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// payload.config.ts
|
|
17
|
+
import { buildConfig } from 'payload/config'
|
|
18
|
+
import { mediaPlugin } from '@rubixstudios/payload-media'
|
|
19
|
+
|
|
20
|
+
export default buildConfig({
|
|
21
|
+
plugins: [
|
|
22
|
+
mediaPlugin({
|
|
23
|
+
disabled: false, // Optional, defaults to false
|
|
24
|
+
access: ({ req: { user } }) => Boolean(user), // Media search access control
|
|
25
|
+
pexels: process.env.API_KEY_PEXELS!,
|
|
26
|
+
pixabay: process.env.API_KEY_PIXABAY!,
|
|
27
|
+
unsplash: process.env.API_KEY_UNSPLASH!,
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Notes
|
|
34
|
+
|
|
35
|
+
API key can be obtained from the respective provider's website.
|
|
36
|
+
|
|
37
|
+
- [Unsplash](https://unsplash.com/developers)
|
|
38
|
+
- [Pexels](https://www.pexels.com/api/)
|
|
39
|
+
- [Pixabay](https://pixabay.com/api/docs/)
|
|
40
|
+
|
|
41
|
+
When using Unplash, utilise your Access Key for the API Token.
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
- Stock image and video search inside Payload Admin
|
|
46
|
+
- Providers: Unsplash, Pexels, Pixabay
|
|
47
|
+
- Provider-specific filters (color, orientation, size, type)
|
|
48
|
+
- Featured and keyword search modes
|
|
49
|
+
- One-click image import
|
|
50
|
+
- Permission-based access control
|
|
51
|
+
|
|
52
|
+
## Liability
|
|
53
|
+
|
|
54
|
+
Rubix Studios, its developers, and contributors bear no responsibility for how images are used.
|
|
55
|
+
Before using any provider, ensure you have reviewed and agreed to their terms and usage policies.
|
|
56
|
+
|
|
57
|
+
## Support
|
|
58
|
+
|
|
59
|
+
For support or inquiries:
|
|
60
|
+
|
|
61
|
+
- LinkedIn: [rubixvi](https://www.linkedin.com/in/rubixvi/)
|
|
62
|
+
- Website: [Rubix Studios](https://rubixstudios.com.au)
|
|
63
|
+
|
|
64
|
+
## Author
|
|
65
|
+
|
|
66
|
+
Rubix Studios Pty. Ltd.
|
|
67
|
+
[https://rubixstudios.com.au](https://rubixstudios.com.au)
|
|
68
|
+
|
|
69
|
+
## Acknowledgments
|
|
70
|
+
|
|
71
|
+
This project is based on the Image Search by Riley Langbein:
|
|
72
|
+
|
|
73
|
+
- [Riley Langbein](https://github.com/rilrom)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/media-search/index.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAsB,MAAM,OAAO,CAAA;AAI1C,eAAO,MAAM,UAAU,yBAAyB,CAAA;AAEhD,eAAO,MAAM,WAAW,yBA8BvB,CAAA"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { Button, Drawer, useConfig, useModal, useUploadControls } from '@payloadcms/ui';
|
|
3
|
+
import React, { useCallback } from 'react';
|
|
4
|
+
import { SearchDrawer } from '../search-drawer/index.js';
|
|
5
|
+
export const drawerSlug = 'payload-media-search';
|
|
6
|
+
export const MediaSearch = ()=>{
|
|
7
|
+
const { config } = useConfig();
|
|
8
|
+
const { setUploadControlFileUrl } = useUploadControls();
|
|
9
|
+
const { openModal, closeModal } = useModal();
|
|
10
|
+
const handleSearchSubmit = useCallback((url)=>{
|
|
11
|
+
if (!url) return;
|
|
12
|
+
setUploadControlFileUrl(url);
|
|
13
|
+
closeModal(drawerSlug);
|
|
14
|
+
}, [
|
|
15
|
+
setUploadControlFileUrl,
|
|
16
|
+
closeModal
|
|
17
|
+
]);
|
|
18
|
+
return /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("span", {
|
|
19
|
+
className: "file-field__orText"
|
|
20
|
+
}, "Or"), /*#__PURE__*/ React.createElement(Button, {
|
|
21
|
+
buttonStyle: "pill",
|
|
22
|
+
size: "small",
|
|
23
|
+
onClick: ()=>openModal(drawerSlug)
|
|
24
|
+
}, "Search media"), /*#__PURE__*/ React.createElement(Drawer, {
|
|
25
|
+
slug: drawerSlug
|
|
26
|
+
}, /*#__PURE__*/ React.createElement(SearchDrawer, {
|
|
27
|
+
serverURL: config.serverURL,
|
|
28
|
+
api: config.routes.api,
|
|
29
|
+
onSelect: handleSearchSubmit
|
|
30
|
+
})));
|
|
31
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ProviderFilters } from '../../../types.js';
|
|
3
|
+
type PexelsFiltersProps = {
|
|
4
|
+
filters: ProviderFilters;
|
|
5
|
+
mediaType: 'image' | 'video';
|
|
6
|
+
setFilters: React.Dispatch<React.SetStateAction<ProviderFilters | null>>;
|
|
7
|
+
baseClass: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const PexelsFilters: ({ filters, mediaType, setFilters, baseClass, }: PexelsFiltersProps) => React.JSX.Element | null;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=pexels.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pexels.d.ts","sourceRoot":"","sources":["../../../../src/components/search-drawer/filters/pexels.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAKL,KAAK,eAAe,EAErB,MAAM,mBAAmB,CAAA;AAE1B,KAAK,kBAAkB,GAAG;IACxB,OAAO,EAAE,eAAe,CAAA;IACxB,SAAS,EAAE,OAAO,GAAG,OAAO,CAAA;IAC5B,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC,CAAA;IACxE,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,aAAa,GAAI,gDAK3B,kBAAkB,6BAyEpB,CAAA"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Select } from '@payloadcms/ui';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { PexelsColours, PexelsOrientation, PexelsSize, PexelsVideoSize } from '../../../types.js';
|
|
4
|
+
export const PexelsFilters = ({ filters, mediaType, setFilters, baseClass })=>{
|
|
5
|
+
if (filters?.provider !== 'pexels') return null;
|
|
6
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
7
|
+
className: `${baseClass}__filters`
|
|
8
|
+
}, mediaType !== 'video' && /*#__PURE__*/ React.createElement(Select, {
|
|
9
|
+
options: PexelsColours,
|
|
10
|
+
value: PexelsColours.find((o)=>o.value === filters.options.color),
|
|
11
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pexels' ? {
|
|
12
|
+
provider: 'pexels',
|
|
13
|
+
options: {
|
|
14
|
+
...prev.options,
|
|
15
|
+
color: opt?.value
|
|
16
|
+
}
|
|
17
|
+
} : prev),
|
|
18
|
+
isClearable: true,
|
|
19
|
+
isSearchable: false,
|
|
20
|
+
placeholder: "Colour"
|
|
21
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
22
|
+
options: mediaType === 'video' ? PexelsVideoSize : PexelsSize,
|
|
23
|
+
value: (mediaType === 'video' ? PexelsVideoSize : PexelsSize).find((o)=>o.value === filters.options.size),
|
|
24
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pexels' ? {
|
|
25
|
+
provider: 'pexels',
|
|
26
|
+
options: {
|
|
27
|
+
...prev.options,
|
|
28
|
+
size: opt?.value
|
|
29
|
+
}
|
|
30
|
+
} : prev),
|
|
31
|
+
isClearable: true,
|
|
32
|
+
isSearchable: false,
|
|
33
|
+
placeholder: "Size"
|
|
34
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
35
|
+
options: PexelsOrientation,
|
|
36
|
+
value: PexelsOrientation.find((o)=>o.value === filters.options.orientation),
|
|
37
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pexels' ? {
|
|
38
|
+
provider: 'pexels',
|
|
39
|
+
options: {
|
|
40
|
+
...prev.options,
|
|
41
|
+
orientation: opt?.value
|
|
42
|
+
}
|
|
43
|
+
} : prev),
|
|
44
|
+
isClearable: true,
|
|
45
|
+
isSearchable: false,
|
|
46
|
+
placeholder: "Orientation"
|
|
47
|
+
}));
|
|
48
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ProviderFilters } from '../../../types.js';
|
|
3
|
+
type PixabayFiltersProps = {
|
|
4
|
+
filters: ProviderFilters;
|
|
5
|
+
mediaType: 'image' | 'video';
|
|
6
|
+
setFilters: React.Dispatch<React.SetStateAction<ProviderFilters | null>>;
|
|
7
|
+
baseClass: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const PixabayFilters: ({ filters, mediaType, setFilters, baseClass, }: PixabayFiltersProps) => React.JSX.Element | null;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=pixabay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pixabay.d.ts","sourceRoot":"","sources":["../../../../src/components/search-drawer/filters/pixabay.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EAML,KAAK,eAAe,EAErB,MAAM,mBAAmB,CAAA;AAE1B,KAAK,mBAAmB,GAAG;IACzB,OAAO,EAAE,eAAe,CAAA;IACxB,SAAS,EAAE,OAAO,GAAG,OAAO,CAAA;IAC5B,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC,CAAA;IACxE,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,gDAK5B,mBAAmB,6BAmHrB,CAAA"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Select } from '@payloadcms/ui';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { PixabayCategories, PixabayColours, PixabayImageType, PixabayOrder, PixabayOrientation } from '../../../types.js';
|
|
4
|
+
export const PixabayFilters = ({ filters, mediaType, setFilters, baseClass })=>{
|
|
5
|
+
if (filters?.provider !== 'pixabay') return null;
|
|
6
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
7
|
+
className: `${baseClass}__filters`
|
|
8
|
+
}, /*#__PURE__*/ React.createElement(Select, {
|
|
9
|
+
options: PixabayCategories,
|
|
10
|
+
value: PixabayCategories.find((o)=>o.value === filters.options.category),
|
|
11
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pixabay' ? {
|
|
12
|
+
provider: 'pixabay',
|
|
13
|
+
options: {
|
|
14
|
+
...prev.options,
|
|
15
|
+
category: opt?.value
|
|
16
|
+
}
|
|
17
|
+
} : prev),
|
|
18
|
+
isClearable: true,
|
|
19
|
+
isSearchable: false,
|
|
20
|
+
placeholder: "Category"
|
|
21
|
+
}), mediaType !== 'video' && /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement(Select, {
|
|
22
|
+
options: PixabayImageType,
|
|
23
|
+
value: PixabayImageType.find((o)=>o.value === filters.options.image_type),
|
|
24
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pixabay' ? {
|
|
25
|
+
provider: 'pixabay',
|
|
26
|
+
options: {
|
|
27
|
+
...prev.options,
|
|
28
|
+
image_type: opt?.value
|
|
29
|
+
}
|
|
30
|
+
} : prev),
|
|
31
|
+
isClearable: true,
|
|
32
|
+
isSearchable: false,
|
|
33
|
+
placeholder: "Type"
|
|
34
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
35
|
+
options: PixabayColours,
|
|
36
|
+
value: PixabayColours.find((o)=>o.value === filters.options.colors),
|
|
37
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pixabay' ? {
|
|
38
|
+
provider: 'pixabay',
|
|
39
|
+
options: {
|
|
40
|
+
...prev.options,
|
|
41
|
+
colors: opt?.value
|
|
42
|
+
}
|
|
43
|
+
} : prev),
|
|
44
|
+
isClearable: true,
|
|
45
|
+
isSearchable: false,
|
|
46
|
+
placeholder: "Colour"
|
|
47
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
48
|
+
options: PixabayOrientation,
|
|
49
|
+
value: PixabayOrientation.find((o)=>o.value === filters.options.orientation),
|
|
50
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pixabay' ? {
|
|
51
|
+
provider: 'pixabay',
|
|
52
|
+
options: {
|
|
53
|
+
...prev.options,
|
|
54
|
+
orientation: opt?.value
|
|
55
|
+
}
|
|
56
|
+
} : prev),
|
|
57
|
+
isClearable: true,
|
|
58
|
+
isSearchable: false,
|
|
59
|
+
placeholder: "Orientation"
|
|
60
|
+
})), /*#__PURE__*/ React.createElement(Select, {
|
|
61
|
+
options: PixabayOrder,
|
|
62
|
+
value: PixabayOrder.find((o)=>o.value === filters.options.order),
|
|
63
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'pixabay' ? {
|
|
64
|
+
provider: 'pixabay',
|
|
65
|
+
options: {
|
|
66
|
+
...prev.options,
|
|
67
|
+
order: opt?.value
|
|
68
|
+
}
|
|
69
|
+
} : prev),
|
|
70
|
+
isClearable: true,
|
|
71
|
+
isSearchable: false,
|
|
72
|
+
placeholder: "Order"
|
|
73
|
+
}));
|
|
74
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type ProviderFilters } from '../../../types.js';
|
|
3
|
+
type UnsplashFiltersProps = {
|
|
4
|
+
filters: ProviderFilters;
|
|
5
|
+
setFilters: React.Dispatch<React.SetStateAction<ProviderFilters | null>>;
|
|
6
|
+
baseClass: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const UnsplashFilters: ({ filters, setFilters, baseClass }: UnsplashFiltersProps) => React.JSX.Element | null;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=unsplash.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unsplash.d.ts","sourceRoot":"","sources":["../../../../src/components/search-drawer/filters/unsplash.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,OAAO,EACL,KAAK,eAAe,EAIrB,MAAM,mBAAmB,CAAA;AAE1B,KAAK,oBAAoB,GAAG;IAC1B,OAAO,EAAE,eAAe,CAAA;IACxB,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,cAAc,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC,CAAA;IACxE,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,eAAe,GAAI,oCAAoC,oBAAoB,6BAgDvF,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Select } from '@payloadcms/ui';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { UnsplashColours, UnsplashOrientation } from '../../../types.js';
|
|
4
|
+
export const UnsplashFilters = ({ filters, setFilters, baseClass })=>{
|
|
5
|
+
if (filters?.provider !== 'unsplash') return null;
|
|
6
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
7
|
+
className: `${baseClass}__filters`
|
|
8
|
+
}, /*#__PURE__*/ React.createElement(Select, {
|
|
9
|
+
options: UnsplashColours,
|
|
10
|
+
value: UnsplashColours.find((o)=>o.value === filters.options.color),
|
|
11
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'unsplash' ? {
|
|
12
|
+
provider: 'unsplash',
|
|
13
|
+
options: {
|
|
14
|
+
...prev.options,
|
|
15
|
+
color: opt?.value
|
|
16
|
+
}
|
|
17
|
+
} : prev),
|
|
18
|
+
isClearable: true,
|
|
19
|
+
isSearchable: false,
|
|
20
|
+
placeholder: "Colour"
|
|
21
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
22
|
+
options: UnsplashOrientation,
|
|
23
|
+
value: UnsplashOrientation.find((o)=>o.value === filters.options.orientation),
|
|
24
|
+
onChange: (opt)=>setFilters((prev)=>prev?.provider === 'unsplash' ? {
|
|
25
|
+
provider: 'unsplash',
|
|
26
|
+
options: {
|
|
27
|
+
...prev.options,
|
|
28
|
+
orientation: opt?.value
|
|
29
|
+
}
|
|
30
|
+
} : prev),
|
|
31
|
+
isClearable: true,
|
|
32
|
+
isSearchable: false,
|
|
33
|
+
placeholder: "Orientation"
|
|
34
|
+
}));
|
|
35
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import './style.css';
|
|
3
|
+
export type SearchDrawerProps = {
|
|
4
|
+
serverURL: string;
|
|
5
|
+
api: string;
|
|
6
|
+
onSelect: (value: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare const SearchDrawer: (props: SearchDrawerProps) => React.JSX.Element;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/search-drawer/index.tsx"],"names":[],"mappings":"AAGA,OAAO,KAA2C,MAAM,OAAO,CAAA;AAe/D,OAAO,aAAa,CAAA;AAIpB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,MAAM,CAAA;IACX,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;CAClC,CAAA;AAED,eAAO,MAAM,YAAY,GAAI,OAAO,iBAAiB,sBA4TpD,CAAA"}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { Pagination, SearchFilter, Select, toast } from '@payloadcms/ui';
|
|
3
|
+
import React, { useCallback, useEffect, useState } from 'react';
|
|
4
|
+
import { MediaOptions } from '../../types.js';
|
|
5
|
+
import { fetchCache } from '../../utils/fetchCache.js';
|
|
6
|
+
import { PexelsFilters } from './filters/pexels.js';
|
|
7
|
+
import { PixabayFilters } from './filters/pixabay.js';
|
|
8
|
+
import { UnsplashFilters } from './filters/unsplash.js';
|
|
9
|
+
import { ImageCard } from './media/image.js';
|
|
10
|
+
import { VideoCard } from './media/video.js';
|
|
11
|
+
import './style.css';
|
|
12
|
+
const baseClass = 'search-media';
|
|
13
|
+
export const SearchDrawer = (props)=>{
|
|
14
|
+
const { serverURL, api, onSelect } = props;
|
|
15
|
+
const [selectedProvider, setSelectedProvider] = useState(null);
|
|
16
|
+
const [options, setOptions] = useState([]);
|
|
17
|
+
const [mediaType, setMediaType] = useState('image');
|
|
18
|
+
const [media, setMedia] = useState(null);
|
|
19
|
+
const [filters, setFilters] = useState(null);
|
|
20
|
+
const [currentPage, setCurrentPage] = useState(null);
|
|
21
|
+
const [totalPages, setTotalPages] = useState(null);
|
|
22
|
+
const [loading, setLoading] = useState(true);
|
|
23
|
+
const [value, setValue] = useState('');
|
|
24
|
+
const defaultError = useCallback(()=>{
|
|
25
|
+
toast.error('Something went wrong.');
|
|
26
|
+
}, []);
|
|
27
|
+
const resetMedia = useCallback(()=>{
|
|
28
|
+
setMedia(null);
|
|
29
|
+
setTotalPages(null);
|
|
30
|
+
setCurrentPage(1);
|
|
31
|
+
}, []);
|
|
32
|
+
const getOptions = useCallback(async ()=>{
|
|
33
|
+
try {
|
|
34
|
+
const response = await fetch(`${serverURL}${api}/providers`);
|
|
35
|
+
const json = await response.json();
|
|
36
|
+
if (json.error) {
|
|
37
|
+
setLoading(false);
|
|
38
|
+
return toast.error(json.error);
|
|
39
|
+
}
|
|
40
|
+
const providers = json.data.map((provider)=>({
|
|
41
|
+
label: provider.name,
|
|
42
|
+
value: provider.key.toLowerCase()
|
|
43
|
+
}));
|
|
44
|
+
setOptions(providers);
|
|
45
|
+
const initial = providers[0];
|
|
46
|
+
if (!initial) {
|
|
47
|
+
setLoading(false);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
setFilters({
|
|
51
|
+
provider: initial.value,
|
|
52
|
+
options: {}
|
|
53
|
+
});
|
|
54
|
+
setSelectedProvider(initial);
|
|
55
|
+
} catch {
|
|
56
|
+
setLoading(false);
|
|
57
|
+
defaultError();
|
|
58
|
+
}
|
|
59
|
+
}, [
|
|
60
|
+
serverURL,
|
|
61
|
+
api,
|
|
62
|
+
defaultError
|
|
63
|
+
]);
|
|
64
|
+
const buildFeatured = useCallback(()=>{
|
|
65
|
+
if (!filters || !mediaType) return '';
|
|
66
|
+
const params = new URLSearchParams();
|
|
67
|
+
switch(filters.provider){
|
|
68
|
+
case 'unsplash':
|
|
69
|
+
{
|
|
70
|
+
const { color, orientation } = filters.options;
|
|
71
|
+
if (color) params.set('color', color);
|
|
72
|
+
if (orientation) params.set('orientation', orientation);
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
case 'pexels':
|
|
76
|
+
{
|
|
77
|
+
const { color, orientation, size } = filters.options;
|
|
78
|
+
if (color) params.set('color', color);
|
|
79
|
+
if (orientation) params.set('orientation', orientation);
|
|
80
|
+
if (size) params.set('size', size);
|
|
81
|
+
if (mediaType === 'video') params.set('media', 'video');
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 'pixabay':
|
|
85
|
+
{
|
|
86
|
+
const { category, image_type, order, orientation, colors } = filters.options;
|
|
87
|
+
if (category) params.set('category', category);
|
|
88
|
+
if (image_type) params.set('image_type', image_type);
|
|
89
|
+
if (order) params.set('order', order);
|
|
90
|
+
if (orientation) params.set('orientation', orientation);
|
|
91
|
+
if (colors) params.set('colors', colors);
|
|
92
|
+
if (mediaType === 'video') params.set('media', 'video');
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const query = params.toString();
|
|
97
|
+
return query ? `?${query}` : '';
|
|
98
|
+
}, [
|
|
99
|
+
filters,
|
|
100
|
+
mediaType
|
|
101
|
+
]);
|
|
102
|
+
const getFeatured = useCallback(async ()=>{
|
|
103
|
+
if (!selectedProvider || !filters || !mediaType) return;
|
|
104
|
+
try {
|
|
105
|
+
setLoading(true);
|
|
106
|
+
const json = await fetchCache(`${serverURL}${api}/providers/${selectedProvider.value}/featured${buildFeatured()}`);
|
|
107
|
+
if (!json) return;
|
|
108
|
+
if (json.error) {
|
|
109
|
+
toast.error(json.error);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
setMedia(json.data.images);
|
|
113
|
+
} catch {
|
|
114
|
+
defaultError();
|
|
115
|
+
} finally{
|
|
116
|
+
setLoading(false);
|
|
117
|
+
}
|
|
118
|
+
}, [
|
|
119
|
+
serverURL,
|
|
120
|
+
api,
|
|
121
|
+
selectedProvider,
|
|
122
|
+
filters,
|
|
123
|
+
buildFeatured,
|
|
124
|
+
mediaType,
|
|
125
|
+
defaultError
|
|
126
|
+
]);
|
|
127
|
+
const buildQuery = useCallback((page = 1)=>{
|
|
128
|
+
if (!filters || !mediaType) return '';
|
|
129
|
+
const params = new URLSearchParams();
|
|
130
|
+
params.set('query', value);
|
|
131
|
+
params.set('page', String(page));
|
|
132
|
+
switch(filters.provider){
|
|
133
|
+
case 'unsplash':
|
|
134
|
+
{
|
|
135
|
+
const { color, orientation } = filters.options;
|
|
136
|
+
if (color) params.set('color', color);
|
|
137
|
+
if (orientation) params.set('orientation', orientation);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'pexels':
|
|
141
|
+
{
|
|
142
|
+
const { color, orientation, size } = filters.options;
|
|
143
|
+
if (color) params.set('color', color);
|
|
144
|
+
if (orientation) params.set('orientation', orientation);
|
|
145
|
+
if (size) params.set('size', size);
|
|
146
|
+
if (mediaType === 'video') params.set('media', 'video');
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
case 'pixabay':
|
|
150
|
+
{
|
|
151
|
+
const { category, image_type, order, orientation, colors } = filters.options;
|
|
152
|
+
if (category) params.set('category', category);
|
|
153
|
+
if (image_type) params.set('image_type', image_type);
|
|
154
|
+
if (order) params.set('order', order);
|
|
155
|
+
if (orientation) params.set('orientation', orientation);
|
|
156
|
+
if (colors) params.set('colors', colors);
|
|
157
|
+
if (mediaType === 'video') params.set('media', 'video');
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return params.toString();
|
|
162
|
+
}, [
|
|
163
|
+
filters,
|
|
164
|
+
value,
|
|
165
|
+
mediaType
|
|
166
|
+
]);
|
|
167
|
+
const getMedia = useCallback(async (page = 1)=>{
|
|
168
|
+
try {
|
|
169
|
+
setLoading(true);
|
|
170
|
+
const json = await fetchCache(`${serverURL}${api}/providers/${selectedProvider?.value}/search?${buildQuery(page)}`);
|
|
171
|
+
if (json.error) return toast.error(json.error);
|
|
172
|
+
setMedia(json.data.images);
|
|
173
|
+
setTotalPages(json.data.totalPages);
|
|
174
|
+
setCurrentPage(page);
|
|
175
|
+
} catch {
|
|
176
|
+
defaultError();
|
|
177
|
+
} finally{
|
|
178
|
+
setLoading(false);
|
|
179
|
+
}
|
|
180
|
+
}, [
|
|
181
|
+
serverURL,
|
|
182
|
+
api,
|
|
183
|
+
selectedProvider?.value,
|
|
184
|
+
defaultError,
|
|
185
|
+
buildQuery
|
|
186
|
+
]);
|
|
187
|
+
const selectImage = async (url, download)=>{
|
|
188
|
+
onSelect(url);
|
|
189
|
+
if (!download) return;
|
|
190
|
+
try {
|
|
191
|
+
await fetch(`${serverURL}${api}/providers/${selectedProvider?.value}/track-download?url=${download}`);
|
|
192
|
+
} catch {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const selectFilter = useCallback((select)=>{
|
|
197
|
+
setFilters({
|
|
198
|
+
provider: select.value,
|
|
199
|
+
options: {}
|
|
200
|
+
});
|
|
201
|
+
setSelectedProvider(select);
|
|
202
|
+
if (select.value === 'unsplash') {
|
|
203
|
+
setMediaType('image');
|
|
204
|
+
}
|
|
205
|
+
resetMedia();
|
|
206
|
+
}, [
|
|
207
|
+
resetMedia
|
|
208
|
+
]);
|
|
209
|
+
const changeFilters = useCallback((search)=>{
|
|
210
|
+
setValue(search);
|
|
211
|
+
resetMedia();
|
|
212
|
+
}, [
|
|
213
|
+
resetMedia
|
|
214
|
+
]);
|
|
215
|
+
useEffect(()=>{
|
|
216
|
+
void getOptions();
|
|
217
|
+
}, [
|
|
218
|
+
getOptions
|
|
219
|
+
]);
|
|
220
|
+
useEffect(()=>{
|
|
221
|
+
if (!selectedProvider?.value || !filters || !mediaType) return;
|
|
222
|
+
if (value.trim().length > 0) {
|
|
223
|
+
void getMedia(1);
|
|
224
|
+
} else {
|
|
225
|
+
void getFeatured();
|
|
226
|
+
}
|
|
227
|
+
}, [
|
|
228
|
+
selectedProvider?.value,
|
|
229
|
+
filters,
|
|
230
|
+
value,
|
|
231
|
+
mediaType,
|
|
232
|
+
getMedia,
|
|
233
|
+
getFeatured
|
|
234
|
+
]);
|
|
235
|
+
return /*#__PURE__*/ React.createElement("div", {
|
|
236
|
+
className: baseClass
|
|
237
|
+
}, /*#__PURE__*/ React.createElement("div", {
|
|
238
|
+
className: `${baseClass}__fields`
|
|
239
|
+
}, /*#__PURE__*/ React.createElement(SearchFilter, {
|
|
240
|
+
label: "",
|
|
241
|
+
handleChange: changeFilters
|
|
242
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
243
|
+
options: MediaOptions.filter((option)=>filters?.provider === 'unsplash' ? option.value === 'image' : true),
|
|
244
|
+
value: {
|
|
245
|
+
label: mediaType === 'image' ? 'Images' : 'Videos',
|
|
246
|
+
value: mediaType
|
|
247
|
+
},
|
|
248
|
+
onChange: (opt)=>{
|
|
249
|
+
const val = opt?.value;
|
|
250
|
+
setMediaType(val);
|
|
251
|
+
resetMedia();
|
|
252
|
+
},
|
|
253
|
+
isClearable: false,
|
|
254
|
+
isSearchable: false,
|
|
255
|
+
isCreatable: false,
|
|
256
|
+
className: `${baseClass}__mediaToggle`
|
|
257
|
+
}), /*#__PURE__*/ React.createElement(Select, {
|
|
258
|
+
options: options,
|
|
259
|
+
value: selectedProvider,
|
|
260
|
+
onChange: (value)=>selectFilter(value),
|
|
261
|
+
isClearable: false,
|
|
262
|
+
isSearchable: false,
|
|
263
|
+
isCreatable: false,
|
|
264
|
+
className: `${baseClass}__options`
|
|
265
|
+
})), filters?.provider === 'pexels' && /*#__PURE__*/ React.createElement(PexelsFilters, {
|
|
266
|
+
filters: filters,
|
|
267
|
+
mediaType: mediaType,
|
|
268
|
+
setFilters: setFilters,
|
|
269
|
+
baseClass: baseClass
|
|
270
|
+
}), filters?.provider === 'pixabay' && /*#__PURE__*/ React.createElement(PixabayFilters, {
|
|
271
|
+
filters: filters,
|
|
272
|
+
mediaType: mediaType,
|
|
273
|
+
setFilters: setFilters,
|
|
274
|
+
baseClass: baseClass
|
|
275
|
+
}), filters?.provider === 'unsplash' && /*#__PURE__*/ React.createElement(UnsplashFilters, {
|
|
276
|
+
filters: filters,
|
|
277
|
+
setFilters: setFilters,
|
|
278
|
+
baseClass: baseClass
|
|
279
|
+
}), loading && /*#__PURE__*/ React.createElement("div", {
|
|
280
|
+
className: `${baseClass}__loading`
|
|
281
|
+
}, "Loading media..."), !loading && media?.length === 0 && /*#__PURE__*/ React.createElement("div", {
|
|
282
|
+
className: `${baseClass}__noResults`
|
|
283
|
+
}, "No media found"), !loading && media && media?.length > 0 && /*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
|
|
284
|
+
className: `${baseClass}__results`
|
|
285
|
+
}, media.map((data)=>/*#__PURE__*/ React.createElement(React.Fragment, {
|
|
286
|
+
key: data.id
|
|
287
|
+
}, mediaType === 'video' ? /*#__PURE__*/ React.createElement(VideoCard, {
|
|
288
|
+
data: data,
|
|
289
|
+
baseClass: baseClass
|
|
290
|
+
}) : /*#__PURE__*/ React.createElement(ImageCard, {
|
|
291
|
+
data: data,
|
|
292
|
+
baseClass: baseClass,
|
|
293
|
+
onSelect: selectImage
|
|
294
|
+
})))), currentPage && totalPages && totalPages > 1 && /*#__PURE__*/ React.createElement("div", {
|
|
295
|
+
className: `${baseClass}__pagination`
|
|
296
|
+
}, /*#__PURE__*/ React.createElement(Pagination, {
|
|
297
|
+
hasNextPage: currentPage < totalPages,
|
|
298
|
+
hasPrevPage: currentPage > 1,
|
|
299
|
+
nextPage: currentPage < totalPages ? currentPage + 1 : undefined,
|
|
300
|
+
numberOfNeighbors: 3,
|
|
301
|
+
page: currentPage,
|
|
302
|
+
prevPage: currentPage > 1 ? currentPage - 1 : undefined,
|
|
303
|
+
totalPages: totalPages,
|
|
304
|
+
onChange: (page)=>getMedia(page)
|
|
305
|
+
}))));
|
|
306
|
+
};
|