File size: 6,911 Bytes
1982de5
 
 
 
 
 
 
 
 
 
 
 
603afa9
1982de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603afa9
 
 
 
 
 
1982de5
7cfb454
1982de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cfb454
1982de5
 
7cfb454
1982de5
 
 
 
 
 
7cfb454
1982de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7cfb454
 
1982de5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
603afa9
1982de5
 
 
603afa9
1982de5
603afa9
1982de5
 
 
603afa9
1982de5
 
 
 
 
 
 
 
 
 
 
603afa9
1982de5
 
 
 
 
603afa9
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
import {JSX} from "preact";
import {useState, useEffect, useCallback} from "preact/hooks";
import "./styles.scss";
import style from "./style.module.scss";
import {Container} from "./components/container";
import {loadTopics, saveTopics} from "./utils/topics";
import {Topic} from "./utils/topics";
import {Topics} from "./components/topics";
import {Topic as TopicComponent} from "./components/topic";
import {Route, routes, RouteSetter} from "./utils/route";
import {Settings} from "./components/settings";
import {Layout} from "./components/layout";
import {fetchSettings, resetSettings, saveSettings} from "./utils/settings";
import {generateTopic, generatePosts} from "./utils/oobabooga";
import {tokenizeTopic} from "./utils/model";

export function App(): JSX.Element {
    const [route, _setRoute] = useState<Route>(routes.home);
    const [page, setPage] = useState<number>(0);
    const [topicId, setTopicId] = useState<Topic["id"] | null>(null);

    // null when loading
    const [topics, setTopics] = useState<Topic[]>(loadTopics);
    const [latestGeneratedTopicId, setLatestGeneratedTopicId] = useState<string|null>(null);
    const [pendingGeneration, setPendingGeneration] = useState<boolean>(false);

    useEffect(() => {
        console.log("save !")
        saveTopics(topics);
    }, [topics]);

    const _generateTopic = async (postsCount: number) => {
        setPendingGeneration(true);
        const topic = await generateTopic(settings, postsCount);
        setLatestGeneratedTopicId(topic.id);
        setTopics(topics => [...topics, topic]);
        setPendingGeneration(false);
    }

    const addPosts = async (topicId: string, postsCount: number) => {
        setPendingGeneration(true);
        const posts = await generatePosts(settings, postsCount, topics.find(t => t.id === topicId));
        const newTopics = [...topics]; // Clone topics to avoid bugs
        const foundIndex = newTopics.findIndex(t => t.id === topicId);
        newTopics[foundIndex].posts = newTopics[foundIndex].posts.concat(posts);
        setTopics(newTopics)
        setPendingGeneration(false);
    }

    // useEffect(() => {
    //     setTopics(loadTopics());
    // }, []);

    const [settings, setSettings] = useState(fetchSettings)

    useEffect(() => {
        saveSettings(settings);
    }, [settings]);

    const resetApp = () => {
        resetSettings();
        setSettings(fetchSettings);
        setTopics([]);
    }

    const updateRoute = useCallback(() => {
        const url = new URL(window.location.href);
        const route = url.searchParams.get("route");
        if (route && route in routes) {
            _setRoute(route as keyof typeof routes);
        }
        const page = url.searchParams.get("page");
        if (page) {
            setPage(parseInt(page));
        }
        const id = url.searchParams.get("id");
        if (id) {
            setTopicId(id);
        }
    }, []);

    // Init page from url
    useEffect(() => {
        updateRoute()
    }, []);

    // Listen for URl change
    useEffect(() => {
        function listener() {
            updateRoute()
        }

        window.addEventListener('popstate', listener);

        return () => {
            window.removeEventListener('popstate', listener)
        }
    }, []);


    // Update URL params on route change
    const setRoute: RouteSetter = useCallback((route: Route, page?: number, id?: string) => {
        const url = new URL(window.location.href);
        url.searchParams.set("route", route);
        _setRoute(route);

        if (page !== undefined) {
            url.searchParams.set("page", String(page));
            setPage(page);
        }   else {
            url.searchParams.delete("page");
            setPage(0);
        }

        if (id !== undefined) {
            url.searchParams.set("id", id);
            setTopicId(id);
        } else {
            url.searchParams.delete("id");
            setTopicId(null);
        }

        const newUrl = url.toString();

        // Avoid to push the same URL mutiple time
        if (newUrl !== window.location.href) {
            window.history.pushState({}, "", newUrl);
        }
    }, []);

    let routeComponent: JSX.Element = undefined;
    let breadcrumbs: string = undefined;
    let title: string = undefined;

    switch (route) {
        case routes.home:
            routeComponent = <Topics
                topics={topics}
                setRoute={setRoute}
                settings={settings}
                setSettings={setSettings}
                generateTopic={_generateTopic}
                pendingGeneration={pendingGeneration}
                latestGeneratedTopicId={latestGeneratedTopicId}
            />
            breadcrumbs = "accueil"
            title = "Liste des sujets"
            break;
        case routes.topic:
            if (topicId === null) {
                routeComponent = <div>Impossible d'afficher le sujet</div>
                breadcrumbs = "accueil"
                title = "Sujet"
            } else {
                if (topics === null) {
                    routeComponent = <div>Chargement...</div>
                    breadcrumbs = `accueil / sujet`
                    title = `Chargement...`
                } else {
                    const topic = topics.find(t => t.id === topicId);
                    routeComponent = <TopicComponent
                        topic={topic}
                        settings={settings}
                        setSettings={setSettings}
                        addPosts={addPosts}
                        pendingGeneration={pendingGeneration}
                    />
                    breadcrumbs = `accueil / ${topic.title}`
                    title = `Sujet : ${topic.title}`
                }
            }

            break;
        case routes.settings:
            routeComponent = <Settings settings={settings} setSettings={setSettings} resetApp={resetApp}/>
            breadcrumbs = "accueil / paramètres"
            title = "Paramètres"
            break;
    }

    return (
        <>
            <header className={style.header}>
                <Container>
                    <h1 className={style.logo}>
                        <a href="#" onClick={e => {
                            e.preventDefault();
                            setRoute(routes.home);
                        }}>
                            JVCGPT
                        </a>
                    </h1>
                </Container>
            </header>
            <main>
                <Container>
                    <Layout
                        breadcrumbs={breadcrumbs}
                        title={title}
                        setRoute={setRoute}
                    >
                        {routeComponent}
                    </Layout>
                </Container>
            </main>
        </>
    );
}