Skip to content

Commit

Permalink
Rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
berhalak committed Feb 5, 2025
1 parent 2013e8c commit 7781e82
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
49 changes: 17 additions & 32 deletions app/client/ui/DocumentSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ import {commonUrls, GristLoadConfig} from 'app/common/gristUrls';
import {not, propertyCompare} from 'app/common/gutil';
import {getCurrency, locales} from 'app/common/Locales';
import {isOwner, isOwnerOrEditor} from 'app/common/roles';
import {DOCTYPE_NORMAL, DOCTYPE_TEMPLATE, DOCTYPE_TUTORIAL, DocumentType} from 'app/common/UserAPI';
import {
AttachmentTransferStatus,
DOCTYPE_NORMAL,
DOCTYPE_TEMPLATE,
DOCTYPE_TUTORIAL,
DocumentType
} from 'app/common/UserAPI';
import {
Computed,
Disposable,
Expand All @@ -42,8 +48,6 @@ import {
Observable,
styled
} from 'grainjs';
import {AttachmentLocationSummary, AttachmentTransferStatus} from 'app/common/UserAPI';
import {Computed, Disposable, dom, fromKo, IDisposableOwner, makeTestId, Observable, styled} from 'grainjs';
import * as moment from 'moment-timezone';

const t = makeT('DocumentSettings');
Expand All @@ -70,45 +74,39 @@ export class DocSettingsPage extends Disposable {
}

public buildDom() {
const INTERNAL = 'internal', EXTERNAL = 'external';
const canChangeEngine = getSupportedEngineChoices().length > 0;
const docPageModel = this._gristDoc.docPageModel;
const isTimingOn = this._gristDoc.isTimingOn;
const isDocOwner = isOwner(docPageModel.currentDoc.get());
const isDocEditor = isOwnerOrEditor(docPageModel.currentDoc.get());
const storageType = Computed.create(this, use => {
const id = use(this._docInfo.documentSettingsJson).attachmentStoreId;
return id ? 'EXTERNAL' : 'INTERNAL';
return id ? EXTERNAL : INTERNAL;
});
storageType.onWrite(async (val) => {
if (val === 'EXTERNAL') {
if (stores.get().length === 0) {
throw new Error("No external stores available");
}
await this._docInfo.attachmentStoreId.setAndSave(stores.get()[0]);
} else {
await this._docInfo.attachmentStoreId.setAndSave(undefined);
}
await this._gristDoc.docApi.setAttachmentStore(val);
});
const storageOptions = [{value: 'INTERNAL', label: 'Internal'}, {value: 'EXTERNAL', label: 'External'}];
const storageOptions = [{value: INTERNAL, label: 'Internal'}, {value: EXTERNAL, label: 'External'}];

const transferStatus = Observable.create<AttachmentTransferStatus|null>(this, null);
const locationSummary = Observable.create<Partial<AttachmentLocationSummary>>(this, {});
const locationSummary = Computed.create(this, use => use(transferStatus)?.locationSummary || null);

const inProgress = Computed.create(this, use => !!use(transferStatus)?.status.isRunning);
const allInCurrent = Computed.create(this, use => {
const {summary} = use(locationSummary);
const summary = use(locationSummary);
const current = use(storageType);
return summary && summary === current;
});
const stores = Observable.create(this, [] as string[]);
const stores = Observable.create(this, [INTERNAL, EXTERNAL] as string[]);

const stillInternal = Computed.create(this, use => {
const currentExternal = use(storageType) === 'EXTERNAL';
const currentExternal = use(storageType) === EXTERNAL;
return currentExternal && (use(inProgress) || !use(allInCurrent));
});

const stillExternal = Computed.create(this, use => {
const currentInternal = use(storageType) === 'INTERNAL';
const currentInternal = use(storageType) === INTERNAL;
return currentInternal && (use(inProgress) || !use(allInCurrent));
});

Expand All @@ -117,26 +115,14 @@ export class DocSettingsPage extends Disposable {
transferStatus.set(s);
});

const refreshLocationSummary = () => this._gristDoc.docApi.getAttachmentLocationSummary().then(s => {
if (this.isDisposed()) { return; }
locationSummary.set(s);
});

const refreshStoreIds = async () => {
const list = await this._gristDoc.docApi.listAllAttachmentStoreIds();
if (this.isDisposed()) { return; }
stores.set(list);
};

const beginTransfer = async () => {
await this._gristDoc.docApi.transferAllAttachments();
await refreshStatus();
};

const attachmentsReady = Observable.create(this, false);
refreshStatus()
.then(refreshLocationSummary)
.then(refreshStoreIds)
.then(refreshStatus)
.then(() => attachmentsReady.set(true))
.catch(reportError);

Expand All @@ -146,7 +132,6 @@ export class DocSettingsPage extends Disposable {
try {
if (inProgress.get()) {
await refreshStatus();
await refreshLocationSummary();
console.log(transferStatus.get());
}
await new Promise(resolve => setTimeout(resolve, 1000));
Expand Down
30 changes: 19 additions & 11 deletions app/common/UserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import {
WebhookUpdate
} from 'app/common/Triggers';
import {addCurrentOrgToPath, getGristConfig} from 'app/common/urlUtils';
import { AxiosProgressEvent } from 'axios';
import omitBy from 'lodash/omitBy';
import {StringUnion} from 'app/common/StringUnion';
import {AttachmentStore} from 'app/plugin/DocApiTypes';
import {AxiosProgressEvent} from 'axios';
import omitBy from 'lodash/omitBy';


export type {FullUser, UserProfile};
Expand Down Expand Up @@ -570,8 +571,14 @@ export interface DocAPI {
* Returns the status of the attachment transfer.
*/
getAttachmentTransferStatus(): Promise<AttachmentTransferStatus>;
getAttachmentLocationSummary(): Promise<AttachmentLocationSummary>;
listAllAttachmentStoreIds(): Promise<string[]>;
/**
* Retries type of attachment storage used by the document.
*/
getAttachmentStore(): Promise<{type: AttachmentStore}>;
/**
* Sets the attachment storage used by the document.
*/
setAttachmentStore(type: AttachmentStore): Promise<void>;
}

// Operations that are supported by a doc worker.
Expand Down Expand Up @@ -1213,12 +1220,15 @@ export class DocAPIImpl extends BaseAPI implements DocAPI {
return this.requestJson(`${this._url}/attachments/transferStatus`);
}

public async getAttachmentLocationSummary(): Promise<AttachmentLocationSummary> {
return this.requestJson(`${this._url}/attachments/locationSummary`);
public async getAttachmentStore(): Promise<{type: AttachmentStore}> {
return this.requestJson(`${this._url}/attachments/store`);
}

public async listAllAttachmentStoreIds(): Promise<string[]> {
return this.requestJson(`${this._url}/attachments/listAllStoreIds`);
public async setAttachmentStore(type: AttachmentStore): Promise<void> {
await this.request(`${this._url}/attachments/store`, {
method: 'POST',
body: JSON.stringify({type}),
});
}

private _getRecords(tableId: string, endpoint: 'data' | 'records', options?: GetRowsParams): Promise<any> {
Expand All @@ -1238,11 +1248,9 @@ export interface AttachmentTransferStatus {
pendingTransferCount: number;
isRunning: boolean;
};
locationSummary: DocAttachmentsLocation;
}

export interface AttachmentLocationSummary {
summary: 'INTERNAL'|'EXTERNAL'|'MIXED';
}

/**
* Represents information to build public doc worker url.
Expand Down
4 changes: 4 additions & 0 deletions app/gen-server/lib/DocApiForwarder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class DocApiForwarder {
app.use('/api/docs/:docId/timing/start', withDoc);
app.use('/api/docs/:docId/timing/stop', withDoc);
app.use('/api/docs/:docId/forms/:vsId', withDoc);
app.use('/api/docs/:docId/attachments/transferStatus', withDoc);
app.use('/api/docs/:docId/attachments/transferAll', withDoc);
app.use('/api/docs/:docId/attachments/store', withDoc);

app.use('^/api/docs$', withoutDoc);
}

Expand Down
5 changes: 4 additions & 1 deletion app/plugin/DocApiTypes-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ export const SqlPost = t.iface([], {
});

export const SetAttachmentStorePost = t.iface([], {
"type": t.union(t.lit("internal"), t.lit("external")),
"type": "AttachmentStore",
});

export const AttachmentStore = t.union(t.lit('internal'), t.lit('external'));

const exportedTypeSuite: t.ITypeSuite = {
NewRecord,
NewRecordWithStringId,
Expand All @@ -113,5 +115,6 @@ const exportedTypeSuite: t.ITypeSuite = {
TablesPatch,
SqlPost,
SetAttachmentStorePost,
AttachmentStore,
};
export default exportedTypeSuite;
5 changes: 4 additions & 1 deletion app/plugin/DocApiTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export interface SqlPost {
// limitations of API node-sqlite3 exposes.
}


export interface SetAttachmentStorePost {
type: "internal" | "external"
type: AttachmentStore
}

export type AttachmentStore = 'internal' | 'external';
4 changes: 0 additions & 4 deletions sandbox/watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

set -x

. .devenv

"$@"

NO_NODEMON=false
for arg in $@; do
if [[ $arg == "--no-nodemon" ]]; then
Expand Down

0 comments on commit 7781e82

Please sign in to comment.