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

User can set custom icon from relative path #1613

Merged
merged 1 commit into from
May 15, 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
13 changes: 12 additions & 1 deletion apps/sensenet/src/components/Icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import travisCi from '@iconify-icons/logos/travis-ci'
import vercelIcon from '@iconify-icons/logos/vercel-icon'
import React, { CSSProperties, FunctionComponent } from 'react'
import { EventLogEntry } from '../services/EventService'
import { IconFromPath } from './IconFromPath'
import { UserAvatar } from './UserAvatar'

export interface IconOptions {
Expand Down Expand Up @@ -183,9 +184,19 @@ const getIconByName = (name: string | undefined, options: IconOptions) => {
}
}

/* eslint-disable react/display-name */
const getIconByPath = (icon: string | undefined, options: IconOptions) => {
if (!icon || !icon.startsWith('/')) {
return null
}

return <IconFromPath path={icon} options={options} />
}

/* eslint-disable react/display-name */
export const defaultContentResolvers: Array<IconResolver<GenericContent>> = [
{
get: (item, options) => getIconByPath(item.Icon, options),
},
{
get: (item, options) =>
options.repo.schemas.isContentFromType<User>(item, 'User') ? (
Expand Down
48 changes: 48 additions & 0 deletions apps/sensenet/src/components/IconFromPath.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { PathHelper } from '@sensenet/client-utils'
import React, { memo, useEffect, useState } from 'react'
import { IconOptions } from './Icon'

const IconFromPath = ({ path, options }: { path: string; options: IconOptions }) => {
const [icon, setIcon] = useState<string | null>(null)

useEffect(() => {
async function fetchData() {
if (options.repo.iconCache.has(path)) {
const cachedData = options.repo.iconCache.get(path) ?? ''
setIcon(cachedData)
return
}

const imageUrl = PathHelper.joinPaths(options.repo.configuration.repositoryUrl, path)

if (path.endsWith('.svg')) {
const fetchedSvg = await options.repo.fetch(imageUrl, { cache: 'force-cache' })
if (!fetchedSvg.ok) {
return
}
const svg = await fetchedSvg.text().catch(() => '')
options.repo.iconCache.set(path, svg)
setIcon(svg)
return
}

options.repo.iconCache.set(path, imageUrl)
setIcon(imageUrl)
}
fetchData()
}, [options.repo, path])

if (!icon) {
return null
}

if (path.endsWith('.svg')) {
return <span dangerouslySetInnerHTML={{ __html: icon }} />
}

return <img src={icon} alt="icon" style={options.style} />
}

const memoIzedIconFromPath = memo(IconFromPath)

export { memoIzedIconFromPath as IconFromPath }
2 changes: 2 additions & 0 deletions packages/sn-client-core/src/Repository/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ export class Repository implements Disposable {
this.schemas.setSchemas(schemas)
}

public iconCache = new Map<string, string>()

constructor(
config?: RepositoryConfiguration,
private fetchMethod: WindowOrWorkerGlobalScope['fetch'] = window && window.fetch && window.fetch.bind(window),
Expand Down
Loading