mirror of
https://github.com/versia-pub/frontend.git
synced 2026-03-13 03:29:16 +01:00
refactor: ♻️ Make auth store require less null checks
This commit is contained in:
parent
68e23a818a
commit
b23ed66401
32 changed files with 111 additions and 124 deletions
|
|
@ -38,34 +38,55 @@ export const useAuthStore = defineStore("auth", {
|
|||
applications: {},
|
||||
}),
|
||||
getters: {
|
||||
identity(state): Identity | null {
|
||||
return state.activeIdentityId
|
||||
? state.identities.find(
|
||||
(id) => id.id === state.activeIdentityId,
|
||||
) || null
|
||||
: null;
|
||||
identity(state): Identity {
|
||||
if (state.activeIdentityId === null) {
|
||||
throw new Error("This functionality requires authentication.");
|
||||
}
|
||||
|
||||
const identity = state.identities.find(
|
||||
(id) => id.id === state.activeIdentityId,
|
||||
);
|
||||
|
||||
if (!identity) {
|
||||
throw new Error("This functionality requires authentication.");
|
||||
}
|
||||
|
||||
return identity;
|
||||
},
|
||||
emojis(): z.infer<typeof CustomEmoji>[] {
|
||||
return this.identity?.emojis || [];
|
||||
},
|
||||
instance(): z.infer<typeof Instance> | null {
|
||||
return this.identity?.instance || null;
|
||||
},
|
||||
account(): z.infer<typeof Account> | null {
|
||||
return this.identity?.account || null;
|
||||
},
|
||||
application(): z.infer<typeof CredentialApplication> | null {
|
||||
if (!this.identity) {
|
||||
identityOptional(state): Identity | null {
|
||||
if (state.activeIdentityId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
state.identities.find(
|
||||
(id) => id.id === state.activeIdentityId,
|
||||
) || null
|
||||
);
|
||||
},
|
||||
emojis(): z.infer<typeof CustomEmoji>[] {
|
||||
return this.identity.emojis;
|
||||
},
|
||||
instance(): z.infer<typeof Instance> {
|
||||
return this.identity.instance;
|
||||
},
|
||||
instanceOptional(): z.infer<typeof Instance> | null {
|
||||
return this.identityOptional?.instance ?? null;
|
||||
},
|
||||
account(): z.infer<typeof Account> {
|
||||
return this.identity.account;
|
||||
},
|
||||
accountOptional(): z.infer<typeof Account> | null {
|
||||
return this.identityOptional?.account ?? null;
|
||||
},
|
||||
application(): z.infer<typeof CredentialApplication> | null {
|
||||
return this.applications[this.identity.instance.domain] || null;
|
||||
},
|
||||
token(): z.infer<typeof Token> | null {
|
||||
return this.identity?.token || null;
|
||||
token(): z.infer<typeof Token> {
|
||||
return this.identity.token;
|
||||
},
|
||||
permissions(): RolePermission[] {
|
||||
const roles = this.account?.roles ?? [];
|
||||
const roles = this.account.roles;
|
||||
|
||||
return roles
|
||||
.flatMap((r) => r.permissions)
|
||||
|
|
@ -76,11 +97,11 @@ export const useAuthStore = defineStore("auth", {
|
|||
},
|
||||
client(): Client {
|
||||
const apiHost = window.location.origin;
|
||||
const domain = this.identity?.instance.domain;
|
||||
const domain = this.identityOptional?.instance.domain;
|
||||
|
||||
return new Client(
|
||||
domain ? new URL(`https://${domain}`) : new URL(apiHost),
|
||||
this.identity?.token.access_token ?? undefined,
|
||||
this.identityOptional?.token.access_token ?? undefined,
|
||||
{
|
||||
globalCatch: (error) => {
|
||||
toast.error(
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ export const calculateMentionsFromReply = (
|
|||
// Deduplicate mentions
|
||||
.filter((men, i, a) => a.indexOf(men) === i)
|
||||
// Remove self
|
||||
.filter((men) => men.id !== authStore.identity?.account.id);
|
||||
.filter((men) => men.id !== authStore.identity.account.id);
|
||||
|
||||
if (peopleToMention.length === 0) {
|
||||
return "";
|
||||
|
|
@ -72,8 +72,8 @@ export const useComposerStore = (key: ComposerStateKey) =>
|
|||
isOverCharacterLimit(): boolean {
|
||||
const authStore = useAuthStore();
|
||||
const characterLimit =
|
||||
authStore.identity?.instance.configuration.statuses
|
||||
.max_characters ?? 0;
|
||||
authStore.identity.instance.configuration.statuses
|
||||
.max_characters;
|
||||
|
||||
return this.characterCount > characterLimit;
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue