pnpm format

This commit is contained in:
Nikita Galaiko 2023-03-30 10:59:56 +02:00
parent 96070b11b3
commit 16b3be9278
4 changed files with 293 additions and 293 deletions

View File

@ -7,185 +7,185 @@ const apiUrl = new URL('/api/', new URL(PUBLIC_API_BASE_URL));
const getUrl = (path: string) => new URL(path, apiUrl).toString();
export type LoginToken = {
token: string;
expires: string;
url: string;
token: string;
expires: string;
url: string;
};
export type User = {
id: number;
name: string;
email: string;
picture: string;
locale: string;
created_at: string;
updated_at: string;
access_token: string;
id: number;
name: string;
email: string;
picture: string;
locale: string;
created_at: string;
updated_at: string;
access_token: string;
};
export type Project = {
name: string;
description: string | null;
repository_id: string;
git_url: string;
created_at: string;
updated_at: string;
name: string;
description: string | null;
repository_id: string;
git_url: string;
created_at: string;
updated_at: string;
};
const parseResponseJSON = async (response: Response) => {
if (response.status === 204 || response.status === 205) {
return null;
} else if (response.status >= 400) {
throw new Error(`HTTP Error ${response.statusText}: ${await response.text()}`);
} else {
return await response.json();
}
if (response.status === 204 || response.status === 205) {
return null;
} else if (response.status >= 400) {
throw new Error(`HTTP Error ${response.statusText}: ${await response.text()}`);
} else {
return await response.json();
}
};
type FetchMiddleware = (f: typeof fetch) => typeof fetch;
const fetchWith = (fetch: typeof window.fetch, ...middlewares: FetchMiddleware[]) =>
middlewares.reduce((f, middleware) => middleware(f), fetch);
middlewares.reduce((f, middleware) => middleware(f), fetch);
const withRequestId: FetchMiddleware = (fetch) => async (url, options) => {
const requestId = nanoid();
if (!options) options = {};
options.headers = {
...options?.headers,
'X-Request-Id': requestId
};
return fetch(url, options);
const requestId = nanoid();
if (!options) options = {};
options.headers = {
...options?.headers,
'X-Request-Id': requestId
};
return fetch(url, options);
};
const withLog: FetchMiddleware = (fetch) => async (url, options) => {
log.info('fetch', url, options);
try {
const resp = await fetch(url, options);
log.info(resp);
return resp;
} catch (e: any) {
log.error('fetch', e);
throw e;
}
log.info('fetch', url, options);
try {
const resp = await fetch(url, options);
log.info(resp);
return resp;
} catch (e: any) {
log.error('fetch', e);
throw e;
}
};
export default (
{ fetch: realFetch }: { fetch: typeof window.fetch } = {
fetch: window.fetch
}
{ fetch: realFetch }: { fetch: typeof window.fetch } = {
fetch: window.fetch
}
) => {
const fetch = fetchWith(realFetch, withRequestId, withLog);
return {
login: {
token: {
create: (): Promise<LoginToken> =>
fetch(getUrl('login/token.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
})
.then(parseResponseJSON)
.then((token) => {
const url = new URL(token.url);
url.host = apiUrl.host;
return {
...token,
url: url.toString()
};
})
},
user: {
get: (token: string): Promise<User> =>
fetch(getUrl(`login/user/${token}.json`), {
method: 'GET'
}).then(parseResponseJSON)
}
},
user: {
get: async (token: string): Promise<User> =>
fetch(getUrl(`user.json`), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
update: async (token: string, params: { name?: string; picture?: File }) => {
const formData = new FormData();
if (params.name) {
formData.append('name', params.name);
}
if (params.picture) {
formData.append('avatar', params.picture);
}
return fetch(getUrl(`user.json`), {
method: 'PUT',
headers: {
'X-Auth-Token': token
},
body: formData
}).then(parseResponseJSON);
}
},
summarize: {
commit: (
token: string,
params: { diff: string; uid?: string }
): Promise<{ message: string }> =>
fetch(getUrl('summarize/commit.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON)
},
projects: {
create: (token: string, params: { name: string; uid?: string }): Promise<Project> =>
fetch(getUrl('projects.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON),
update: (
token: string,
repositoryId: string,
params: { name: string; description?: string }
): Promise<Project> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON),
list: (token: string): Promise<Project[]> =>
fetch(getUrl('projects.json'), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
get: (token: string, repositoryId: string): Promise<Project> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
delete: (token: string, repositoryId: string): Promise<void> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'DELETE',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON)
}
};
const fetch = fetchWith(realFetch, withRequestId, withLog);
return {
login: {
token: {
create: (): Promise<LoginToken> =>
fetch(getUrl('login/token.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({})
})
.then(parseResponseJSON)
.then((token) => {
const url = new URL(token.url);
url.host = apiUrl.host;
return {
...token,
url: url.toString()
};
})
},
user: {
get: (token: string): Promise<User> =>
fetch(getUrl(`login/user/${token}.json`), {
method: 'GET'
}).then(parseResponseJSON)
}
},
user: {
get: async (token: string): Promise<User> =>
fetch(getUrl(`user.json`), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
update: async (token: string, params: { name?: string; picture?: File }) => {
const formData = new FormData();
if (params.name) {
formData.append('name', params.name);
}
if (params.picture) {
formData.append('avatar', params.picture);
}
return fetch(getUrl(`user.json`), {
method: 'PUT',
headers: {
'X-Auth-Token': token
},
body: formData
}).then(parseResponseJSON);
}
},
summarize: {
commit: (
token: string,
params: { diff: string; uid?: string }
): Promise<{ message: string }> =>
fetch(getUrl('summarize/commit.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON)
},
projects: {
create: (token: string, params: { name: string; uid?: string }): Promise<Project> =>
fetch(getUrl('projects.json'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON),
update: (
token: string,
repositoryId: string,
params: { name: string; description?: string }
): Promise<Project> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
},
body: JSON.stringify(params)
}).then(parseResponseJSON),
list: (token: string): Promise<Project[]> =>
fetch(getUrl('projects.json'), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
get: (token: string, repositoryId: string): Promise<Project> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'GET',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON),
delete: (token: string, repositoryId: string): Promise<void> =>
fetch(getUrl(`projects/${repositoryId}.json`), {
method: 'DELETE',
headers: {
'X-Auth-Token': token
}
}).then(parseResponseJSON)
}
};
};

View File

@ -5,148 +5,148 @@ export type ActionInPalette = { component: ComponentType };
export type Action = ActionLink | ActionInPalette;
export namespace Action {
export const isLink = (action: Action): action is ActionLink => 'href' in action;
export const isActionInPalette = (action: Action): action is ActionInPalette =>
'component' in action;
export const isLink = (action: Action): action is ActionLink => 'href' in action;
export const isActionInPalette = (action: Action): action is ActionInPalette =>
'component' in action;
}
export type Command = {
title: string;
description: string;
action: Action;
selected: boolean;
visible: boolean;
icon: ComponentType;
title: string;
description: string;
action: Action;
selected: boolean;
visible: boolean;
icon: ComponentType;
};
export type CommandGroup = {
name: string;
description?: string;
visible: boolean;
commands: Command[];
icon: ComponentType;
name: string;
description?: string;
visible: boolean;
commands: Command[];
icon: ComponentType;
};
export const firstVisibleCommand = (commandGroups: CommandGroup[]): [number, number] => {
for (let i = 0; i < commandGroups.length; i++) {
const group = commandGroups[i];
if (group.visible) {
for (let j = 0; j < group.commands.length; j++) {
const command = group.commands[j];
if (command.visible) {
return [i, j];
}
}
}
}
return [0, 0];
for (let i = 0; i < commandGroups.length; i++) {
const group = commandGroups[i];
if (group.visible) {
for (let j = 0; j < group.commands.length; j++) {
const command = group.commands[j];
if (command.visible) {
return [i, j];
}
}
}
}
return [0, 0];
};
export const lastVisibleCommand = (commandGroups: CommandGroup[]): [number, number] => {
for (let i = commandGroups.length - 1; i >= 0; i--) {
const group = commandGroups[i];
if (group.visible) {
for (let j = group.commands.length - 1; j >= 0; j--) {
const command = group.commands[j];
if (command.visible) {
return [i, j];
}
}
}
}
return [0, 0];
for (let i = commandGroups.length - 1; i >= 0; i--) {
const group = commandGroups[i];
if (group.visible) {
for (let j = group.commands.length - 1; j >= 0; j--) {
const command = group.commands[j];
if (command.visible) {
return [i, j];
}
}
}
}
return [0, 0];
};
export const nextCommand = (
commandGroups: CommandGroup[],
selection: [number, number]
commandGroups: CommandGroup[],
selection: [number, number]
): [number, number] => {
// Next is in the same group
const nextVisibleCommandInGrpIndex = commandGroups[selection[0]].commands
.slice(selection[1] + 1)
.findIndex((command) => command.visible);
if (nextVisibleCommandInGrpIndex !== -1) {
// Found next visible command in the same group
return [selection[0], selection[1] + 1 + nextVisibleCommandInGrpIndex];
}
// Find next visible group
// Next is in the same group
const nextVisibleCommandInGrpIndex = commandGroups[selection[0]].commands
.slice(selection[1] + 1)
.findIndex((command) => command.visible);
if (nextVisibleCommandInGrpIndex !== -1) {
// Found next visible command in the same group
return [selection[0], selection[1] + 1 + nextVisibleCommandInGrpIndex];
}
// Find next visible group
const nextVisibleGroupIndex = commandGroups
.slice(selection[0] + 1)
.findIndex((group) => group.visible);
if (nextVisibleGroupIndex !== -1) {
const firstVisibleCommandIdx = commandGroups[
selection[0] + 1 + nextVisibleGroupIndex
].commands.findIndex((command) => command.visible);
if (firstVisibleCommandIdx !== -1) {
// Found next visible command in the next group
return [selection[0] + 1 + nextVisibleGroupIndex, firstVisibleCommandIdx];
}
}
return selection;
const nextVisibleGroupIndex = commandGroups
.slice(selection[0] + 1)
.findIndex((group) => group.visible);
if (nextVisibleGroupIndex !== -1) {
const firstVisibleCommandIdx = commandGroups[
selection[0] + 1 + nextVisibleGroupIndex
].commands.findIndex((command) => command.visible);
if (firstVisibleCommandIdx !== -1) {
// Found next visible command in the next group
return [selection[0] + 1 + nextVisibleGroupIndex, firstVisibleCommandIdx];
}
}
return selection;
};
export const previousCommand = (
commandGroups: CommandGroup[],
selection: [number, number]
commandGroups: CommandGroup[],
selection: [number, number]
): [number, number] => {
// Previous is in the same group
const previousVisibleCommandInGrpIndex = commandGroups[selection[0]].commands
.slice(0, selection[1])
.reverse()
.findIndex((command) => command.visible);
// Previous is in the same group
const previousVisibleCommandInGrpIndex = commandGroups[selection[0]].commands
.slice(0, selection[1])
.reverse()
.findIndex((command) => command.visible);
if (previousVisibleCommandInGrpIndex !== -1) {
// Found previous visible command in the same group
return [selection[0], selection[1] - 1 - previousVisibleCommandInGrpIndex];
}
// Find previous visible group
const previousVisibleGroupIndex = commandGroups
.slice(0, selection[0])
.reverse()
.findIndex((group) => group.visible);
if (previousVisibleCommandInGrpIndex !== -1) {
// Found previous visible command in the same group
return [selection[0], selection[1] - 1 - previousVisibleCommandInGrpIndex];
}
// Find previous visible group
const previousVisibleGroupIndex = commandGroups
.slice(0, selection[0])
.reverse()
.findIndex((group) => group.visible);
if (previousVisibleGroupIndex !== -1) {
const previousVisibleCommandIndex = commandGroups[
selection[0] - 1 - previousVisibleGroupIndex
].commands
.slice()
.reverse()
.findIndex((command) => command.visible);
if (previousVisibleGroupIndex !== -1) {
const previousVisibleCommandIndex = commandGroups[
selection[0] - 1 - previousVisibleGroupIndex
].commands
.slice()
.reverse()
.findIndex((command) => command.visible);
if (previousVisibleCommandIndex !== -1) {
// Found previous visible command in the previous group
return [selection[0] - 1 - previousVisibleGroupIndex, previousVisibleCommandIndex];
}
}
return selection;
if (previousVisibleCommandIndex !== -1) {
// Found previous visible command in the previous group
return [selection[0] - 1 - previousVisibleGroupIndex, previousVisibleCommandIndex];
}
}
return selection;
};
export const firstVisibleSubCommand = (commands: Command[]): number => {
const firstVisibleGroup = commands.findIndex((command) => command.visible);
if (firstVisibleGroup === -1) {
return 0;
}
return firstVisibleGroup;
const firstVisibleGroup = commands.findIndex((command) => command.visible);
if (firstVisibleGroup === -1) {
return 0;
}
return firstVisibleGroup;
};
export const nextSubCommand = (commands: Command[], selection: number): number => {
const nextVisibleCommandIndex = commands
.slice(selection + 1)
.findIndex((command) => command.visible);
const nextVisibleCommandIndex = commands
.slice(selection + 1)
.findIndex((command) => command.visible);
if (nextVisibleCommandIndex !== -1) {
return selection + 1 + nextVisibleCommandIndex;
}
return selection;
if (nextVisibleCommandIndex !== -1) {
return selection + 1 + nextVisibleCommandIndex;
}
return selection;
};
export const previousSubCommand = (commands: Command[], selection: number): number => {
const previousVisibleCommandIndex = commands
.slice(0, selection)
.reverse()
.findIndex((command) => command.visible);
if (previousVisibleCommandIndex !== -1) {
return selection - 1 - previousVisibleCommandIndex;
}
return selection;
const previousVisibleCommandIndex = commands
.slice(0, selection)
.reverse()
.findIndex((command) => command.visible);
if (previousVisibleCommandIndex !== -1) {
return selection - 1 - previousVisibleCommandIndex;
}
return selection;
};

View File

@ -4,10 +4,10 @@ export { default as statuses } from './statuses';
export { default as activity } from './activity';
export const commit = (params: { projectId: string; message: string; push: boolean }) =>
invoke<boolean>('git_commit', params);
invoke<boolean>('git_commit', params);
export const stage = (params: { projectId: string; paths: Array<string> }) =>
invoke<void>('git_stage', params);
invoke<void>('git_stage', params);
export const unstage = (params: { projectId: string; paths: Array<string> }) =>
invoke<void>('git_unstage', params);
invoke<void>('git_unstage', params);

View File

@ -3,36 +3,36 @@ import { appWindow } from '@tauri-apps/api/window';
import { writable, type Readable } from 'svelte/store';
export type Status = {
path: string;
status: FileStatus;
staged: boolean;
path: string;
status: FileStatus;
staged: boolean;
};
type FileStatus = 'added' | 'modified' | 'deleted' | 'renamed' | 'typeChange' | 'other';
const list = (params: { projectId: string }) =>
invoke<Record<string, [FileStatus, boolean]>>('git_status', params);
invoke<Record<string, [FileStatus, boolean]>>('git_status', params);
const convertToStatuses = (statusesGit: Record<string, [FileStatus, boolean]>): Status[] =>
Object.entries(statusesGit).map((status) => ({
path: status[0],
status: status[1][0],
staged: status[1][1]
}));
Object.entries(statusesGit).map((status) => ({
path: status[0],
status: status[1][0],
staged: status[1][1]
}));
export default async (params: { projectId: string }) => {
const statuses = await list(params).then(convertToStatuses);
const store = writable(statuses);
const statuses = await list(params).then(convertToStatuses);
const store = writable(statuses);
[
`project://${params.projectId}/git/index`,
`project://${params.projectId}/git/activity`,
`project://${params.projectId}/sessions`
].forEach((eventName) => {
appWindow.listen(eventName, async () => {
store.set(await list(params).then(convertToStatuses));
});
});
[
`project://${params.projectId}/git/index`,
`project://${params.projectId}/git/activity`,
`project://${params.projectId}/sessions`
].forEach((eventName) => {
appWindow.listen(eventName, async () => {
store.set(await list(params).then(convertToStatuses));
});
});
return store as Readable<Status[]>;
return store as Readable<Status[]>;
};