Skip to content

Commit

Permalink
[SWIF-264] [Implementation of coding best practices]
Browse files Browse the repository at this point in the history
  • Loading branch information
zakialvi-e64 committed Dec 1, 2023
1 parent a9079e2 commit d07ed18
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 58 deletions.
84 changes: 36 additions & 48 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Transcription extends Plugin {
"mpg",
"mkv",
];
public transcription_engine: TranscriptionEngine;
public transcriptionEngine: TranscriptionEngine;
statusBar: StatusBar;


Expand All @@ -61,8 +61,6 @@ export default class Transcription extends Plugin {
public supabase = createClient(
SUPABASE_URL,
SUPABASE_KEY,
// "https://auth.swiftink.io",
// "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InZjZGVxZ3JzcWFleHBub2dhdWx5Iiwicm9sZSI6ImFub24iLCJpYXQiOjE2ODU2OTM4NDUsImV4cCI6MjAwMTI2OTg0NX0.BBxpvuejw_E-Q_g6SU6G6sGP_6r4KnrP-vHV2JZpAho",
{
auth: {
detectSessionInUrl: false,
Expand All @@ -73,18 +71,38 @@ export default class Transcription extends Plugin {
);
public user: User | null;

private querySelectionOnAuthentication(authString: string, display: string) {
if (authString === ".swiftink-manage-account-btn") {
return document
.querySelectorAll(authString)
.forEach((element) => {
element.innerHTML = `Manage ${this.user?.email}`;
});
}
else {
return document
.querySelectorAll(authString)
.forEach((element) => {
element.setAttribute(
"style",
display,
);
});
}
}

private pendingCommand: { file: TFile | TFile[], parentFile: TFile } | null = null;

private async executePendingCommand(pendingCommand: { file: TFile | TFile[], parentFile: TFile }) {
// Additional checks or setup before starting the transcription process

try {
const filesToTranscribe = Array.isArray(pendingCommand.file) ? pendingCommand.file : [pendingCommand.file];
console.log(filesToTranscribe)


for (const file of filesToTranscribe) {
await this.authenticateAndTranscribe(file, pendingCommand.parentFile);
//console.log(`Transcription result for ${file.name}: ${transcription}`);

}
} catch (error) {
console.error("Error during transcription process:", error);
Expand All @@ -110,7 +128,7 @@ export default class Transcription extends Plugin {
public async transcribeAndWrite(parent_file: TFile, file: TFile) {
if (this.settings.debug) console.log("Transcribing " + file.path);

this.transcription_engine
this.transcriptionEngine
.getTranscription(file)
.then(async (transcription) => {
let fileText = await this.app.vault.read(parent_file);
Expand All @@ -137,8 +155,8 @@ export default class Transcription extends Plugin {
// First check if 402 is in the error message, if so alert the user that they need to pay
if (
error &&
error.message &&
error.message.includes("402")
error?.message &&
error?.message.includes("402")
) {
new Notice(
"You have exceeded the free tier. Please upgrade to a paid plan at swiftink.io/pricing to continue transcribing files. Thanks for using Swiftink!",
Expand All @@ -160,7 +178,7 @@ export default class Transcription extends Plugin {
console.log("Loading Obsidian Transcription");
if (this.settings.debug) console.log("Debug mode enabled");

this.transcription_engine = new TranscriptionEngine(
this.transcriptionEngine = new TranscriptionEngine(
this.settings,
this.app.vault,
this.statusBar,
Expand All @@ -169,7 +187,7 @@ export default class Transcription extends Plugin {
);

// Prompt the user to sign in if the have Swiftink selected and are not signed in
if (this.settings.transcription_engine == "swiftink") {
if (this.settings.transcriptionEngine == "swiftink") {
this.user = await this.supabase.auth.getUser().then((res) => {
return res.data.user || null;
});
Expand Down Expand Up @@ -424,45 +442,15 @@ export default class Transcription extends Plugin {

// Show the settings for user auth/unauth based on whether the user is signed in
if (this.user == null) {
document
.querySelectorAll(".swiftink-unauthed-only")
.forEach((element) => {
element.setAttribute(
"style",
"display: block !important",
);
});
document
.querySelectorAll(".swiftink-authed-only")
.forEach((element) => {
element.setAttribute(
"style",
"display: none !important",
);
});

this.querySelectionOnAuthentication(".swiftink-unauthed-only", "display: block !important");
this.querySelectionOnAuthentication(".swiftink-authed-only", "display: none !important");

} else {
document
.querySelectorAll(".swiftink-unauthed-only")
.forEach((element) => {
element.setAttribute(
"style",
"display: none !important",
);
});
document
.querySelectorAll(".swiftink-authed-only")
.forEach((element) => {
element.setAttribute(
"style",
"display: block !important",
);
});
// Also set the user's email in the settings tab
document
.querySelectorAll(".swiftink-manage-account-btn")
.forEach((element) => {
element.innerHTML = `Manage ${this.user?.email}`;
});
this.querySelectionOnAuthentication(".swiftink-unauthed-only", "display: none !important");
this.querySelectionOnAuthentication(".swiftink-authed-only", "display: block !important");
this.querySelectionOnAuthentication(".swiftink-manage-account-btn", "");

}

// Execute the pending command if there is one
Expand Down
12 changes: 6 additions & 6 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface TranscriptionSettings {
verbosity: number;
whisperASRUrl: string;
debug: boolean;
transcription_engine: string;
transcriptionEngine: string;
embedAdditionalFunctionality: boolean;
embedSummary: boolean;
embedOutline: boolean;
Expand All @@ -35,7 +35,7 @@ const DEFAULT_SETTINGS: TranscriptionSettings = {
verbosity: 1,
whisperASRUrl: "http://localhost:9000",
debug: false,
transcription_engine: "swiftink",
transcriptionEngine: "swiftink",
embedAdditionalFunctionality: true,
embedSummary: true,
embedOutline: true,
Expand Down Expand Up @@ -164,9 +164,9 @@ class TranscriptionSettingTab extends PluginSettingTab {
dropdown
.addOption("swiftink", "Swiftink")
.addOption("whisper_asr", "Whisper ASR")
.setValue(this.plugin.settings.transcription_engine)
.setValue(this.plugin.settings.transcriptionEngine)
.onChange(async (value) => {
this.plugin.settings.transcription_engine = value;
this.plugin.settings.transcriptionEngine = value;
await this.plugin.saveSettings();
// Hide the settings for the other transcription engine
if (value == "swiftink") {
Expand Down Expand Up @@ -473,14 +473,14 @@ class TranscriptionSettingTab extends PluginSettingTab {
// Logic! (the incredible true story)

// Initially hide the settings for the other transcription engine
if (this.plugin.settings.transcription_engine == "swiftink") {
if (this.plugin.settings.transcriptionEngine == "swiftink") {
containerEl.findAll(".swiftink-settings").forEach((element) => {
element.style.display = "block";
});
containerEl.findAll(".whisper-asr-settings").forEach((element) => {
element.style.display = "none";
});
} else if (this.plugin.settings.transcription_engine == "whisper_asr") {
} else if (this.plugin.settings.transcriptionEngine == "whisper_asr") {
containerEl.findAll(".swiftink-settings").forEach((element) => {
element.style.display = "none";
});
Expand Down
8 changes: 4 additions & 4 deletions src/transcribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type TranscriptionBackend = (file: TFile) => Promise<string>;
export class TranscriptionEngine {
settings: TranscriptionSettings;
vault: Vault;
status_bar: StatusBar | null;
statusBar: StatusBar | null;
supabase: SupabaseClient;
app: App;

Expand All @@ -32,7 +32,7 @@ export class TranscriptionEngine {
) {
this.settings = settings;
this.vault = vault;
this.status_bar = statusBar;
this.statusBar = statusBar;
this.supabase = supabase;
this.app = app;
}
Expand Down Expand Up @@ -63,11 +63,11 @@ export class TranscriptionEngine {
async getTranscription(file: TFile): Promise<string> {
if (this.settings.debug)
console.log(
`Transcription engine: ${this.settings.transcription_engine}`,
`Transcription engine: ${this.settings.transcriptionEngine}`,
);
const start = new Date();
this.transcriptionEngine =
this.transcription_engines[this.settings.transcription_engine];
this.transcription_engines[this.settings.transcriptionEngine];
return this.transcriptionEngine(file).then((transcription) => {
if (this.settings.debug)
console.log(`Transcription: ${transcription}`);
Expand Down

0 comments on commit d07ed18

Please sign in to comment.