Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creditline #224

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@ export default defineConfig({
})
```

### Plugin Config

```ts
// sanity.config.ts
export default defineConfig({
//...
plugins: [
media({
creditLine: {
enabled: true,
// boolean - enables an optional "Credit Line" field in the plugin.
// Used to store credits e.g. photographer, licence info
excludeSources: ['unsplash'],
// string | string[] - when used with 3rd party asset sources, you may
// wish to prevent users overwriting the creditLine based on the `source.name`
},
maximumUploadSize: 10000000
// number - maximum file size (in bytes) that can be uploaded through the plugin interface
})
],
})
```

## Known issues

<details>
Expand Down
19 changes: 19 additions & 0 deletions src/components/DialogAssetEdit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import FormFieldInputText from '../FormFieldInputText'
import FormFieldInputTextarea from '../FormFieldInputTextarea'
import FormSubmitButton from '../FormSubmitButton'
import Image from '../Image'
import {useToolOptions} from '../../contexts/ToolOptionsContext'

type Props = {
children: ReactNode
Expand Down Expand Up @@ -59,10 +60,14 @@ const DialogAssetEdit = (props: Props) => {

const assetTagOptions = useTypedSelector(selectTagSelectOptions(currentAsset))

// Check if credit line options are configured
const {creditLine} = useToolOptions()

const generateDefaultValues = useCallback(
(asset?: Asset): AssetFormData => {
return {
altText: asset?.altText || '',
creditLine: asset?.creditLine || '',
description: asset?.description || '',
originalFilename: asset?.originalFilename || '',
opt: {media: {tags: assetTagOptions}},
Expand Down Expand Up @@ -342,6 +347,20 @@ const DialogAssetEdit = (props: Props) => {
rows={5}
value={currentAsset?.description}
/>
{/* CreditLine */}
{creditLine?.enabled && (
<FormFieldInputText
{...register('creditLine')}
error={errors?.creditLine?.message}
label="Credit"
name="creditLine"
value={currentAsset?.creditLine}
disabled={
formUpdating ||
creditLine?.excludeSources?.includes(currentAsset?.source?.name)
}
/>
)}
</Stack>
</TabPanel>

Expand Down
8 changes: 8 additions & 0 deletions src/components/SearchFacetsControl/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {FACETS} from '../../constants'
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
import useTypedSelector from '../../hooks/useTypedSelector'
import {searchActions} from '../../modules/search'
import {useToolOptions} from '../../contexts/ToolOptionsContext'

const SearchFacetsControl = () => {
// Redux
Expand All @@ -17,11 +18,18 @@ const SearchFacetsControl = () => {

const popoverProps = usePortalPopoverProps()

const {creditLine} = useToolOptions()

const isTool = !selectedDocument

const filteredFacets = FACETS
// Filter facets based on current context, whether it's invoked as a tool, or via selection through via custom asset source.
.filter(facet => {
// Remove credit line filter if it's not enabled
if (!creditLine?.enabled && facet?.type === 'string' && facet?.name === 'creditLine') {
return false
}

if (facet.type === 'group' || facet.type === 'divider') {
return true
}
Expand Down
10 changes: 10 additions & 0 deletions src/config/searchFacets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export const inputs: Record<SearchFacetName, SearchFacetInputProps> = {
type: 'string',
value: ''
},
creditLine: {
assetTypes: ['file', 'image'],
field: 'creditLine',
name: 'creditLine',
operatorType: 'empty',
operatorTypes: ['empty', 'notEmpty', null, 'includes', 'doesNotInclude'],
title: 'Credit',
type: 'string',
value: ''
},
description: {
assetTypes: ['file', 'image'],
field: 'description',
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const FACETS: (SearchFacetDivider | SearchFacetGroup | SearchFacetInputPr
divider,
inputs.title,
inputs.altText,
inputs.creditLine,
inputs.description,
divider,
inputs.isOpaque,
Expand Down
26 changes: 22 additions & 4 deletions src/contexts/ToolOptionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {DropzoneOptions} from 'react-dropzone'

type ContextProps = {
dropzone: Pick<DropzoneOptions, 'maxSize'>
creditLine: MediaToolOptions['creditLine']
}

const ToolOptionsContext = createContext<ContextProps | null>(null)
Expand All @@ -13,10 +14,27 @@ type Props = {
}

export const ToolOptionsProvider = ({options, children}: PropsWithChildren<Props>) => {
const value = useMemo<ContextProps>(
() => ({dropzone: {maxSize: options?.maximumUploadSize}}),
[options?.maximumUploadSize]
)
const value = useMemo<ContextProps>(() => {
let creditLineExcludeSources

if (options?.creditLine?.excludeSources) {
creditLineExcludeSources = Array.isArray(options?.creditLine?.excludeSources)
? options.creditLine.excludeSources
: [options?.creditLine?.excludeSources]
}

return {
dropzone: {maxSize: options?.maximumUploadSize},
creditLine: {
enabled: options?.creditLine?.enabled || false,
excludeSources: creditLineExcludeSources
}
}
}, [
options?.creditLine?.enabled,
options?.creditLine?.excludeSources,
options?.maximumUploadSize
])

return <ToolOptionsContext.Provider value={value}>{children}</ToolOptionsContext.Provider>
}
Expand Down
1 change: 1 addition & 0 deletions src/formSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const tagOptionSchema = z.object({

export const assetFormSchema = z.object({
altText: z.string().trim().optional(),
creditLine: z.string().trim().optional(),
description: z.string().trim().optional(),
opt: z.object({
media: z.object({
Expand Down
4 changes: 4 additions & 0 deletions src/modules/assets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
field: string
}

/**

Check warning on line 63 in src/modules/assets/index.ts

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: '* * NOTE: * `fetchCount` returns the...'
* NOTE:
* `fetchCount` returns the number of items retrieved in the most recent fetch.
* This is a temporary workaround to be able to determine when there are no more items to retrieve.
Expand Down Expand Up @@ -239,6 +239,7 @@
_createdAt,
_updatedAt,
altText,
creditLine,
description,
extension,
metadata {
Expand All @@ -252,6 +253,9 @@
},
originalFilename,
size,
source {
name
},
title,
url
} ${pipe} ${sort} ${selector},
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import {RootReducerState} from '../modules/types'

export type MediaToolOptions = {
maximumUploadSize?: number
creditLine: {
enabled: boolean
excludeSources?: string | string[]
}
}

type CustomFields = {
Expand Down Expand Up @@ -152,6 +156,7 @@ export type FileAsset = SanityAssetDocument &
export type ImageAsset = SanityImageAssetDocument &
CustomFields & {
_type: 'sanity.imageAsset'
creditLine?: string
}

export type MarkDef = {_key: string; _type: string}
Expand Down Expand Up @@ -238,6 +243,7 @@ export type SearchFacetInputStringProps = SearchFacetInputCommon & {

export type SearchFacetName =
| 'altText'
| 'creditLine'
| 'description'
| 'fileName'
| 'height'
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constructFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const constructFilter = ({
// references(*[_type == "media.tag" && name.current == "${searchQuery.trim()}"]._id)
...(searchQuery
? [
groq`[_id, altText, assetId, description, originalFilename, title, url] match '*${searchQuery.trim()}*'`
groq`[_id, altText, assetId, creditLine, description, originalFilename, title, url] match '*${searchQuery.trim()}*'`
]
: []),
// Search facets
Expand Down
Loading