forked from lindylearn/unclutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
80 lines (72 loc) · 2.96 KB
/
App.tsx
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
import React, { useMemo, useReducer, useState } from "react";
import { LindyAnnotation } from "../common/annotations/create";
import { hypothesisSyncFeatureFlag } from "../common/featureFlags";
import { groupAnnotations } from "./common/grouping";
import { useAnnotationSettings, useFeatureFlag } from "./common/hooks";
import AnnotationsList from "./components/AnnotationsList";
import { useAnnotationModifiers, useFetchAnnotations } from "./state/actions";
import { annotationReducer, handleWindowEventFactory } from "./state/local";
export default function App({ url, title }) {
// extension settings
const hypothesisSyncEnabled = useFeatureFlag(hypothesisSyncFeatureFlag);
// annotation settings (updated through events below)
const {
personalAnnotationsEnabled,
setPersonalAnnotationsEnabled,
enableSocialAnnotations,
showAllSocialAnnotations,
setEnableSocialAnnotations,
} = useAnnotationSettings();
// keep local annotations state
const [annotations, mutateAnnotations] = useReducer(annotationReducer, []);
useFetchAnnotations(
url,
personalAnnotationsEnabled,
enableSocialAnnotations,
mutateAnnotations
);
// handlers to modify remote & local state
const {
createReply,
deleteHideAnnotation,
onAnnotationHoverUpdate,
unfocusAnnotation,
updateAnnotation,
} = useAnnotationModifiers(mutateAnnotations);
// receive events from the text highlighting content script code
useMemo(() => {
window.onmessage = handleWindowEventFactory(
mutateAnnotations,
setEnableSocialAnnotations,
setPersonalAnnotationsEnabled,
title
);
}, []);
// group and filter annotations on every local state change (e.g. added, focused)
const [groupedAnnotations, setGroupedAnnotations] = useState<
LindyAnnotation[][]
>([]);
React.useEffect(() => {
const visibleAnnotations = annotations.filter(
(a) => a.isMyAnnotation || showAllSocialAnnotations || a.focused
);
// use large grouping margin to display every annotation properly
const groupedAnnotations = groupAnnotations(visibleAnnotations, 100);
setGroupedAnnotations(groupedAnnotations);
}, [annotations]);
return (
// x margin to show slight shadow (iframe allows no overflow)
<div className="font-paragraph text-gray-700 mx-2">
<AnnotationsList
groupedAnnotations={groupedAnnotations}
hypothesisSyncEnabled={hypothesisSyncEnabled}
showAllSocialAnnotations={showAllSocialAnnotations}
deleteHideAnnotation={deleteHideAnnotation}
onAnnotationHoverUpdate={onAnnotationHoverUpdate}
unfocusAnnotation={unfocusAnnotation}
createReply={createReply}
updateAnnotation={updateAnnotation}
/>
</div>
);
}