Skip to content

Commit

Permalink
fix after comments
Browse files Browse the repository at this point in the history
  • Loading branch information
christinaroise committed Sep 23, 2024
1 parent 9daeedd commit 635729b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 34 deletions.
10 changes: 7 additions & 3 deletions internationalization/supportedLanguages.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { uuid } from "@sanity/uuid";

export interface Language {
_key: string;
title: string;
Expand All @@ -8,24 +10,26 @@ export interface Language {

export const supportedLanguages: Language[] = [
{
_key: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
_key: uuid(),
title: "English",
id: "en",
icon: "🇬🇧",
default: true,
},
{
_key: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
_key: uuid(),
title: "Swedish",
id: "se",
icon: "🇸🇪",
default: false,
},
{
_key: `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`,
_key: uuid(),
title: "Norwegian",
id: "no",
icon: "🇳🇴",
default: false,
},
];

export const defaultLanguage = supportedLanguages.find((lang) => lang.default);
13 changes: 10 additions & 3 deletions studio/components/LanguageSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ const LanguageSelector = ({ value = [], onChange }: LanguageSelectorProps) => {

// Get the currently set default language
const currentDefaultLanguage = value.find((lang) => lang.default)?.id || null;
console.log(currentDefaultLanguage);

const handleLanguageSelection = (lang: Language) => {
const isSelected = value.some((item) => item.id === lang.id);
// Prevent deselecting the last remaining language
if (isSelected && value.length === 1) {
console.log("Cannot deselect the last remaining language.");
return; // Exit early if there's only one language selected
}

const updatedValue = isSelected
? value.filter((item) => item.id !== lang.id) // Deselect language
: [...value, { ...lang, default: false }]; // Select language

const newDefaultLanguage = getNewDefaultLanguage(
updatedValue,
currentDefaultLanguage,
lang,
lang
);

const finalValue = updatedValue.map((item) => ({
Expand Down Expand Up @@ -124,7 +131,7 @@ const LanguageSelector = ({ value = [], onChange }: LanguageSelectorProps) => {
const getNewDefaultLanguage = (
updatedLanguages: Language[],
currentDefault: string | null,
deselectedLang: Language,
deselectedLang: Language
): string | null => {
if (updatedLanguages.length === 1) {
return updatedLanguages[0].id; // Only one language left
Expand All @@ -134,7 +141,7 @@ const getNewDefaultLanguage = (
// Find a new default language if the current default is deselected
return (
supportedLanguages.find((lang) =>
updatedLanguages.some((item) => item.id === lang.id),
updatedLanguages.some((item) => item.id === lang.id)
)?.id || null
);
}
Expand Down
26 changes: 13 additions & 13 deletions studio/deskStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ const adminSection = (S: StructureBuilder) =>
S.document()
.schemaType(companyInfoID)
.documentId(companyInfoID)
.title("Parent Company"),
.title("Parent Company")
),
S.listItem()
.title("Company Locations")
.icon(PinIcon)
.child(
S.documentTypeList(companyLocationID).title("Company Locations"),
S.documentTypeList(companyLocationID).title("Company Locations")
),
S.listItem()
.title("Legal Documents")
.icon(CogIcon)
.child(
S.documentTypeList(legalDocumentID).title("Company Locations"),
S.documentTypeList(legalDocumentID).title("Legal Documents")
),
]),
])
);

// Site Settings Section
Expand All @@ -76,7 +76,7 @@ const siteSettingSection = (S: StructureBuilder) =>
S.document()
.schemaType(brandAssetsID)
.documentId(brandAssetsID)
.title("Brand Assets"),
.title("Brand Assets")
),
S.listItem()
.title("Navigation Manager")
Expand All @@ -85,13 +85,13 @@ const siteSettingSection = (S: StructureBuilder) =>
S.document()
.schemaType("navigationManager")
.documentId("navigationManager")
.title("Navigation Manager"),
.title("Navigation Manager")
),
S.listItem()
.title("Social Media Profiles")
.icon(UsersIcon)
.child(
S.document().schemaType(soMeLinksID).documentId(soMeLinksID),
S.document().schemaType(soMeLinksID).documentId(soMeLinksID)
),
S.listItem()
.title("Languages")
Expand All @@ -100,7 +100,7 @@ const siteSettingSection = (S: StructureBuilder) =>
S.document()
.schemaType(languageSettingsID)
.documentId(languageSettingsID)
.title("Languages"),
.title("Languages")
),
S.listItem()
.title("Default SEO")
Expand All @@ -109,13 +109,13 @@ const siteSettingSection = (S: StructureBuilder) =>
S.document()
.schemaType(defaultSeoID)
.documentId(defaultSeoID)
.title("Default SEO"),
.title("Default SEO")
),
S.listItem()
.title("Broken Links")
.icon(DoubleChevronRightIcon)
.child(S.documentTypeList(brokenLinkID).title("Redirects")),
]),
])
);

