hardlounge/client/js/store.js

215 lines
5.4 KiB
JavaScript
Raw Normal View History

import Vue from "vue";
import Vuex from "vuex";
import {createSettingsStore} from "./store-settings";
2019-11-16 17:24:03 +00:00
import storage from "./localStorage";
const appName = document.title;
Vue.use(Vuex);
function detectDesktopNotificationState() {
if (!("Notification" in window)) {
return "unsupported";
} else if (Notification.permission === "granted") {
return "granted";
}
return "blocked";
}
2021-04-29 23:35:08 +00:00
const store = new Vuex.Store({
2019-02-27 14:15:34 +00:00
state: {
2019-11-05 19:29:51 +00:00
appLoaded: false,
activeChannel: null,
currentUserVisibleError: null,
desktopNotificationState: detectDesktopNotificationState(),
isAutoCompleting: false,
2019-02-27 14:15:34 +00:00
isConnected: false,
networks: [],
mentions: [],
2019-11-16 17:24:03 +00:00
hasServiceWorker: false,
pushNotificationState: "unsupported",
serverConfiguration: null,
2019-03-03 17:47:49 +00:00
sessions: [],
sidebarOpen: false,
2019-08-05 12:24:44 +00:00
sidebarDragging: false,
userlistOpen: storage.get("thelounge.state.userlist") !== "false",
2019-08-05 14:29:35 +00:00
versionData: null,
versionStatus: "loading",
versionDataExpired: false,
serverHasSettings: false,
2019-12-31 16:21:34 +00:00
messageSearchResults: null,
messageSearchInProgress: false,
searchEnabled: false,
2019-02-27 14:15:34 +00:00
},
mutations: {
2019-11-05 19:29:51 +00:00
appLoaded(state) {
state.appLoaded = true;
},
activeChannel(state, channel) {
state.activeChannel = channel;
},
currentUserVisibleError(state, error) {
state.currentUserVisibleError = error;
},
refreshDesktopNotificationState(state) {
state.desktopNotificationState = detectDesktopNotificationState();
},
isAutoCompleting(state, isAutoCompleting) {
state.isAutoCompleting = isAutoCompleting;
},
2019-02-27 14:15:34 +00:00
isConnected(state, payload) {
state.isConnected = payload;
},
networks(state, networks) {
state.networks = networks;
},
mentions(state, mentions) {
state.mentions = mentions;
},
removeNetwork(state, networkId) {
state.networks.splice(
store.state.networks.findIndex((n) => n.uuid === networkId),
1
);
},
sortNetworks(state, sortFn) {
state.networks.sort(sortFn);
},
2019-11-16 17:24:03 +00:00
hasServiceWorker(state) {
state.hasServiceWorker = true;
},
pushNotificationState(state, pushNotificationState) {
state.pushNotificationState = pushNotificationState;
},
serverConfiguration(state, serverConfiguration) {
state.serverConfiguration = serverConfiguration;
},
2019-03-03 17:47:49 +00:00
sessions(state, payload) {
state.sessions = payload;
},
sidebarOpen(state, payload) {
state.sidebarOpen = payload;
},
2019-08-05 12:24:44 +00:00
sidebarDragging(state, payload) {
state.sidebarDragging = payload;
},
toggleSidebar(state) {
state.sidebarOpen = !state.sidebarOpen;
},
toggleUserlist(state) {
state.userlistOpen = !state.userlistOpen;
},
userlistOpen(state, payload) {
state.userlistOpen = payload;
},
2019-08-05 14:29:35 +00:00
versionData(state, payload) {
state.versionData = payload;
},
versionStatus(state, payload) {
state.versionStatus = payload;
},
versionDataExpired(state, payload) {
state.versionDataExpired = payload;
},
serverHasSettings(state, value) {
state.serverHasSettings = value;
},
2019-12-31 16:21:34 +00:00
messageSearchInProgress(state, value) {
state.messageSearchInProgress = value;
},
messageSearchResults(state, value) {
state.messageSearchResults = value;
},
addMessageSearchResults(state, value) {
2020-04-24 07:20:40 +00:00
// Append the search results and add networks and channels to new messages
2021-04-29 23:35:08 +00:00
value.results = [...state.messageSearchResults.results, ...value.results];
2020-04-24 07:20:40 +00:00
state.messageSearchResults = value;
},
2019-03-03 17:47:49 +00:00
},
getters: {
2019-11-03 11:23:03 +00:00
findChannelOnCurrentNetwork: (state) => (name) => {
name = name.toLowerCase();
return state.activeChannel.network.channels.find((c) => c.name.toLowerCase() === name);
},
2019-12-31 16:21:34 +00:00
findChannelOnNetwork: (state) => (networkUuid, channelName) => {
for (const network of state.networks) {
if (network.uuid !== networkUuid) {
continue;
}
for (const channel of network.channels) {
if (channel.name === channelName) {
return {network, channel};
}
}
}
return null;
},
findChannel: (state) => (id) => {
for (const network of state.networks) {
for (const channel of network.channels) {
if (channel.id === id) {
return {network, channel};
}
}
}
return null;
},
findNetwork: (state) => (uuid) => {
for (const network of state.networks) {
if (network.uuid === uuid) {
return network;
}
}
return null;
},
highlightCount(state) {
let highlightCount = 0;
for (const network of state.networks) {
for (const channel of network.channels) {
highlightCount += channel.highlight;
}
}
return highlightCount;
},
title(state, getters) {
const alertEventCount = getters.highlightCount ? `(${getters.highlightCount}) ` : "";
const channelname = state.activeChannel ? `${state.activeChannel.channel.name}` : "";
return alertEventCount + channelname + appName;
},
2019-11-12 15:51:40 +00:00
initChannel: () => (channel) => {
// TODO: This should be a mutation
channel.pendingMessage = "";
channel.inputHistoryPosition = 0;
channel.inputHistory = [""];
channel.historyLoading = false;
channel.scrolledToBottom = true;
channel.editTopic = false;
channel.moreHistoryAvailable = channel.totalMessages > channel.messages.length;
delete channel.totalMessages;
if (channel.type === "channel") {
channel.usersOutdated = true;
}
return channel;
},
},
});
// Settings module is registered dynamically because it benefits
// from a direct reference to the store
store.registerModule("settings", createSettingsStore(store));
export default store;