Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/SenseNet/sn-client into …
Browse files Browse the repository at this point in the history
…2092/feature/long-text-field-tip-tap-editor
  • Loading branch information
hassanad94 committed May 24, 2024
2 parents 33d2861 + a4c24e0 commit 16f45cc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
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 }} style={options.style} />
}

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

0 comments on commit 16f45cc

Please sign in to comment.