// Section for dynamic and customizable Pages
Expand Down Expand Up @@ -150,7 +150,7 @@ const specialPagesSection = (S: StructureBuilder) =>
S.document()
.schemaType(compensationsId)
.documentId(compensationsId)
.title("Compensations"),
.title("Compensations")
),
S.listItem()
.title("Customer Cases")
Expand All @@ -159,9 +159,9 @@ const specialPagesSection = (S: StructureBuilder) =>
S.document()
.schemaType(customerCasesPageID)
.documentId(customerCasesPageID)
.title("Customer Cases"),
.title("Customer Cases")
),
]),
])
);

// Main export
Expand Down
7 changes: 2 additions & 5 deletions studio/schemas/documents/languageSettings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import { defineType } from "sanity";

import { supportedLanguages } from "internationalization/supportedLanguages";
import { defaultLanguage } from "internationalization/supportedLanguages";
import LanguageSelector from "studio/components/LanguageSelector";

export const languageSettingsID = "languageSettings";
Expand All @@ -27,9 +26,7 @@ const languageSettings = defineType({
},
],
components: { input: LanguageSelector },
initialValue: () => [
supportedLanguages.find((language) => language.default)?.id,
],
initialValue: () => [defaultLanguage],
},
],
});
Expand Down
6 changes: 3 additions & 3 deletions studioShared/deskStructure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StructureResolver } from "sanity/structure";

import { supportedLanguages } from "internationalization/supportedLanguages";
import { defaultLanguage } from "internationalization/supportedLanguages";

import { customerCaseID } from "./schemas/documents/customerCase";

Expand All @@ -17,7 +17,7 @@ export const deskStructure: StructureResolver = (S) =>
.filter("_type == $type && language == $lang")
.params({
type: customerCaseID,
lang: supportedLanguages.find((language) => language.default)?.id,
}),
lang: defaultLanguage?.id,
})
),
]);
12 changes: 5 additions & 7 deletions studioShared/studioConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { structureTool } from "sanity/structure";
import { media } from "sanity-plugin-media";

import { languageID } from "internationalization/languageSchemaField";
import { supportedLanguages } from "internationalization/supportedLanguages";
import {
defaultLanguage,
supportedLanguages,
} from "internationalization/supportedLanguages";
import StudioIcon from "studio/components/studioIcon/StudioIcon";

import { deskStructure } from "./deskStructure";
Expand All @@ -25,9 +28,7 @@ const config: WorkspaceOptions = {
...schema,
templates: (prev) =>
prev.filter(
(template) =>
template.value.language ===
supportedLanguages.find((language) => language.default)?.id,
(template) => template.value.language === defaultLanguage?.id
),
},
plugins: [
Expand All @@ -37,9 +38,6 @@ const config: WorkspaceOptions = {
visionTool({ defaultApiVersion: apiVersion }),
media(),
documentInternationalization({
// TODO: a function that takes the client and returns a promise of an array of supported languages
// MUST return an "id" and "title" as strings
// supportedLanguages: (client) => client.fetch(`*[_type == "language"]{id, title}`),
supportedLanguages: supportedLanguages,
schemaTypes: [customerCaseID],
languageField: languageID,
Expand Down

0 comments on commit 635729b

Please sign in to comment.