File size: 8,080 Bytes
a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 603afa9 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 a417977 1982de5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
import {Topic, topicsCtx} from "@/contexts/topics";
import {Layout} from "../layout";
import {Spinner} from "../spinner";
import style from "./style.module.scss";
import {Button} from "../button";
import {Input} from "../input";
import {Clipboard, Trash2} from "preact-feather";
import {useContext, useEffect, useMemo, useRef, useState} from "preact/hooks";
import {frenchToIso8601, iso8601ToFrench} from "@/utils/dates";
import {FormGroup} from "../form";
import {Slider} from "../slider";
import cn from "classnames";
import {routeCtx, routes} from "@/contexts/route";
import {settingsCtx} from "@/contexts/settings";
import {Wysiwyg} from "@/components/wysiwyg";
import {Alert} from "@/components/alert";
import {logCtx} from "@/contexts/log";
import {Pagination} from "@/components/pagination";
const topicsPerPage = 10;
export function Topics(props: {
page: number,
setPage: (newPage: number) => void,
}) {
const [importContent, setImportContent] = useState("");
const [topicsContext, setTopicsContext, topicsActions] = useContext(topicsCtx);
const [, , logActions] = useContext(logCtx);
const [settings, setSettings] = useContext(settingsCtx);
useEffect(() => {
if((topicsPerPage * props.page) >= topicsContext.topics.length) {
props.setPage(Math.ceil(topicsContext.topics.length / topicsPerPage)/* - 1*/);
}
}, [topicsContext.topics.length])
const sortedTopics = useMemo(() => {
return [...topicsContext.topics].sort((topicA, topicB) => {
if (topicA.posts.length < 1 || topicB.posts.length < 1) {
return 0;
}
return topicB.posts[topicB.posts.length - 1].date.localeCompare(topicA.posts[topicA.posts.length - 1].date);
});
}, [topicsContext.topics]);
const sortedTopicsSlice = useMemo(
() => sortedTopics.slice(props.page * topicsPerPage, (props.page + 1) * topicsPerPage),
[props.page, sortedTopics]
);
const pendingGeneration = topicsContext.generation === "pending";
return (
<div>
<div className={style.listWrapper}>
<Pagination
pageCount={Math.ceil(sortedTopics.length / topicsPerPage)}
page={props.page}
setPage={props.setPage}
/>
<List topics={sortedTopicsSlice}/>
<Pagination
pageCount={Math.ceil(sortedTopics.length / topicsPerPage)}
page={props.page}
setPage={props.setPage}
/>
</div>
<div>
<h2>Nouveau sujet</h2>
<Alert
lines={
topicsContext.generation === "error" ? [
"Erreur lors de la génération du topic",
"Veuillez regarder le log pour plus d'informations"
] : []
}
/>
<div className={style.generationSettings}>
<FormGroup>
<label for="postCount">Nombre de posts</label>
<Slider
name="postCount"
value={settings.postCount}
onChange={(v) => setSettings({...settings, postCount: v})}
min={1}
max={10}
step={1}
/>
</FormGroup>
</div>
<Button
onClick={() => {
topicsActions.generateTopic(settings, logActions.log)
}}
secondary={true}
loading={pendingGeneration}
>
{pendingGeneration ? "Génération en cours…" : "Générer"}
</Button>
</div>
<hr/>
<div>
<Wysiwyg
showTitle={true}
onSubmit={(user, title, text) => {
// setLastAddedTopicId(topicsActions.addTopic(user, title, text));
topicsActions.addTopic(user, title, text)
props.setPage(0);
window.scrollTo({top: 0, behavior: 'smooth'});
}}
disabled={pendingGeneration}
/>
</div>
<hr/>
<div>
<Alert
lines={
topicsContext.import === "error" ? [
"Erreur lors de l'importation du sujet",
] : []
}
/>
<div className={style.import}>
<textarea
placeholder="Importer un sujet…"
name="import"
id="import"
cols="30"
rows="10"
value={importContent}
onInput={(e) => setImportContent((e.target as HTMLTextAreaElement).value)}
/>
</div>
<Button
onClick={() => {
topicsActions.importTopic(importContent);
setImportContent("");
}}
secondary={true}
>
Importer
</Button>
</div>
</div>
)
}
function List(props: {
topics: Topic[],
// latestGeneratedTopicId: string | null,
}) {
return (
<ul className={style.list}>
<li className={style.head}>
<span>Sujet</span>
<span>Auteur</span>
<span>NB</span>
<span>Dernier msg</span>
<span></span>
</li>
{props.topics.length < 1 && <li><span>Aucun sujet</span><span/><span/><span/><span/></li>}
{props.topics.map(topic => <TopicElement topic={topic}/>)}
</ul>
)
}
function TopicElement(props: {
topic: Topic;
}) {
const topic = props.topic;
const [, setRoute] = useContext(routeCtx);
const [, , topicsActions] = useContext(topicsCtx);
let isRecent: boolean = false;
if (topic.posts.length > 0) {
const firstPost = topic.posts[0];
const dateDelta = new Date() - new Date(firstPost.generationDate || firstPost.date); // difference in ms
// If the topic is more recent than 3 sec
if (dateDelta < 3000) {
isRecent = true;
}
}
return (
<li className={cn({[style.highlight]: isRecent})}>
<span>
<a href={routes.topic(topic.id, 1).location} onClick={(e) => {
e.preventDefault();
setRoute(routes.topic(topic.id, 1));
}}>
{topic.title}
</a>
</span>
<span>{topic.posts[0].user}</span>
<span>{topic.posts.length}</span>
<span>{iso8601ToFrench(topic.posts[topic.posts.length - 1].date)}</span>
<span>
<span title="Copier le sujet">
<Clipboard
size={16}
className={style.clipboard}
onClick={() => {
const json = JSON.stringify(topic);
navigator.clipboard.writeText(json);
}}
/>
</span>
<span title="Supprimer le sujet">
<Trash2
size={16}
className={style.trash}
onClick={() => topicsActions.deleteTopic(topic.id)}
/>
</span>
</span>
</li>
)
} |