2021-04-13 11:02:41 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-03-21 22:06:25 +01:00
|
|
|
<script lang="typescript">
|
2021-03-22 15:41:43 +01:00
|
|
|
import type { Writable } from "svelte/store";
|
2021-04-16 03:10:39 +02:00
|
|
|
import type { PreferenceRaw, PreferencePayload } from "sveltelib/preferences";
|
2021-03-22 15:41:43 +01:00
|
|
|
|
2021-04-22 19:55:26 +02:00
|
|
|
import pb from "lib/backend_proto";
|
|
|
|
import { postRequest } from "lib/postrequest";
|
2021-04-14 23:46:13 +02:00
|
|
|
|
2021-03-22 03:06:53 +01:00
|
|
|
import useAsync from "sveltelib/async";
|
|
|
|
import useAsyncReactive from "sveltelib/asyncReactive";
|
2021-04-14 23:46:13 +02:00
|
|
|
import { getPreferences } from "sveltelib/preferences";
|
2021-03-21 22:06:25 +01:00
|
|
|
|
2021-04-14 23:56:58 +02:00
|
|
|
import { daysToRevlogRange } from "./graph-helpers";
|
2021-03-21 22:06:25 +01:00
|
|
|
|
2021-03-22 02:44:08 +01:00
|
|
|
export let search: Writable<string>;
|
|
|
|
export let days: Writable<number>;
|
2021-03-21 22:06:25 +01:00
|
|
|
|
2021-04-14 23:56:58 +02:00
|
|
|
async function getGraphData(
|
|
|
|
search: string,
|
|
|
|
days: number
|
|
|
|
): Promise<pb.BackendProto.GraphsOut> {
|
|
|
|
return pb.BackendProto.GraphsOut.decode(
|
|
|
|
await postRequest("/_anki/graphData", JSON.stringify({ search, days }))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getGraphPreferences(): Promise<pb.BackendProto.GraphPreferences> {
|
|
|
|
return pb.BackendProto.GraphPreferences.decode(
|
|
|
|
await postRequest("/_anki/graphPreferences", JSON.stringify({}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setGraphPreferences(
|
|
|
|
prefs: PreferencePayload<pb.BackendProto.GraphPreferences>
|
|
|
|
): Promise<void> {
|
|
|
|
await postRequest(
|
|
|
|
"/_anki/setGraphPreferences",
|
2021-04-20 09:39:47 +02:00
|
|
|
pb.BackendProto.GraphPreferences.encode(prefs).finish()
|
2021-04-14 23:56:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-21 23:45:59 +01:00
|
|
|
const {
|
|
|
|
loading: graphLoading,
|
2021-03-22 00:40:19 +01:00
|
|
|
error: graphError,
|
2021-03-21 23:45:59 +01:00
|
|
|
value: graphValue,
|
2021-03-22 02:44:08 +01:00
|
|
|
} = useAsyncReactive(() => getGraphData($search, $days), [search, days]);
|
2021-03-21 23:45:59 +01:00
|
|
|
|
2021-03-22 15:25:49 +01:00
|
|
|
const {
|
|
|
|
loading: prefsLoading,
|
|
|
|
error: prefsError,
|
|
|
|
value: prefsValue,
|
2021-04-14 23:46:13 +02:00
|
|
|
} = useAsync(() =>
|
|
|
|
getPreferences(
|
|
|
|
getGraphPreferences,
|
|
|
|
setGraphPreferences,
|
|
|
|
pb.BackendProto.GraphPreferences.toObject.bind(
|
|
|
|
pb.BackendProto.GraphPreferences
|
2021-04-16 03:10:39 +02:00
|
|
|
) as (
|
|
|
|
preferences: pb.BackendProto.GraphPreferences,
|
|
|
|
options: { defaults: boolean }
|
|
|
|
) => PreferenceRaw<pb.BackendProto.GraphPreferences>
|
2021-04-14 23:46:13 +02:00
|
|
|
)
|
|
|
|
);
|
2021-03-21 22:06:25 +01:00
|
|
|
|
|
|
|
$: revlogRange = daysToRevlogRange($days);
|
2021-03-22 00:40:19 +01:00
|
|
|
|
|
|
|
$: {
|
|
|
|
if ($graphError) {
|
2021-03-22 02:44:08 +01:00
|
|
|
alert($graphError);
|
2021-03-22 00:40:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$: {
|
|
|
|
if ($prefsError) {
|
2021-03-22 02:44:08 +01:00
|
|
|
alert($prefsError);
|
2021-03-22 00:40:19 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 22:06:25 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<slot
|
2021-03-21 22:35:53 +01:00
|
|
|
{revlogRange}
|
2021-03-21 23:45:59 +01:00
|
|
|
loading={$graphLoading || $prefsLoading}
|
|
|
|
sourceData={$graphValue}
|
|
|
|
preferences={$prefsValue} />
|