Skip to content

Commit

Permalink
Set default schedule (#133)
Browse files Browse the repository at this point in the history
Add option in ... to set default schedule upon opening hydrant
(requested by a user)

---------

Co-authored-by: Diego Temkin <65834932+dtemkin1@users.noreply.github.com>
Co-authored-by: Pratyush Venkatakrishnan <contact@psvenk.com>
  • Loading branch information
3 people authored Feb 2, 2025
1 parent 2b2d7ef commit 842ea88
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/components/ScheduleSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
LuEllipsis,
LuFilePlus2,
LuPencilLine,
LuPin,
LuPinOff,
LuSave,
LuShare2,
LuTrash2,
Expand All @@ -61,13 +63,22 @@ function SelectWithWarn(props: {
const { state, saveId, saves } = props;
const [confirmSave, setConfirmSave] = useState("");
const confirmName = saves.find((save) => save.id === confirmSave)?.name;
const defaultScheduleId = state.defaultSchedule;

const formatScheduleName = (id: string, name: string) => {
return id === defaultScheduleId ? `${name} (default)` : name;
};

return (
<>
<SelectRoot
collection={createListCollection({
items: [
{ label: "Not saved", value: "" },
...saves.map(({ id, name }) => ({ label: name, value: id })),
...saves.map(({ id, name }) => ({
label: formatScheduleName(id, name),
value: id,
})),
],
})}
size="sm"
Expand All @@ -90,7 +101,7 @@ function SelectWithWarn(props: {
<SelectContent>
{saves.map(({ id, name }) => (
<SelectItem item={id} key={id}>
{name}
{formatScheduleName(id, name)}
</SelectItem>
))}
</SelectContent>
Expand Down Expand Up @@ -213,6 +224,7 @@ export function ScheduleSwitcher(props: {
const currentName = saves.find((save) => save.id === saveId)?.name ?? "";
const [isRenaming, setIsRenaming] = useState(false);
const [name, setName] = useState(currentName);
const defaultScheduleId = state.defaultSchedule;

useEffect(() => {
setName(saves.find((save) => save.id === saveId)?.name ?? "");
Expand Down Expand Up @@ -307,6 +319,27 @@ export function ScheduleSwitcher(props: {
</>
)}
</MenuItem>
{saveId && (
<MenuItem
value="toggleDefault"
onClick={() => {
state.defaultSchedule =
defaultScheduleId === saveId ? null : saveId;
}}
>
{defaultScheduleId === saveId ? (
<>
<LuPinOff />
<Box flex="1">Unset as default</Box>
</>
) : (
<>
<LuPin />
<Box flex="1">Set as default</Box>
</>
)}
</MenuItem>
)}
<ExportDialog state={state}>
<MenuItem value="share">
<LuShare2 />
Expand Down
2 changes: 2 additions & 0 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Preferences = {
colorScheme: ColorScheme;
roundedCorners: boolean;
showEventTimes: boolean;
defaultScheduleId: string | null;
showFeedback: boolean;
};

Expand All @@ -20,6 +21,7 @@ export const DEFAULT_PREFERENCES: Preferences = {
colorScheme: COLOR_SCHEME_PRESETS[0],
roundedCorners: false,
showEventTimes: false,
defaultScheduleId: null,
showFeedback: true,
};

Expand Down
27 changes: 25 additions & 2 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,21 @@ export class State {
return url.href;
}

/** Initialize the state from either the URL or localStorage. */
/** Set a schedule as the default schedule */
set defaultSchedule(id: string | null) {
this.preferences = {
...this.preferences,
defaultScheduleId: id,
};
this.updateState();
}

/** Get the current default schedule id */
get defaultSchedule(): string | null {
return this.preferences.defaultScheduleId;
}

/** Initialize the state from either the URL, default schedule, or first schedule. */
initState(): void {
const preferences = this.store.globalGet("preferences");
if (preferences) {
Expand All @@ -432,7 +446,16 @@ export class State {
if (save) {
this.inflate(urldecode(save));
} else {
this.loadSave(this.saves[0]!.id);
// Try to load default schedule if set, otherwise load first save
const defaultScheduleId = this.preferences.defaultScheduleId;
if (
defaultScheduleId &&
this.saves.some((save) => save.id === defaultScheduleId)
) {
this.loadSave(defaultScheduleId);
} else {
this.loadSave(this.saves[0]!.id);
}
}
// Load starred classes from storage
const storedStarred = this.store.get("starredClasses");
Expand Down

0 comments on commit 842ea88

Please sign in to comment.