Remve SeparateInactiveCheckbox and correctly use preferences values

* Expose issue with non-existing calendar value in GraphPreferences
This commit is contained in:
Henrik Giesel 2021-01-22 14:56:41 +01:00
parent d1ada88657
commit b0c2e8c99c
6 changed files with 32 additions and 29 deletions

View file

@ -8,10 +8,12 @@
import type { I18n } from "anki/i18n";
export let sourceData: pb.BackendProto.GraphsOut | null = null;
export let preferences: pb.BackendProto.GraphsPreferencesOut | null = null;
export let revlogRange: RevlogRange;
export let i18n: I18n;
export let nightMode: boolean;
let { calendarFirstDayOfWeek } = preferences;
let graphData: GraphData | null = null;
let bounds = defaultGraphBounds();
@ -25,7 +27,7 @@
let targetYear = maxYear;
$: if (sourceData) {
graphData = gatherData(sourceData);
graphData = gatherData(sourceData, $calendarFirstDayOfWeek);
}
$: {

View file

@ -5,7 +5,6 @@
import type { PreferenceStore } from "./preferences";
import type pb from "anki/backend_proto";
import type { I18n } from "anki/i18n";
import SeparateInactiveCheckbox from "./SeparateInactiveCheckbox.svelte";
export let sourceData: pb.BackendProto.GraphsOut;
export let i18n: I18n;
@ -26,6 +25,7 @@
tableData = renderCards(svg as any, bounds, graphData);
}
const label = i18n.tr(i18n.TR.STATISTICS_COUNTS_SEPARATE_SUSPENDED_BURIED_CARDS);
const total = i18n.tr(i18n.TR.STATISTICS_COUNTS_TOTAL_CARDS);
</script>
@ -58,7 +58,10 @@
<h1>{graphData.title}</h1>
<div class="range-box-inner">
<SeparateInactiveCheckbox {i18n} {cardCountsSeparateInactive} />
<label>
<input type="checkbox" bind:checked={$cardCountsSeparateInactive} />
{label}
</label>
</div>
<div class="counts-outer">

View file

@ -1,14 +0,0 @@
<script lang="typescript">
import type { I18n } from "anki/i18n";
import type { CustomStore } from "./preferences";
export let i18n: I18n;
export let cardCountsSeparateInactive: CustomStore<boolean>;
const label = i18n.tr(i18n.TR.STATISTICS_COUNTS_SEPARATE_SUSPENDED_BURIED_CARDS);
</script>
<label>
<input type="checkbox" bind:checked={$cardCountsSeparateInactive} />
{label}
</label>

View file

@ -6,7 +6,7 @@
@typescript-eslint/no-explicit-any: "off",
*/
import type pb from "anki/backend_proto";
import pb from "anki/backend_proto";
import { interpolateBlues } from "d3-scale-chromatic";
import "d3-transition";
import { select, mouse } from "d3-selection";
@ -41,7 +41,13 @@ interface DayDatum {
date: Date;
}
export function gatherData(data: pb.BackendProto.GraphsOut): GraphData {
type WeekdayType = pb.BackendProto.GraphsPreferencesOut.Weekday;
const Weekday = pb.BackendProto.GraphsPreferencesOut.Weekday; /* enum */
export function gatherData(
data: pb.BackendProto.GraphsOut,
firstDayOfWeek: WeekdayType
): GraphData {
const reviewCount = new Map<number, number>();
for (const review of data.revlog as pb.BackendProto.RevlogEntry[]) {
@ -56,17 +62,17 @@ export function gatherData(data: pb.BackendProto.GraphsOut): GraphData {
}
const timeFunction =
data.firstWeekday === 1
firstDayOfWeek === Weekday.MONDAY
? timeMonday
: data.firstWeekday === 5
: data.firstWeekday === Weekday.FRIDAY
? timeFriday
: data.firstWeekday === 6
: data.firstWeekday === Weekday.SATURDAY
? timeSaturday
: timeSunday;
const weekdayLabels: number[] = [];
for (let i = 0; i < 7; i++) {
weekdayLabels.push((data.firstWeekday + i) % 7);
weekdayLabels.push((firstDayOfWeek + i) % 7);
}
return { reviewCount, timeFunction, weekdayLabels };

View file

@ -25,10 +25,15 @@ export async function getGraphPreferences(): Promise<pb.BackendProto.GraphsPrefe
);
}
export async function setGraphPreferences(prefs: pb.BackendProto.GraphsPreferencesOut): Promise<void> {
export async function setGraphPreferences(
prefs: pb.BackendProto.GraphsPreferencesOut
): Promise<void> {
return (async () => {
await postRequest("/_anki/setGraphPreferences", pb.BackendProto.GraphsPreferencesOut.encode(prefs).finish())
})()
await postRequest(
"/_anki/setGraphPreferences",
pb.BackendProto.GraphsPreferencesOut.encode(prefs).finish()
);
})();
}
// amount of data to fetch from backend

View file

@ -2,7 +2,6 @@ import type pb from "anki/backend_proto";
import { getGraphPreferences, setGraphPreferences } from "./graph-helpers";
import { writable, get } from "svelte/store";
export interface CustomStore<T> {
subscribe: (getter: (value: T) => void) => () => void;
set: (value: T) => void;
@ -29,7 +28,9 @@ function createPreference<T>(
};
}
function preparePreferences(graphsPreferences: pb.BackendProto.GraphsPreferencesOut): PreferenceStore {
function preparePreferences(
graphsPreferences: pb.BackendProto.GraphsPreferencesOut
): PreferenceStore {
const preferences: Partial<PreferenceStore> = {};
function constructPreferences(): pb.BackendProto.GraphsPreferencesOut {
@ -53,5 +54,5 @@ function preparePreferences(graphsPreferences: pb.BackendProto.GraphsPreferences
export async function getPreferences() {
const initialPreferences = await getGraphPreferences();
return preparePreferences(initialPreferences)
return preparePreferences(initialPreferences);
}