tissue/resources/assets/js/components/MetadataPreview.tsx

158 lines
5.6 KiB
TypeScript
Raw Normal View History

2020-08-17 18:23:16 +09:00
import React, { useEffect, useState } from 'react';
2020-08-17 18:17:38 +09:00
import classNames from 'classnames';
2020-08-17 16:00:04 +09:00
import { fetchGet, ResponseError } from '../fetch';
enum MetadataLoadState {
Inactive,
Loading,
Success,
Failed,
}
type Metadata = {
url: string;
title: string;
description: string;
image: string;
expires_at: string | null;
tags: {
name: string;
}[];
};
2020-08-13 00:38:26 +09:00
type Suggestion = {
name: string;
used: boolean;
};
type MetadataPreviewProps = {
2020-08-17 16:00:04 +09:00
link: string;
2020-08-13 00:38:26 +09:00
tags: string[];
2020-08-17 16:00:04 +09:00
onClickTag: (tag: string) => void;
2020-08-13 00:38:26 +09:00
};
const MetadataLoading = () => (
<div className="row no-gutters">
<div className="col-12">
<div className="card-body">
<h6 className="card-title text-center font-weight-bold text-info">
<span className="oi oi-loop-circular" />
</h6>
</div>
</div>
</div>
);
const MetadataLoadFailed = () => (
<div className="row no-gutters">
<div className="col-12">
<div className="card-body">
<h6 className="card-title text-center font-weight-bold text-danger">
<span className="oi oi-circle-x" />
</h6>
</div>
</div>
</div>
);
2020-08-17 16:00:04 +09:00
export const MetadataPreview: React.FC<MetadataPreviewProps> = ({ link, tags, onClickTag }) => {
const [state, setState] = useState(MetadataLoadState.Inactive);
const [metadata, setMetadata] = useState<Metadata | null>(null);
useEffect(() => {
if (link.trim() === '' || !/^https?:\/\//.test(link)) {
setState(MetadataLoadState.Inactive);
setMetadata(null);
return;
}
setState(MetadataLoadState.Loading);
fetchGet('/api/checkin/card', { url: link })
.then((response) => {
if (!response.ok) {
throw new ResponseError(response);
}
return response.json();
})
.then((data) => {
setState(MetadataLoadState.Success);
setMetadata(data);
})
.catch(() => {
setState(MetadataLoadState.Failed);
setMetadata(null);
});
}, [link]);
2020-08-13 00:38:26 +09:00
if (state === MetadataLoadState.Inactive) {
return null;
}
const hasImage = metadata !== null && metadata.image !== '';
const descClasses = classNames({
'col-8': hasImage,
'col-12': !hasImage,
});
const tagClasses = (s: Suggestion) =>
classNames({
'list-inline-item': true,
badge: true,
'badge-primary': !s.used,
'badge-secondary': s.used,
'metadata-tag-item': true,
});
const suggestions =
metadata?.tags.map((t) => ({
name: t.name,
used: tags.indexOf(t.name) !== -1,
})) ?? [];
return (
<div className="form-row">
<div className="form-group col-sm-12">
<div className="card tis-metadata-preview-link-card mb-2 px-0">
{state === MetadataLoadState.Loading ? (
<MetadataLoading />
) : state === MetadataLoadState.Success ? (
<div className="row no-gutters">
2020-08-17 16:00:04 +09:00
{hasImage && (
<div className="col-4 justify-content-center align-items-center">
<img src={metadata?.image} alt="Thumbnail" className="w-100 bg-secondary" />
</div>
)}
2020-08-13 00:38:26 +09:00
<div className={descClasses}>
<div className="card-body">
<h6 className="card-title font-weight-bold">{metadata?.title}</h6>
{suggestions.length > 0 && (
<>
<p className="card-text mb-2">
<br />
<span className="text-secondary">
()
</span>
</p>
<ul className="list-inline d-inline">
{suggestions.map((tag) => (
<li
key={tag.name}
className={tagClasses(tag)}
2020-08-17 16:00:04 +09:00
onClick={() => onClickTag(tag.name)}
2020-08-13 00:38:26 +09:00
>
<span className="oi oi-tag" /> {tag.name}
</li>
))}
</ul>
</>
)}
</div>
</div>
</div>
) : (
<MetadataLoadFailed />
)}
</div>
</div>
</div>
);
};