Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions plugin/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,57 @@ export class TodoistApiClient {
}
}

public async createTask(content: string, options?: CreateTaskParams): Promise<void> {
const body = snakify({
content: content,
...(options ?? {}),
public async createTask(content: string, options?: CreateTaskParams, useQuickAddEndpoint?: boolean): Promise<void> {
if (useQuickAddEndpoint) {
// Quick add endpoint expects a different payload
const body = new URLSearchParams({
text: content,
...((options?.description ? { description: options.description } : {})),
...((options?.projectId ? { project_id: options.projectId } : {})),
...((options?.sectionId ? { section_id: options.sectionId } : {})),
...((options?.dueDate ? { due_date: options.dueDate } : {})),
...((options?.dueDatetime ? { due_datetime: options.dueDatetime } : {})),
...((options?.labels ? { labels: options.labels.join(",") } : {})),
...((options?.priority ? { priority: String(options.priority) } : {})),
});
await this.doQuickAdd("https://api.todoist.com/sync/v9/quick/add", body);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync API is deprecated and will be removed towards the end of the year, it looks like there is a quick add endpoint on the new API, but it looks slightly different, can we pivot onto that one?

} else {
const body = snakify({
content: content,
...(options ?? {}),
});
await this.do("/tasks", "POST", body);
}
}

private async doQuickAdd(url: string, body: URLSearchParams): Promise<WebResponse> {
const params: RequestParams = {
url,
method: "POST",
headers: {
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/x-www-form-urlencoded",
},
body: body.toString(),
};

debug({
msg: "Sending Todoist Quick Add API request",
context: params,
});
await this.do("/tasks", "POST", body);

const response = await this.fetcher.fetch(params);

debug({
msg: "Received Todoist Quick Add API response",
context: response,
});

if (response.statusCode >= 400) {
throw new TodoistApiError(params, response);
}

return response;
}

public async closeTask(id: TaskId): Promise<void> {
Expand Down
7 changes: 5 additions & 2 deletions plugin/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Repository, type RepositoryReader } from "@/data/repository";
import { SubscriptionManager, type UnsubscribeCallback } from "@/data/subscriptions";
import type { Task } from "@/data/task";
import { Maybe } from "@/utils/maybe";
import { useSettingsStore } from "@/settings";

export enum QueryErrorKind {
BadRequest = 0,
Expand Down Expand Up @@ -38,8 +39,10 @@ class LabelsRepository extends Repository<LabelId, Label> {
export class TodoistAdapter {
public actions = {
closeTask: async (id: TaskId) => await this.closeTask(id),
createTask: async (content: string, params: CreateTaskParams) =>
await this.api.withInner((api) => api.createTask(content, params)),
createTask: async (content: string, params: CreateTaskParams) => {
const { useQuickAddEndpoint } = useSettingsStore.getState();
await this.api.withInner((api) => api.createTask(content, params, useQuickAddEndpoint));
},
};

private readonly api: Maybe<TodoistApiClient> = Maybe.Empty();
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/i18n/langs/nl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const nl: DeepPartial<Translations> = {
label: "Debug-logboek inschakelen",
description: "Of debug-logboeken moeten worden ingeschakeld",
},
},
},
deprecation: {
warningMessage:
"Deze instelling is verouderd en wordt in een toekomstige release verwijderd.",
Expand Down
4 changes: 4 additions & 0 deletions plugin/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const defaultSettings: Settings = {
taskCreationDefaultProject: null,

debugLogging: false,

useQuickAddEndpoint: false,
};

export type Settings = {
Expand All @@ -48,6 +50,8 @@ export type Settings = {
taskCreationDefaultProject: ProjectDefaultSetting;

debugLogging: boolean;

useQuickAddEndpoint: boolean;
};

export const useSettingsStore = create<Settings>(() => ({
Expand Down
16 changes: 16 additions & 0 deletions plugin/src/ui/createTaskModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import "./styles.scss";

import type { Translations } from "@/i18n/translation";
import { OptionsSelector } from "@/ui/createTaskModal/OptionsSelector";
import { Setting } from "@/ui/settings/SettingItem";

export type LinkDestination = "content" | "description";

Expand Down Expand Up @@ -138,6 +139,7 @@ const CreateTaskModalContent: React.FC<CreateTaskProps> = ({
);

const [options, setOptions] = useState<TaskCreationOptions>(initialOptions);
const [useQuickAdd, setUseQuickAdd] = useState(settings.useQuickAddEndpoint);

const isSubmitButtonDisabled = content === "" && options.appendLinkTo !== "content";

Expand Down Expand Up @@ -186,6 +188,10 @@ const CreateTaskModalContent: React.FC<CreateTaskProps> = ({
}

try {
// Persist the last used preference so user choice is remembered
if (useQuickAdd !== settings.useQuickAddEndpoint) {
await plugin.writeOptions({ useQuickAddEndpoint: useQuickAdd });
}
await plugin.services.todoist.actions.createTask(
buildWithLink(content, options.appendLinkTo === "content"),
params,
Expand Down Expand Up @@ -228,6 +234,16 @@ const CreateTaskModalContent: React.FC<CreateTaskProps> = ({
<div className="task-creation-notes">
<ul>{linkDestinationMessage && <li>{linkDestinationMessage}</li>}</ul>
</div>
<div className="task-creation-quick-add-toggle">
<label className="quick-add-toggle-label">
<input
type="checkbox"
checked={useQuickAdd}
onChange={(e) => setUseQuickAdd(e.currentTarget.checked)}
/>
<span>Use natural language (quick add)</span>
</label>
</div>
<hr />
<div className="task-creation-controls">
<div>
Expand Down